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

Make fields with defaults not required in the serialization schema by default #7275

Merged
merged 13 commits into from Sep 18, 2023

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Aug 29, 2023

Closes #7209

Note this PR is not against the main branch, but against @Kludex's branch for fixing the JSON schema generation based on config settings as it shares some implementation

This should have tests added for the new config settings before being merged into main, but I wanted to open now to get some initial review. Reading the discussion in the issue linked above may be helpful for understanding the motivation for this change.

Selected Reviewer: @lig

@dmontagu
Copy link
Contributor Author

please review

Base automatically changed from feat/support-ser-json-timedelta-bytes to main August 29, 2023 15:59
@cloudflare-pages
Copy link

cloudflare-pages bot commented Aug 29, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 475fc07
Status: ✅  Deploy successful!
Preview URL: https://f34f93d4.pydantic-docs2.pages.dev
Branch Preview URL: https://dmontagu-serialization-defau.pydantic-docs2.pages.dev

View logs


This can be useful when using frameworks (such as FastAPI) that may generate different schemas for validation
and serialization that must both be referenced from the same schema; when this happens, we automatically append
`-Input` to the definition reference for the validation schema and `-Output` to the definition reference for the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I removed the ability to override the -Input and -Output values was twofold:

  • Right now, these refs are computed after the config has been removed from the config stack. It was possible to work around this, but very ugly
  • I think the most compelling use case was to be able to set one of suffixes to be the empty string, e.g. so the input would use the empty string and output would use '-Output'. Unfortunately, the way I've implemented it, it tries to use the first definition reference that is not used by any other schema. Because the serialization schema tries to use without the suffix before using the suffix, it means the empty string just doesn't work, because there are always two schemas that want the name Model.

I believe with enough effort it may be possible to handle this better, but I think the changes still made by this PR are higher priority than controlling the definition ref suffix so I figured it was worth getting them in and deferring further work on this.

@@ -713,7 +713,7 @@ class Model(BaseModel):
path.unlink()

path = Path('directory')
path.mkdir()
path.mkdir(exist_ok=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to this PR, but this resolved some local test failures for me, and I don't see any reason not to keep it there.

@Kludex Kludex assigned Kludex and unassigned lig Sep 18, 2023
@Kludex
Copy link
Member

Kludex commented Sep 18, 2023

Assigning myself since I'm already in the loop here.


## JSON schema customization

#### `json_schema_serialization_defaults_required`
Copy link
Member

Choose a reason for hiding this comment

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

There's a limit on characters. Also, I don't think we should use parameter name as the name of the section.

Screenshot 2023-09-18 at 11 10 35

docs/usage/model_config.md Outdated Show resolved Hide resolved
@Kludex Kludex enabled auto-merge (squash) September 18, 2023 09:18
@Kludex Kludex enabled auto-merge (squash) September 18, 2023 09:25
@Kludex Kludex merged commit 3f6f847 into main Sep 18, 2023
49 checks passed
@Kludex Kludex deleted the dmontagu/serialization-default-not-required branch September 18, 2023 09:35
@davidhewitt davidhewitt added the relnotes-change Used for changes to existing functionality which don't have a better categorization. label Sep 21, 2023
hathawsh added a commit to OpenPaymentNetwork/openapi-pydantic that referenced this pull request Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review relnotes-change Used for changes to existing functionality which don't have a better categorization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

model serialization / validation schemas leaking into schema
4 participants