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

avoid misparsing references as other types using Pydantic discriminated unions #1216

Conversation

nkrishnaswami
Copy link
Contributor

@nkrishnaswami nkrishnaswami commented Mar 10, 2025

I’ve found a corner case, I think due to changes to Pydantic’s smart mode for disambiguating unions, where a subdocument with a $ref key and additional keys (title, description, etc) is validated as a Schema instead of as a Reference model. I believe these should be ignored per "any properties added SHALL be ignored" () This was a problem for me since I want to use a reference to an enum as a header parameter in FastAPI. The OpenAPI document generated by FastAPI included several fields (title, description, etc) along with $ref. TheSchema instance it was validate to lacked any actual type constraint information, so openapi-python-client needed to treat this as an AnyParameter, and disallowed it in headers.

I have attached an example OpenAPI document oa.json you can use to replicate this with the current openapi-python-client, or you can use the test cases from the first commit in this PR, which resolves it by using a Pydantic discriminated union testing for the presence of $ref for all the unions including Reference.

(I had created an issue upstream, but figured I'd share the workaround here.)

@nkrishnaswami nkrishnaswami changed the title Error processing some header parameters with reference schema fix: avoid misparsing references as other types using Pydantic discriminated unions Mar 13, 2025
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.

I think your read of the spec is right, the presence of $ref should guarantee it's treated like a reference. Thanks for the fix!

A couple recommendations:

  1. Taking prints out of the tests, I assume those were there for debugging
  2. We should add an e2e test for this as well, just to make sure if we tweak the way we use models at some point we don't get a regression. I think just adding some extra stuff to existing $refs in https://github.com/openapi-generators/openapi-python-client/blob/main/end_to_end_tests/baseline_openapi_3.0.json and verifying it doesn't change anything should be enough.

@nkrishnaswami
Copy link
Contributor Author

Thanks, @dbanty -- I'll make the updates now.

* Remove `print` statements
* add example verifying new behavior to E2E test
@nkrishnaswami
Copy link
Contributor Author

I've confirmed the modified E2E test fails without this change, also

@dbanty
Copy link
Collaborator

dbanty commented Mar 15, 2025

@nkrishnaswami hope you don't mind I pushed directly to try and solve mypy & Ruff's complaints in CI

@dbanty dbanty changed the title fix: avoid misparsing references as other types using Pydantic discriminated unions avoid misparsing references as other types using Pydantic discriminated unions Mar 15, 2025
@dbanty dbanty added this pull request to the merge queue Mar 15, 2025
Merged via the queue into openapi-generators:main with commit e6a597f Mar 15, 2025
22 checks passed
@nkrishnaswami
Copy link
Contributor Author

Don't mind at all -- thanks!

@nkrishnaswami nkrishnaswami deleted the use-discriminated-unions-for-references branch March 20, 2025 20:05
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