OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java/Spring] additionalProperties to true breaks compilation

Open
#8,003 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Spring
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

v5.0.0-beta2 and v5.0.0-beta3 do not generate correct Java putPropertiesItem method for Map fields

openapi-generator version

v5.0.0-beta2, v5.0.0-beta3 and latest

Works ok in v5.0.0-beta

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Test
  version: '1.0.0'

paths:
  /test:
    get:
      summary: Test
      description: Example
      responses:
        200:
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/test'

components:
  schemas:
    test:
      type: object
      required:
        - field_A
        - field_B
      properties:
        field_A:
          type: string
          enum:
            - Feature
        field_B:
          type: integer
        properties:
          type: object
          properties:
            field_C:
              type: integer
          additionalProperties: true


Generation Details

cat out/java/src/main/java/org/openapitools/model/Test.java
Expected


  public Test putPropertiesItem(String key, Object propertiesItem) {
    if (this.properties == null) {
      this.properties = new HashMap<>();
    }
    this.properties.put(key, propertiesItem);
    return this;
  }


vs

  public Test putPropertiesItem(String key, Object propertiesItem) {
    if (this.properties == null) {
      this.properties = ;
    }
    this.properties.put(key, propertiesItem);
    return this;
  }
Steps to reproduce
docker run --rm \                                          
    -v $PWD:/local openapitools/openapi-generator-cli:v5.0.0-beta3 generate \ 
    -i /local/test.yaml \
    -g spring \
    -o /local/out/java
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied OpenAPI YAML and the documented Docker command using the spring generator, then inspect the generated out/java/src/main/java/org/openapitools/model/Test.java. Trace how additionalProperties: true produces putPropertiesItem and verify the generated project compiles with the expected HashMap initialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.