Correctly set required and nullable json schema values #43
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using this library on models that has a
json_schema_extra
field populated with "nullable" False etc the resulting model will not be marked as nullable in the json schema.pydantic-partial
should mark all fields as optional in the resulting partial model.An example is having a pydantic model with a field that has a default value.
In API responses you want the field to not be nullable (as it has a default value, it will not be null in the API response).
To acchieve this you can create a model like this:
This will make sure the field
name
has a default value if not provided, AND that the json schema for the API response iwll not mark it as nullable.But for a PATCH request etc you can use pydantic-partial to create a version of this model where all fields are optional for the request body.
The issue is that this library won't override the json_schema_extra values already existing on the model.
Results before my changes:
Partial A:
PartialB:
PartialC: (Has a default value, and no "required" or "nullable" values. Means it is nullable)
Results after the changes in this PR:
PartialA:
PartialB:
PartialC: (Has a default value, and no "required" or "nullable" values. Means it is nullable)