-
-
Notifications
You must be signed in to change notification settings - Fork 229
correctly resolve references to a type that is itself just a single allOf reference #1103
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
Merged
dbanty
merged 7 commits into
openapi-generators:main
from
eli-bl:issue-1091-allof-single
Aug 25, 2024
Merged
correctly resolve references to a type that is itself just a single allOf reference #1103
dbanty
merged 7 commits into
openapi-generators:main
from
eli-bl:issue-1091-allof-single
Aug 25, 2024
Conversation
This file contains hidden or 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
dbanty
previously approved these changes
Aug 25, 2024
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.
Thanks so much for tackling this problem!
dbanty
approved these changes
Aug 25, 2024
Merged
github-merge-queue bot
pushed a commit
that referenced
this pull request
Aug 25, 2024
> [!IMPORTANT] > Merging this pull request will create this release ## Fixes ### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum` Fixed by PR #1092. Thanks @mikkelam! ### Add missing `cast` import when using `const` Fixed by PR #1072. Thanks @dorcohe! ### Correctly resolve references to a type that is itself just a single allOf reference PR #1103 fixed issue #1091. Thanks @eli-bl! ### Support `const` booleans and floats Fixed in PR #1086. Thanks @flxdot! Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
micha91
pushed a commit
to micha91/openapi-python-client
that referenced
this pull request
May 13, 2025
…llOf reference (openapi-generators#1103) Fixes openapi-generators#1091 Example of a valid spec that triggered this bug: https://gist.github.com/eli-bl/8f5c7d1d872d9fda5379fa6370dab6a8 In this spec, CreateCat contains only an `allOf` with a single reference to CreateAnimal. The current behavior of `openapi-python-client` in such a case is that it treats CreateCat as simply an alias for CreateAnimal; any references to it are treated as references to CreateAnimal, and it doesn't bother creating a model class for CreateCat. And if the spec contained only those two types, then this would be successful. (Note, the term "alias" doesn't exist in OpenAPI/JSON Schema, but I'm using it here to mean "a type that extends one other type with `allOf`, with no changes." Whether that should be treated as a separate thing in any way is not really a concern of OpenAPI; it's an issue for us only because we are generating code for model classes. See also: openapi-generators#1104) Anyway, in this case the spec also contains UpdateCat, which extends CreateCat with an additional property. This _should_ be exactly the same as extending CreateAnimal... but, prior to this fix, it resulted in a parsing error. The problem happened like this: 1. In `_create_schemas`, we create a ModelProperty for each of the three schemas. * The one for CreateCat is handled slightly differently: its `data` attribute points to the exact same schema as CreateAnimal, and we do not add it into `schemas.classes_by_name` because we don't want to generate a separate Python class for it. 2. In `_process_models`, we're attempting to finish filling in the property list for each model. * That might not be possible right away because there might be a reference to another model that hasn't been fully processed yet. So we iterate as many times as necessary until they're all fully resolved. However... * What we are iterating over is `schemas.classes_by_name`. There's an incorrect assumption that every named model is included in that dict; in this case, CreateCat is not in it. * Therefore, CreateCat remains in an invalid state, and the reference from CreateAnimal to CreateCat causes an error. My solution is to use `classes_by_name` only for the purpose of determining what Python classes to generate, and add a new collection, `models_to_process`, which includes _every_ ModelProperty including ones that are aliases. After the fix, generating a client from the example spec succeeds. The only Python model classes created are CreateAnimal and UpdateCat; the `post` endpoint that referenced CreateCat uses the CreateAnimal class. Again, that's consistent with how `openapi-python-client` currently handles these type aliases; the difference is just that it no longer fails when it sees a reference _to_ the alias. --------- Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
micha91
pushed a commit
to micha91/openapi-python-client
that referenced
this pull request
May 13, 2025
> [!IMPORTANT] > Merging this pull request will create this release ## Fixes ### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum` Fixed by PR openapi-generators#1092. Thanks @mikkelam! ### Add missing `cast` import when using `const` Fixed by PR openapi-generators#1072. Thanks @dorcohe! ### Correctly resolve references to a type that is itself just a single allOf reference PR openapi-generators#1103 fixed issue openapi-generators#1091. Thanks @eli-bl! ### Support `const` booleans and floats Fixed in PR openapi-generators#1086. Thanks @flxdot! Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1091
Example of a valid spec that triggered this bug: https://gist.github.com/eli-bl/8f5c7d1d872d9fda5379fa6370dab6a8
In this spec, CreateCat contains only an
allOf
with a single reference to CreateAnimal. The current behavior ofopenapi-python-client
in such a case is that it treats CreateCat as simply an alias for CreateAnimal; any references to it are treated as references to CreateAnimal, and it doesn't bother creating a model class for CreateCat. And if the spec contained only those two types, then this would be successful.(Note, the term "alias" doesn't exist in OpenAPI/JSON Schema, but I'm using it here to mean "a type that extends one other type with
allOf
, with no changes." Whether that should be treated as a separate thing in any way is not really a concern of OpenAPI; it's an issue for us only because we are generating code for model classes. See also: #1104)Anyway, in this case the spec also contains UpdateCat, which extends CreateCat with an additional property. This should be exactly the same as extending CreateAnimal... but, prior to this fix, it resulted in a parsing error. The problem happened like this:
_create_schemas
, we create a ModelProperty for each of the three schemas.data
attribute points to the exact same schema as CreateAnimal, and we do not add it intoschemas.classes_by_name
because we don't want to generate a separate Python class for it._process_models
, we're attempting to finish filling in the property list for each model.schemas.classes_by_name
. There's an incorrect assumption that every named model is included in that dict; in this case, CreateCat is not in it.My solution is to use
classes_by_name
only for the purpose of determining what Python classes to generate, and add a new collection,models_to_process
, which includes every ModelProperty including ones that are aliases.After the fix, generating a client from the example spec succeeds. The only Python model classes created are CreateAnimal and UpdateCat; the
post
endpoint that referenced CreateCat uses the CreateAnimal class. Again, that's consistent with howopenapi-python-client
currently handles these type aliases; the difference is just that it no longer fails when it sees a reference to the alias.