OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.83k stars 6.58k forks source link

[BUG][Java/Spring] putX method calls superclass method with incorrect arguments #12494

Closed WeihmannDev closed 2 years ago

WeihmannDev commented 2 years ago

Bug Report Checklist

Description

Assume that there is a schema ChildClass which inherits via allOf from schema ParentClass with discriminator, and ParentClass has a property x which references a schema that is defined via additionalProperty. Then the overwritten putX builder method in ChildClass is broken.

openapi-generator version

6.0.0, 6.0.1-SNAPSHOT

OpenAPI declaration file content or url
    ChildClass:
      description: ""
      type: object
      allOf:
        - $ref: "#/components/schemas/ParentClass"
        - type: object
          description: ""
          properties:
            objectType:
              type: string
              default: "ChildClass"

    ParentClass:
      type: object
      description: ""
      discriminator:
        propertyName: objectType
      properties:
        objectType:
          type: string
          default: "ParentClass"
        someMap:
          $ref: "#/components/schemas/MapClass"

    MapClass:
      type: object
      description: ""
      additionalProperties:
        type: string
        description: ""
Generation Details

Using openapi-generator-maven-plugin with the following configuration

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>6.0.1-SNAPSHOT</version>
  <configuration>
      <generateApis>true</generateApis>
      <generateModelTests>false</generateModelTests>
      <generateApiTests>false</generateApiTests>
      <configOptions>
          <interfaceOnly>true</interfaceOnly>
          <java8>true</java8>
          <useTags>true</useTags>
          <booleanGetterPrefix>is</booleanGetterPrefix>
          <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
          <skipDefaultInterface>true</skipDefaultInterface>
      </configOptions>
  </configuration>
  <executions>
      <execution>
          <id>generate-test-api</id>
          <goals>
              <goal>generate</goal>
          </goals>
          <configuration>
              <output>${project.build.directory}/generated-sources</output>
              <inputSpec>${project.basedir}/src/main/openapi/test.yaml</inputSpec>
              <generatorName>spring</generatorName>
              <modelPackage>dto</modelPackage>
              <apiPackage>api</apiPackage>
              <invokerPackage>invoker</invokerPackage>
          </configuration>
      </execution>
  </executions>
</plugin>
Steps to reproduce

Add the code at Generator Details to your pom and add the openapi code given above to some valid openapi file /src/main/openapi/test.yaml (I skipped parts of the yaml to keep it as succinct as possible).

The generated class ParentClass contains the following.

  public ParentClass putSomeMapItem(String key, String someMapItem) {
    if (this.someMap == null) {
      this.someMap = new HashMap<>();
    }
    this.someMap.put(key, someMapItem);
    return this;
  }

The generated class ChildClass contains the following.

  public ChildClass putSomeMapItem(String key, String someMapItem) {
    super.putSomeMapItem(someMapItem);
    return this;
  }

However, expected is the following.

  public ChildClass putSomeMapItem(String key, String someMapItem) {
    super.putSomeMapItem(key, someMapItem);
    return this;
  }
Related issues/PRs

I did not find any.

Suggest a fix
WeihmannDev commented 2 years ago

I'm currently preparing a pull request for a fix.

WeihmannDev commented 2 years ago

Fixed here https://github.com/OpenAPITools/openapi-generator/pull/12496.

icarusfire commented 1 year ago

@WeihmannTNG @welshm @wing328 @jimschubert

Hi, I am still having this problem, could you help please? thx!

https://stackoverflow.com/questions/76755112/how-to-stop-openapi-generator-maven-plugin-creates-putitem-method-for-type-obje