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.84k stars 6.59k forks source link

[BUG] Java maven plugin: `_id` and `id` generates 2 getters with same name #getId #8291

Closed dmitry-weirdo closed 10 months ago

dmitry-weirdo commented 3 years ago

Bug Report Checklist

Description

My schema has two fields named _id and id, with different types:

      properties:
        _id:
          type: object
        id:
          type: integer

From this OAS, the openapi-generator-maven-plugin generates 2 fields:

  private @Valid Object id;
  private @Valid Integer id;

as well as get/set/build methods with same names, but different argument types. It works (as overloading) with setid and id, but getId methods differ only in return type, which is not allowed in java.

openapi-generator version

openapi-generator versions in pom.xml:

        <openapi-generator-maven-plugin.version>5.0.0</openapi-generator-maven-plugin.version>
        <jackson-databind-nullable-version>0.2.0</jackson-databind-nullable-version>
        <swagger-annotations-version>1.5.8</swagger-annotations-version>
Generation Details
            <plugin>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-assertions-generator-maven-plugin</artifactId>
                <version>${assertj-assertions-generator-maven-plugin.version}</version>
                <configuration>
                    <packages>
                        <param>ru.klavogonki.openapi.model</param>
                    </packages>

                    <entryPointClassPackage>ru.klavogonki.kgparser.assertions</entryPointClassPackage>
                    <hierarchical>true</hierarchical>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate-assertions</goal>
                        </goals>
                        <phase>generate-test-resources</phase>
                    </execution>
                </executions>
            </plugin>

mvn clean compile.

Steps to reproduce

Run maven, try to compile it, including the generated classes.

It fails on compiling the class:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project kgparser-srv: Compilation failure: Compilation failure:
[ERROR] /C:/java/kgparser/kgparserSrv/target/generated-sources/openapi/src/gen/java/ru/klavogonki/openapi/model/GetSummaryUser.java:[21,26] variable id is already defined in class ru.klavogonki.openapi.mo
del.GetSummaryUser
[ERROR] /C:/java/kgparser/kgparserSrv/target/generated-sources/openapi/src/gen/java/ru/klavogonki/openapi/model/GetSummaryUser.java:[80,18] method getId() is already defined in class ru.klavogonki.openapi
.model.GetSummaryUser
[ERROR] /C:/java/kgparser/kgparserSrv/target/generated-sources/openapi/src/gen/java/ru/klavogonki/openapi/model/GetSummaryUser.java:[81,12] incompatible types: java.lang.Object cannot be converted to java
.lang.Integer

Suggested solution

From _id field, a field with name _id and appropriate get/set/builder methods should be generated.

agilob commented 3 years ago

This is expected in java code generator as the naming convention removes special characters from field names.

Same thing is done in any other language generator, there was identical question last week about golang, same behaviour is fallowed.

dmitry-weirdo commented 3 years ago

Underscore _ is NOT a special characters and MUST NOT be escaped.

I've validated a trivial schema at https://apitools.dev/swagger-parser/online/, it passes the validation without any errors.

swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: >
    A sample API that uses a petstore as an example
    to demonstrate features in the swagger-2.0 specification
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      description: Returns all pets from the petstore
      responses:
        "200":
          description: pet response
          schema:
            type: array
            items:
              $ref: "#/definitions/pet"
definitions:
  pet:
    type: object
    properties:
      _id: 
        type: object
      id: 
        type: integer

Also for OAS 3.0, you can try the following on https://editor.swagger.io/, it also passes:

openapi: 3.0.3

info:
  title: TEst API 
  description: API collected from known endpoints
  version: 0.0.1
servers:
  - url: http://test.com/api/
    description: Main (production) server

paths:
  /profile/get-index-data:
    get:
      summary: 'Test summary'
      description: 'Test description'
      responses:
        200:
          description: 'Test description'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestSchema'

components:
  schemas:
    TestSchema:
      type: object
      properties:
        _id:
          type: object
        id:
          type: integer
cemartins commented 2 years ago

I have the same problem with an object with properties setId and id.
The compiler fails with method setId(java.lang.String) is already defined in class

Onarki commented 2 years ago

Still no workaround for this issue ?

gorjan-mishevski commented 10 months ago

You can fix this by adding the following command in the CLI: --name-mappings _id=_id

wing328 commented 10 months ago

right, nameMappings option can solve this problem

ref: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#name-mapping