Closed dmitry-weirdo closed 10 months 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.
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
Bug Report Checklist
Description
My schema has two fields named
_id
andid
, with different types:From this OAS, the
openapi-generator-maven-plugin
generates 2 fields:as well as get/set/build methods with same names, but different argument types. It works (as overloading) with
setid
andid
, butgetId
methods differ only in return type, which is not allowed in java.openapi-generator version
openapi-generator versions in
pom.xml
:Generation Details
mvn clean compile
.Steps to reproduce
Run maven, try to compile it, including the generated classes.
It fails on compiling the class:
Suggested solution
From
_id
field, a field with name_id
and appropriate get/set/builder methods should be generated.