correctly resolve references to a type that is itself just a single allOf reference #1103
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.