Skip to content

fix: Support discriminator for deserializing polymorphic types #717

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

Closed

Conversation

yoavk
Copy link

@yoavk yoavk commented Jan 5, 2023

Partially closes #219.

This fixes deserializing polymorphic types that have the same members, which otherwise would rely on order of types in the union.

@codecov
Copy link

codecov bot commented Jan 16, 2023

Codecov Report

Attention: Patch coverage is 16.66667% with 10 lines in your changes missing coverage. Please review.

Project coverage is 99.49%. Comparing base (aa324fc) to head (ee75c00).
Report is 244 commits behind head on main.

Files with missing lines Patch % Lines
...penapi_python_client/parser/properties/__init__.py 16.66% 10 Missing ⚠️
Additional details and impacted files
@@             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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@dbanty dbanty left a 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:

  1. An end-to-end test that adds a discriminated type (with partial mapping) to end_to_end_tests/openapi.json
  2. 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()
Copy link
Collaborator

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:

  1. We must enforce at parse time (return a ParseError if it fails) that every type within the union is a ModelProperty with a required property matching the discriminator
  2. 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 an if statement or, better yet, a dict to look up the correct class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support discriminator for polymorphic types and support oneOf
2 participants