Skip to content

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

Merged
Kludex merged 13 commits intomainfrom
dmontagu/serialization-default-not-required
Sep 18, 2023
Merged

Make fields with defaults not required in the serialization schema by default#7275
Kludex merged 13 commits intomainfrom
dmontagu/serialization-default-not-required

Conversation

@dmontagu
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

please review

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

cloudflare-workers-and-pages Bot commented Aug 29, 2023

Comment thread docs/usage/model_config.md

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
Copy Markdown
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.

Comment thread pydantic/types.py

path = Path('directory')
path.mkdir()
path.mkdir(exist_ok=True)
Copy link
Copy Markdown
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
Copy Markdown
Member

Kludex commented Sep 18, 2023

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

Comment thread docs/usage/model_config.md Outdated

## JSON schema customization

#### `json_schema_serialization_defaults_required`
Copy link
Copy Markdown
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

Comment thread docs/usage/model_config.md Outdated
@Kludex Kludex enabled auto-merge (squash) September 18, 2023 09:18
@Kludex Kludex disabled auto-merge September 18, 2023 09:20
@Kludex Kludex enabled auto-merge (squash) September 18, 2023 09:25
@Kludex Kludex merged commit 3f6f847 into main Sep 18, 2023
@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
zmievsa pushed a commit to zmievsa/cadwyn that referenced this pull request Apr 20, 2026
There are cases where the validation and serialization schema of a model
will be different. In this case the default for FastAPI is to generate
an input schema and an output schema. It can be disabled with an option
which however is not being passed by Cadwyn down to FastAPI.


https://fastapi.tiangolo.com/how-to/separate-openapi-schemas/#model-for-input

Note that the mere presence of defaults is, contrary to what the fastapi
docs say, not sufficient to produce differing schemas, at least since
pydantic/pydantic#7275

Co-authored-by: Kostas Koukopoulos </usr/bin/[email protected]>
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