Bug Report Checklist
Description
Given an OpenAPI schema containing a component with several oneOf's, in which each oneOf entry has a unique 'type' value. Here's a partial example:
{
"components": {
"schemas": {
"ResponseContent": {
"discriminator": {
"propertyName": "type"
},
"oneOf": [
{
"$ref": "#/components/schemas/Title"
},
{
"$ref": "#/components/schemas/Subtitle"
},
{
"$ref": "#/components/schemas/Paragraph"
}
]
},
"Title": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"title"
]
}
}
},
"Subtitle": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"subtitle"
]
}
}
},
"Paragraph": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"paragraph"
]
}
}
}
}
}
}
Using the 7.0.1 java generator, the above schema will generate this Title model:
public class Title {
public enum TypeEnum {
TITLE("title"),
SUBTITLE("subtitle"),
PARAGRAPH("paragraph");
// ...
public static TypeEnum fromValue(String value) {
for (TypeEnum b : TypeEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
This indicates that the 7.0.1 java generator generates model code in which the first entry in the oneOf will match all the other entries. In the above case, the generated Title class matches with the typeEnum of title, subtitle and paragraph. This will cause the model to never be able to deserialize properly, as there are multiple options to deserialize json with type: 'subtitle' (because both Title and Subtitle classes match will it).
This did work properly in openapi-generator 6.6.0.
openapi-generator version
Issue observed in 7.0.1. It worked properly in 6.6.0.
OpenAPI declaration file content or url
See https://github.com/skwakman/openapi-generator-java-issue/blob/main/src/main/resources/example-schema.json .
Generation Details
See https://github.com/skwakman/openapi-generator-java-issue/blob/main/build.gradle.kts
Steps to reproduce
I've made an example project illustrating the issue.
- Check out https://github.com/skwakman/openapi-generator-java-issue
- run
./gradlew openApiGenerate
- See the generated code for
Title in build/generate-resources/main/src/main/java/com/example/api/model/Title.java. Its 'TypeEnum' matches with all the type values of the other components. The code for the other components (for instance in build/generate-resources/main/src/main/java/com/example/api/model/Subtitle.java) show that they only match for their specific type value.
- Run the project's Main class in
src/main/java to see what problem this causes: Jackson is not able to deserialize a Subtitle, because both Title and Subtitle classes match.
Change the version of openapi-generator in build.gradle.kts to 6.6.0, run ./gradlew clean and run steps 2-4 again. The Title class will contain the correct TypeEnum values.
Suggest a fix
There's probably a bug somewhere in the java generator, introduced since 7.0.1.
Bug Report Checklist
Description
Given an OpenAPI schema containing a component with several oneOf's, in which each oneOf entry has a unique 'type' value. Here's a partial example:
{ "components": { "schemas": { "ResponseContent": { "discriminator": { "propertyName": "type" }, "oneOf": [ { "$ref": "#/components/schemas/Title" }, { "$ref": "#/components/schemas/Subtitle" }, { "$ref": "#/components/schemas/Paragraph" } ] }, "Title": { "type": "object", "properties": { "type": { "type": "string", "enum": [ "title" ] } } }, "Subtitle": { "type": "object", "properties": { "type": { "type": "string", "enum": [ "subtitle" ] } } }, "Paragraph": { "type": "object", "properties": { "type": { "type": "string", "enum": [ "paragraph" ] } } } } } }Using the 7.0.1 java generator, the above schema will generate this Title model:
This indicates that the 7.0.1 java generator generates model code in which the first entry in the oneOf will match all the other entries. In the above case, the generated
Titleclass matches with the typeEnum oftitle,subtitleandparagraph. This will cause the model to never be able to deserialize properly, as there are multiple options to deserialize json withtype: 'subtitle'(because both Title and Subtitle classes match will it).This did work properly in openapi-generator 6.6.0.
openapi-generator version
Issue observed in 7.0.1. It worked properly in 6.6.0.
OpenAPI declaration file content or url
See https://github.com/skwakman/openapi-generator-java-issue/blob/main/src/main/resources/example-schema.json .
Generation Details
See https://github.com/skwakman/openapi-generator-java-issue/blob/main/build.gradle.kts
Steps to reproduce
I've made an example project illustrating the issue.
./gradlew openApiGenerateTitleinbuild/generate-resources/main/src/main/java/com/example/api/model/Title.java. Its 'TypeEnum' matches with all the type values of the other components. The code for the other components (for instance in build/generate-resources/main/src/main/java/com/example/api/model/Subtitle.java) show that they only match for their specific type value.src/main/javato see what problem this causes: Jackson is not able to deserialize a Subtitle, because both Title and Subtitle classes match.Change the version of openapi-generator in
build.gradle.ktsto 6.6.0, run./gradlew cleanand run steps 2-4 again. TheTitleclass will contain the correct TypeEnum values.Suggest a fix
There's probably a bug somewhere in the java generator, introduced since 7.0.1.