OpenAPITools / OpenAPITools/openapi-generator
[BUG] [General] Problem with inheritance when subclass has no property
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)?
- What's the version of OpenAPI Generator used? - 5.0.0-beta
- Have you search for related issues/PRs?
Description
The problem occurs when reading a schema with subclasses, when the subclasses do not specify properties of their own, but only inherit all properties from their parent (e.g. in order to be futureproof, one might first specify the inheritance hierarchy and later on add specific properties for some subclasses).
The schema is valid according to https://editor.swagger.io/ and https://apitools.dev/swagger-parser/online/
When I try to run the code generator says "Invalid inline schema defined in allOf in 'Cat'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the allOf schemas must use $ref. Change this inline definition to a $ref definition"
Link to the stack trace (when run via Maven):
https://gist.github.com/MarcMil/557bee99da5dc4af38086c1f6088c6d2
openapi-generator version
5.0.0 Beta
OpenAPI declaration file content or url
Here the OpenAPI schema which does not work:
https://gist.github.com/MarcMil/4a78e766e755be073018111a67abae0f
This works, because there is a field "foo" in each subclass:
https://gist.github.com/MarcMil/707bc45824a8ee69daf93d6af0dc091d
Command line used for generation
java -jar openapi-generator-cli.jar generate -g java -i "OpenAPI.json"
Leads to:
Exception in thread "main" java.lang.RuntimeException: Could not process model 'Animal'.Please make sure that your schema is correct!
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:475)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:850)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:432)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Invalid inline schema defined in allOf in 'Cat'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the allOf schemas must use $ref. Change this inline definition to a $ref definition
at org.openapitools.codegen.DefaultCodegen.getAllOfDescendants(DefaultCodegen.java:2794)
at org.openapitools.codegen.DefaultCodegen.createDiscriminator(DefaultCodegen.java:2851)
at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:2229)
at org.openapitools.codegen.languages.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:1017)
at org.openapitools.codegen.languages.JavaClientCodegen.fromModel(JavaClientCodegen.java:723)
at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1127)
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:470)
... 4 more
Steps to reproduce
Try to read in the OpenAPI declaration file already breaks. Interestingly, validate works: "No validation issues detected."
Related issues/PRs
Unknown
Suggest a fix
Link to the stack trace:
https://gist.github.com/MarcMil/557bee99da5dc4af38086c1f6088c6d2
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 failure with the linked OpenAPI declaration and the Java generation command. Start at DefaultCodegen.getAllOfDescendants and createDiscriminator, which appear in the stack trace, and compare the failing schema with the working variant. Done means valid inheritance with propertyless subclasses generates without the reported exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100