Opt-in solution to remove unnecessary model for inline schema composition #9834
+285
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix #3100 fix #5171
Description
When there is a
ComposedSchema
with inline schemas, the children are flatten. What is does is it's creating a new schema, by taking the parent's name and adding_allOf
,_anyOf
or_oneOf
, or taking thetitle
value and appending all the properties defined in the inline schema. Then the inline schema becomes just a reference to the newly created schema which is added to theimports
of the parent. (example in this issue)This is alright when we have a
discriminator
in our inline schema and what we wish for is inheritance but when all we want is composition it's just creating unused schemas.A possible quick fix to prevent these unnecessary schemas is to ignore them in the
.openapi-generator-ignore
but it does not remove the schemas from theallModels
instance meaning that you can have reference to them in your generated files (like some imports or exports, hence possibly breaking the build).Solution
The proposed solution is to generate the new models only when it's for inheritance and just adding the properties of the inline schema to the parent (overriding the parent's properties when there is a name's conflict) without generating a new model. This solution comes with an opt-in CLI parameter :
--allow-inline-schemas
, to ease the merge of this fix.