swagger-api / swagger-api/swagger-codegen
The jaxrs-spec generation for allof composition generates a flat class at 2.4.14 but hierarchical classes at 3.20.24 – this seems to contradict the spec
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
composition_2_yaml.txt
composition_3_yaml.txt
Description
The v2 spec https://swagger.io/specification/v2/ and v3 spec https://swagger.io/specification/ both state that a spec
“allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model composition. allOf takes in an array of object definitions that are validated independently but together compose a single object. While composition offers model extensibility, it does not imply a hierarchy between the models. To support polymorphism, the OpenAPI Specification adds the discriminator field.”
However at 2.4.14 with jaxrs-spec a single class containing all the fields is generated (this seems to comply with the specification docs) whereas 2.4.18 and 3.0.24 both generate two classes with one extending the other (this seems to conflict with the specification docs).
Given the attached ExtendedErrorModel/BasicErrorModel example files composition_2.yaml and composition_3.yaml taken from the docs as the composition example (https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/):
Running v2 generation:
java -jar swagger-codegen-cli-2.4.14.jar generate -i composition_2.yaml -l jaxrs-spec
… generates:
public class ExtendedErrorModel {
private @Valid String message = null;
private @Valid Integer code = null;
private @Valid String rootCause = null;
Running v3 generation:
java -jar swagger-codegen-cli-3.0.24.jar generate -i composition_3.yaml -l jaxrs-spec
… generates:
public class ExtendedErrorModel extends BasicErrorModel {
private @Valid String rootCause = null;
This seems to be incorrect?
Swagger-codegen version
swagger-codegen-cli-2.4.14.jar
swagger-codegen-cli-3.0.24.jar
Swagger declaration file content or url
Refer to attached files - extract below:
ExtendedErrorModel:
allOf: # Combines the BasicErrorModel and the inline model
- $ref: '#/definitions/BasicErrorModel'
- type: object
required:
- rootCause
properties:
rootCause:
type: string
Command line used for generation
java -jar swagger-codegen-cli-2.4.14.jar generate -i composition_2.yaml -l jaxrs-spec
java -jar swagger-codegen-cli-3.0.24.jar generate -i composition_3.yaml -l jaxrs-spec
Steps to reproduce
Run the command above.
Related issues/PRs
Unknown
Suggest a fix/enhancement
The behaviour should be as at 2.4.14, ie allOf with no discriminator results in a single class containing all of the fields.
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
Start with the attached composition_2_yaml.txt and composition_3_yaml.txt, then reproduce both generation commands using the jaxrs-spec generator for versions 2.4.14 and 3.0.24. Compare the generated ExtendedErrorModel and BasicErrorModel classes against the cited OpenAPI allOf and discriminator documentation. Done means the behavior is consistent with the chosen specification interpretation and covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100