OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] java generator with library jersey2 or 3 produces multiple getString methods in some cases
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When there is a oneOf schema with multiple options that resolve to strings, the code generated produce multiple getString methods one per oneOf option. Because of that, compilation fails with "method getString() is already defined in class MyResponse"
openapi-generator version
Tested with 7.7.0 and latest docker image (7.8.0-SNAPSHOT)
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example API v2
description: A reference API
contact: {}
version: 2.0.0
paths: {}
components:
schemas:
MySchema:
oneOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
A:
type: string
pattern: "^a:.*$"
B:
type: string
pattern: "^b:.*$"
Note this example uses pattern to be sure we have unique options, but in my case I'm using the extension x-extensible-enum to differentiate schema A from B.
Generation Details
language: java
library: jersey2
Steps to reproduce
-
create the file openapi-example.yml in the current directory
-
run:
docker run -v `pwd`:/temp -it openapitools/openapi-generator-cli:latest generate -g java --library=jersey2 -i /temp/openapi-example.yml -o /temp/build/ -
Look at the content of the generated file
build/src/main/java/org/openapitools/client/model/MySchema.javawhich will include twice the definition ofgetString()method:
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2024-07-09T09:53:46.452318051Z[Etc/UTC]", comments = "Generator version: 7.8.0-SNAPSHOT")
@JsonDeserialize(using = MySchema.MySchemaDeserializer.class)
@JsonSerialize(using = MySchema.MySchemaSerializer.class)
public class MySchema extends AbstractOpenApiSchema {
...
/**
* Get the actual instance of `String`. If the actual instance is not `String`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `String`
* @throws ClassCastException if the instance is not `String`
*/
public String getString() throws ClassCastException {
return (String)super.getActualInstance();
}
/**
* Get the actual instance of `String`. If the actual instance is not `String`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `String`
* @throws ClassCastException if the instance is not `String`
*/
public String getString() throws ClassCastException {
return (String)super.getActualInstance();
}
}
Suggest a fix
The issue is not present without setting library and is appearing in both jersey2 and jersey3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided OpenAPI YAML and Docker generation command using the Java jersey2 library, then inspect build/src/main/java/org/openapitools/client/model/MySchema.java. Confirm whether jersey3 shows the same result and identify why duplicate getString() methods are generated. Done means generated models compile without duplicate method definitions for the oneOf string schemas.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100