-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
fix: Support discriminator for deserializing polymorphic types #717
base: main
Are you sure you want to change the base?
Conversation
…lient into feature/union-discriminator
Codecov Report
@@ Coverage Diff @@
## main #717 +/- ##
===========================================
- Coverage 100.00% 99.49% -0.51%
===========================================
Files 49 49
Lines 1970 1982 +12
===========================================
+ Hits 1970 1972 +2
- Misses 0 10 +10
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
In addition to splitting out the discriminated type from union, test coverage will need to be added in a few ways:
- An end-to-end test that adds a discriminated type (with partial mapping) to
end_to_end_tests/openapi.json
- Unit tests which call the constructor directly and test for
ParseError
in the failure conditions (e.g., a discriminator field is not required or not present)
for property_value, schema_path in self.discriminator.mapping.items(): | ||
ref_path = parse_reference_path(schema_path) | ||
if isinstance(ref_path, ParseError): | ||
raise TypeError() |
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 try, when at all possible, not to raise
anything since it usually results in a poor error message for the consumer—who usually has no idea why the OpenAPI Document is wrong (I should probably add a code style section to the contribution guide).
I think in this case, it might be a good idea to create a new property type DiscriminatedProperty
or similar, since we should handle it in a few different ways:
- We must enforce at parse time (return a
ParseError
if it fails) that every type within the union is aModelProperty
with a required property matching the discriminator - Instead of trying every inner property at runtime, we only attempt the build the one matching the discriminator. So instead of a bunch of
try/except
, there will be anif
statement or, better yet, adict
to look up the correct class.
Partially closes #219.
This fixes deserializing polymorphic types that have the same members, which otherwise would rely on order of types in the union.