Fix OpenAPI discriminator with allOf when no container references parent#102
Open
staging-devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Open
Fix OpenAPI discriminator with allOf when no container references parent#102staging-devin-ai-integration[bot] wants to merge 1 commit intomainfrom
staging-devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
This fixes the issue where discriminator literal types were not applied to subtype schemas when using allOf inheritance without a container schema that references the parent. When a parent schema (like Pet) has a discriminator and subtypes (like Cat, Dog, Lizard) inherit from it via allOf, the discriminator literal types should be applied to the subtypes' discriminator field (e.g., petType should become Literal['cat'] in Cat). Previously, this only worked when there was a container schema (like PetContainer) that referenced the parent schema with a field. Now the discriminator types are applied even without such a container. Changes: - Added _get_discriminator_info_for_subtype() hook in base Parser class - Overrode the hook in OpenAPIParser to return discriminator info for subtypes - Extended __apply_discriminator_type() to process known subtypes directly - Added test case for discriminator+allOf without container reference Fixes koxudaxi#2528
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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
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.
Summary
This PR fixes issue koxudaxi#2528 - OpenAPI polymorphism with discriminator and allOf not working.
Problem
When a discriminator is used with allOf (as described in the OpenAPI spec), the discriminator literal types were not applied to subtype schemas if there was no container schema referencing the parent.
For example, given:
The generated
Catclass would not havepet_type: Literal['cat']unless there was another schema likePetContainerwith a field referencingPet.Solution
_get_discriminator_info_for_subtype()hook method in the baseParserclass that returnsNoneby defaultOpenAPIParserto return discriminator info for known subtypes (schemas that use allOf to inherit from a discriminated parent)__apply_discriminator_type()in the base parser to also process models that are known subtypes, applying the discriminator literal types even when not referenced by a fieldChanges
src/datamodel_code_generator/parser/base.py: Added hook method and extended discriminator processing logicsrc/datamodel_code_generator/parser/openapi.py: Implemented hook to provide discriminator info for subtypestests/main/openapi/test_main_openapi.py: Added test casetests/data/openapi/discriminator_allof_no_container.yaml: Test input schema (exact example from OpenAPI spec)tests/data/expected/main/openapi/discriminator/allof_no_container.py: Expected outputTesting
All discriminator-related tests pass:
Example
Input schema (from OpenAPI spec example):
Generated output (now correct):