OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generation of composed Schemas that also have additionalproperties: true
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?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
Is it really expected that ModelUtils.IsMapSchema() returns true for a composed schema that has additionalproperties: true? I am not sure if this is a bug or not.
See the code below. When the input is a composed schema, that also has additionalproperties: true, ModelUtils.IsMapSchema() returns true.
https://github.com/OpenAPITools/openapi-generator/blob/3ad4f06898170bbe900aeab0578ba6dc46d6a751/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java#L440
The reason I bring this up is because I was surprised that when you set "additionalproperties: true" in the OAS document, that may cause fewer models to be generated as a side effect. I am starting to understand why, and I'm adding documentation in #5316 to explain why. When you add "additionalProperties: true" to a schema, the parser uses a MapSchema instead of ObjectSchema, and that in turn causes the "generate-alias-as-model" logic to kick in. By default generate-alias-as-model is false, which controls when models are skipped. One item that would benefit some explanation is an "alias". The word "alias" is not used anywhere in the OpenAPI specification. What is a good way to describe what an "alias" mean in OpenAPITools?
openapi-generator version
master February 13th 2020
OpenAPI declaration file content or url
Consider the following YAML. It is a composed 'allOf' schema, and ModelUtils.isMapSchema() returns false for that schema. This could be used to model a concrete "Address" class that extends from AbstractAddress. Note that the "Address" schema in codegen itself does not have properties, since the properties are in the referenced schemas.
Address:
allOf:
- $ref: '#/components/schemas/AbstractAddress'
- type: object
properties:
name:
type: string
Now add additionalProperties: {} to the same schema. For example, this may be needed because somehow "Address" requires dynamic properties that are not known at design time. In that case, the ModelUtils.isMapSchema() function returns true.
Address:
additionalProperties: {}
allOf:
- $ref: '#/components/schemas/AbstractAddress'
- type: object
properties:
name:
type: string
This difference matters at https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L453.
The concrete class "Address" is generated when additionalproperties: false
The concrete class "Address" is NOT generated when additionalproperties: true. This was completely unexpected to me and looks like a bug.
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix
- Change ModelUtils.isMapSchema() function. Always return false for composed schemas?? Maybe this is going to have bad side effects. I'm not sure.
- Change the DefaultGenerator to handle the case when ModelUtils.isMapSchema() returns true, but the schema is also a composed schema. Maybe introspect the composed schema to determine if it has properties.
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 ModelUtils.isMapSchema() and DefaultGenerator.java at the referenced locations, then reproduce the difference using the two Address schemas in the issue. Trace why a composed schema with additionalProperties causes the model to be skipped. Done means composed schemas with dynamic properties have the intended generated model behavior without breaking map handling.
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