[BUG] Java maven plugin: `_id` and `id` generates 2 getters with same name #getId
Bug Report Checklist
- [ x] Have you provided a full/minimal spec to reproduce the issue?
- [ x] Have you validated the input using an OpenAPI validator (example)?
- [ x] Have you tested with the latest master to confirm the issue still exists?
- [ x] Have you searched for related issues/PRs?
- [ x] What's the actual output vs expected output?
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.
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.
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
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
Still no workaround for this issue ?
You can fix this by adding the following command in the CLI: --name-mappings _id=_id
right, nameMappings option can solve this problem
ref: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#name-mapping