Skip to content
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 discriminator mapping resolution in schemas with parent-child hierarchy #2145

Merged
merged 8 commits into from
Nov 10, 2024

Conversation

sternakt
Copy link
Contributor

@sternakt sternakt commented Nov 5, 2024

This PR addresses #1832, where using discriminators in schemas with specific property mappings can cause a RuntimeError: Discriminator type is not found.

Background

The error occurs because the current discriminator mapping resolution fails to properly handle cases where the discriminator field appears in parent models, preventing the correct mapping key from being located.

Problem Description

Previously, the code only checked the discriminator_model path against the mapping keys, but it did not account for scenarios where:

The discriminator path does not directly match the path of the child models, although it matches their parent classes.
Paths need to be validated in a more recursive fashion, checking both parent and child models for a valid mapping.

Solution

The proposed solution introduces a helper function, check_paths(), to validate the paths for both the main discriminator_model and its base classes when the initial mapping fails. This function:

  • Verifies mapping keys in both the main model and its parent classes.

Summary

  • Introduced check_paths() function to handle discriminator mapping resolution.
  • Ensured that the discriminator field is validated against both the main model and its parent models, preventing RuntimeError.

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cb0462a) to head (550b5ab).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2145   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           38        38           
  Lines         4259      4263    +4     
  Branches       984       984           
=========================================
+ Hits          4259      4263    +4     
Flag Coverage Δ
unittests 99.67% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link

codspeed-hq bot commented Nov 5, 2024

CodSpeed Performance Report

Merging #2145 will not alter performance

Comparing sternakt:main (550b5ab) with main (cb0462a)

Summary

✅ 30 untouched benchmarks

@koxudaxi koxudaxi merged commit 6c3e114 into koxudaxi:main Nov 10, 2024
76 checks passed
@koxudaxi
Copy link
Owner

Thank you very much!!

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.

2 participants