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

Allow different serializer to an Annotated type for python and json mode #8432

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ChillPC
Copy link

@ChillPC ChillPC commented Dec 22, 2023

Change Summary

PlainSerializer and WrapSerializer can take the arguments :

  • when_used as a str like before or a set containing the literals python and/or json
  • unless_none as a bool

It will permit to have custom different serializer for the python and json mode using a JsonOrPythonSchema.

Related issue number

fix #8086

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

…ated_plain_only_python` because:

- json default serializer applied on `model_dump_json()`
- python serializer applied on both `model_dump(mode='json')`
Copy link

codspeed-hq bot commented Dec 22, 2023

CodSpeed Performance Report

Merging #8432 will not alter performance

Comparing ChillPC:different-serializer-mode (ad378db) with main (19fa86c)

Summary

✅ 10 untouched benchmarks

@ChillPC
Copy link
Author

ChillPC commented Dec 22, 2023

Hello, I am afraid this MR will not be possible without touching the pydantic-core because of the test not passing. The python serializer of the JsonOrPythonSchema is applied whenever model_dump is called (the mode change nothing). The json serializer is used only when model_dump_json is called.

@sydney-runkle
Copy link
Member

@ChillPC,

Thanks for your work on this! I'll chat with @davidhewitt on Monday about this, given that he did some advising on the initial issue 👍.

I think we'll probably include this new feature in 2.7.0... but stay tuned :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow different serializer to an Annotated type for "python" and "json" mode
3 participants