-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
✨ Support providing serializing context to response models #11634
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
base: master
Are you sure you want to change the base?
Changes from all commits
3d7e369
16e7a5c
78d1336
aa50306
45b3e76
7a14e73
886c1f4
c2d27f5
8b802d1
bf51d60
14ed359
25716e3
606a7fe
f5b7890
663439d
90eb570
6caa3fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FrozenSet, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mapping, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Optional, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sequence, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tuple, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -69,8 +70,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with_info_plain_validator_function as with_info_plain_validator_function, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except ImportError: # pragma: no cover | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pydantic_core.core_schema import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| general_plain_validator_function as with_info_plain_validator_function, # noqa: F401 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pydantic_core.core_schema import ( # noqa: F401 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| general_plain_validator_function as with_info_plain_validator_function, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RequiredParam = PydanticUndefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -146,19 +147,35 @@ def serialize( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_unset: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_defaults: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_none: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| context: Optional[Dict[str, Any]] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # What calls this code passes a value that already called | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # self._type_adapter.validate_python(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._type_adapter.dump_python( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mode=mode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| include=include, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude=exclude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| by_alias=by_alias, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_unset=exclude_unset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_defaults=exclude_defaults, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_none=exclude_none, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # context argument was introduced in pydantic 2.8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if PYDANTIC_VERSION >= "2.8": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._type_adapter.dump_python( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mode=mode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| include=include, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude=exclude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| by_alias=by_alias, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_unset=exclude_unset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_defaults=exclude_defaults, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_none=exclude_none, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| context=context, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._type_adapter.dump_python( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mode=mode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| include=include, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude=exclude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| by_alias=by_alias, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_unset=exclude_unset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_defaults=exclude_defaults, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exclude_none=exclude_none, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were linting issues when changing this to use dict approach from #11670 so leaving as is.
Comment on lines
+154
to
+178
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This doesn't give any linting issues |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __hash__(self) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Each ModelField is unique for our purposes, to allow making a dict from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1075,6 +1075,7 @@ def add_api_route( | |||||
| response_model_exclude_unset: bool = False, | ||||||
| response_model_exclude_defaults: bool = False, | ||||||
| response_model_exclude_none: bool = False, | ||||||
| response_model_context: Optional[Dict[str, Any]] = None, | ||||||
| include_in_schema: bool = True, | ||||||
| response_class: Union[Type[Response], DefaultPlaceholder] = Default( | ||||||
| JSONResponse | ||||||
|
|
@@ -1105,6 +1106,7 @@ def add_api_route( | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -1133,6 +1135,7 @@ def api_route( | |||||
| response_model_exclude_unset: bool = False, | ||||||
| response_model_exclude_defaults: bool = False, | ||||||
| response_model_exclude_none: bool = False, | ||||||
| response_model_context: Optional[Dict[str, Any]] = None, | ||||||
| include_in_schema: bool = True, | ||||||
| response_class: Type[Response] = Default(JSONResponse), | ||||||
| name: Optional[str] = None, | ||||||
|
|
@@ -1162,6 +1165,7 @@ def decorator(func: DecoratedCallable) -> DecoratedCallable: | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -1711,6 +1715,21 @@ def get( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -1822,6 +1841,7 @@ def read_items(): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -2084,6 +2104,21 @@ def put( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -2200,6 +2235,7 @@ def replace_item(item_id: str, item: Item): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -2462,6 +2498,21 @@ def post( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -2578,6 +2629,7 @@ def create_item(item: Item): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -2840,6 +2892,21 @@ def delete( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -2951,6 +3018,7 @@ def delete_item(item_id: str): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -3213,6 +3281,21 @@ def options( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -3324,6 +3407,7 @@ def get_item_options(): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -3586,6 +3670,21 @@ def head( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -3697,6 +3796,7 @@ def get_items_headers(response: Response): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -3959,6 +4059,21 @@ def patch( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -4075,6 +4190,7 @@ def update_item(item: Item): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
@@ -4337,6 +4453,21 @@ def trace( | |||||
| """ | ||||||
| ), | ||||||
| ] = False, | ||||||
| response_model_context: Annotated[ | ||||||
| Optional[Dict[str, Any]], | ||||||
| Doc( | ||||||
| """ | ||||||
| Additional context to pass to Pydantic when creating the response. | ||||||
|
|
||||||
| This will be passed in as serialization context to the response model. | ||||||
|
|
||||||
| Note: This feature is a noop on pydantic < 2.8 | ||||||
|
|
||||||
| Read more about serialization context in the | ||||||
| [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/serialization/#serialization-context) | ||||||
| """ | ||||||
| ), | ||||||
| ] = None, | ||||||
| include_in_schema: Annotated[ | ||||||
| bool, | ||||||
| Doc( | ||||||
|
|
@@ -4448,6 +4579,7 @@ def trace_item(item_id: str): | |||||
| response_model_exclude_unset=response_model_exclude_unset, | ||||||
| response_model_exclude_defaults=response_model_exclude_defaults, | ||||||
| response_model_exclude_none=response_model_exclude_none, | ||||||
| response_model_context=response_model_context, | ||||||
| include_in_schema=include_in_schema, | ||||||
| response_class=response_class, | ||||||
| name=name, | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need this.
There are other places where the code may fail with older versions of Pydantic and if we try to cover all such cases we will end up having huge amount of runs