-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python] Adds allOf/oneOf/anyOf composed models #4446
Merged
jimschubert
merged 3 commits into
OpenAPITools:master
from
spacether:allof_anyof_oneof_addition
Dec 15, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1807,17 +1807,22 @@ public CodegenModel fromModel(String name, Schema schema) { | |
m.allVars = new ArrayList<CodegenProperty>(); | ||
if (composed.getAllOf() != null) { | ||
int modelImplCnt = 0; // only one inline object allowed in a ComposedModel | ||
int modelDiscriminators = 0; // only one discriminator allowed in a ComposedModel | ||
for (Schema innerSchema : composed.getAllOf()) { // TOOD need to work with anyOf, oneOf as well | ||
if (m.discriminator == null) { | ||
if (m.discriminator == null && innerSchema.getDiscriminator() != null) { | ||
LOGGER.debug("discriminator is set to null (not correctly set earlier): {}", name); | ||
m.discriminator = createDiscriminator(name, schema); | ||
m.discriminator = createDiscriminator(name, innerSchema); | ||
modelDiscriminators++; | ||
} | ||
|
||
if (innerSchema.getXml() != null) { | ||
m.xmlPrefix = innerSchema.getXml().getPrefix(); | ||
m.xmlNamespace = innerSchema.getXml().getNamespace(); | ||
m.xmlName = innerSchema.getXml().getName(); | ||
} | ||
if (modelDiscriminators > 1) { | ||
LOGGER.error("Allof composed schema is inheriting >1 discriminator. Only use one discriminator: {}", composed); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should error out if the user tries to define multiple discriminators in one schema. |
||
|
||
if (modelImplCnt++ > 1) { | ||
LOGGER.warn("More than one inline schema specified in allOf:. Only the first one is recognized. All others are ignored."); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per here: https://swagger.io/specification/#discriminatorObject
We should be able to set the discriminator on the parent composed schema, but that's not yet working because of this bug in swagger parser: swagger-api/swagger-parser#1269
In place of having discriminator at the root level of a composed schema, we should be able to use it from our definition in our allOf schemas.
I updated this code to set the discriminator on self if one of its ancestor allOf schemas defines discriminator.
Please see my ParentPet class which demonstrates this usage at: https://github.com/spacether/openapi-generator/blob/allof_anyof_oneof_addition/modules/openapi-generator/src/test/resources/2_0/python-client-experimental/petstore-with-fake-endpoints-models-for-testing.yaml#L2072
NOTE: the original invocation here
m.discriminator = createDiscriminator(name, schema);
did not do anything because we already ran that exact line above in line 1772.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this seems reasonable, but I'd like to have someone else on the @OpenAPITools/generator-core-team review as well since it affects all generators.