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

Add a convenience type to override JSON schema #5788

Merged
merged 3 commits into from
May 18, 2023
Merged

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented May 17, 2023

Closes #5096
Closes #4079

Introduces an annotation WithJsonSchema that you can use to override the JSON schema for a type.

Also makes it so that if you raise pydantic_core.PydanticOmit while generating the JSON schema for a field of any type that has fields (i.e., model, dataclass, typeddict), that field is omitted from the generated JSON schema.

The tests added to this PR demonstrate how to use this functionality to address:

Selected Reviewer: @samuelcolvin

@dmontagu dmontagu mentioned this pull request May 17, 2023
@dmontagu
Copy link
Contributor Author

@adriangb has suggested using pydantic_core.PydanticOmit rather than a new exception class. As long as we document it somewhere (which we need to do anyway), I think that's fine, I'll make the change.

@cloudflare-pages
Copy link

cloudflare-pages bot commented May 17, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: eb9a90e
Status: ✅  Deploy successful!
Preview URL: https://7fb14495.pydantic-docs2.pages.dev
Branch Preview URL: https://json-schema-override.pydantic-docs2.pages.dev

View logs

@dmontagu
Copy link
Contributor Author

Also Adrian I know I suggested we should re-export pydantic_core.PydanticOmit but I take it back — considering this will be pretty niche anyway I think it's fine to just leave as an import from pydantic_core unless we get feedback to the contrary. (Happy to change that though if you or Samuel prefer.)

@dmontagu
Copy link
Contributor Author

please review

Copy link
Member

@adriangb adriangb left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@dmontagu dmontagu changed the title Add some convenience types for overriding JSON schema Add a convenience type to override JSON schema May 18, 2023
@dmontagu dmontagu enabled auto-merge (squash) May 18, 2023 14:34
@dmontagu dmontagu merged commit 58b59f6 into main May 18, 2023
39 checks passed
@dmontagu dmontagu deleted the json-schema-override branch May 18, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON schema: Callable model.schema() fails when using arbitrary types
3 participants