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

Support Pydantic root model as query parameter #11306

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

MarkusSintonen
Copy link

@MarkusSintonen MarkusSintonen commented Mar 17, 2024

Fixes #11134

Adds support for Pydantic root model in query/path parameters. Supports PydaticV2 RootModel[X] and PydanticV1 BaseModel.__root__. This is a non breaking change as previously root models usage threw errors on route initialization when they were not used as bodies.

Adds lots of (previously missing) tests.

@MarkusSintonen MarkusSintonen changed the title feat: Support Pydantic root model as query parameter Support Pydantic root model as query parameter Mar 17, 2024

return field_annotation_is_scalar(
field.field_info.annotation
) and not isinstance(field.field_info, params.Body)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check was missing from v1 side. Lifted the params.Body check out from this "scalar field check" into is_body_param check which is more appropriate for checking the params.Body case.

elif not field_annotation_is_scalar(annotation=type_annotation):
elif (
not field_annotation_is_scalar(type_annotation)
# Root models by default regarded as bodies
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backward compatibility. Root models have been regarded as body params by default eventhough they were scalar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to explicitly require annotating by Body/Query,etc

@MarkusSintonen
Copy link
Author

Can I have a review @tiangolo / @alejsdev 🙏

@tiangolo tiangolo added bug Something isn't working p3 p2 and removed p3 labels Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use RootModel as query parameter
2 participants