Skip to content

Commit f4b20fa

Browse files
committed
feat: implement BaseModel.model_dump_json
1 parent 00031eb commit f4b20fa

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

doc/changelog.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
Changelog
22
=========
33

4-
[0.2.5] - 2024-11-13
4+
[0.2.5] - Unreleased
55
--------------------
6+
67
Fixed
78
^^^^^
9+
- Implement :meth:`~scim2_models.BaseModel.model_dump_json`.
810

11+
[0.2.5] - 2024-11-13
12+
--------------------
13+
14+
Fixed
15+
^^^^^
916
- :meth:`~scim2_models.BaseModel.model_validate` types.
1017

1118
[0.2.4] - 2024-11-03

scim2_models/base.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -724,20 +724,13 @@ def model_validate(
724724
kwargs.setdefault("context", {}).setdefault("scim", scim_ctx)
725725
return super().model_validate(*args, **kwargs)
726726

727-
def model_dump(
727+
def _prepare_model_dump(
728728
self,
729-
*args,
730729
scim_ctx: Optional[Context] = Context.DEFAULT,
731730
attributes: Optional[list[str]] = None,
732731
excluded_attributes: Optional[list[str]] = None,
733732
**kwargs,
734-
) -> dict:
735-
"""Create a model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump`.
736-
737-
:param scim_ctx: If a SCIM context is passed, some default values of
738-
Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM
739-
messages. Pass :data:`None` to get the default Pydantic behavior.
740-
"""
733+
):
741734
kwargs.setdefault("context", {}).setdefault("scim", scim_ctx)
742735
kwargs["context"]["scim_attributes"] = [
743736
validate_attribute_urn(attribute, self.__class__)
@@ -751,9 +744,48 @@ def model_dump(
751744
if scim_ctx:
752745
kwargs.setdefault("exclude_none", True)
753746
kwargs.setdefault("by_alias", True)
754-
kwargs.setdefault("mode", "json")
755747

756-
return super().model_dump(*args, **kwargs)
748+
return kwargs
749+
750+
def model_dump(
751+
self,
752+
*args,
753+
scim_ctx: Optional[Context] = Context.DEFAULT,
754+
attributes: Optional[list[str]] = None,
755+
excluded_attributes: Optional[list[str]] = None,
756+
**kwargs,
757+
) -> dict:
758+
"""Create a model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump`.
759+
760+
:param scim_ctx: If a SCIM context is passed, some default values of
761+
Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM
762+
messages. Pass :data:`None` to get the default Pydantic behavior.
763+
"""
764+
dump_kwargs = self._prepare_model_dump(
765+
scim_ctx, attributes, excluded_attributes, **kwargs
766+
)
767+
if scim_ctx:
768+
dump_kwargs.setdefault("mode", "json")
769+
return super().model_dump(*args, **dump_kwargs)
770+
771+
def model_dump_json(
772+
self,
773+
*args,
774+
scim_ctx: Optional[Context] = Context.DEFAULT,
775+
attributes: Optional[list[str]] = None,
776+
excluded_attributes: Optional[list[str]] = None,
777+
**kwargs,
778+
) -> dict:
779+
"""Create a JSON model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump_json`.
780+
781+
:param scim_ctx: If a SCIM context is passed, some default values of
782+
Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM
783+
messages. Pass :data:`None` to get the default Pydantic behavior.
784+
"""
785+
dump_kwargs = self._prepare_model_dump(
786+
scim_ctx, attributes, excluded_attributes, **kwargs
787+
)
788+
return super().model_dump_json(*args, **dump_kwargs)
757789

758790
def get_attribute_urn(self, field_name: str) -> str:
759791
"""Build the full URN of the attribute.

tests/test_model_serialization.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def mut_resource():
6565
)
6666

6767

68+
def test_model_dump_json(mut_resource):
69+
assert (
70+
mut_resource.model_dump_json()
71+
== '{"schemas":["org:example:MutResource"],"id":"id","readOnly":"x","readWrite":"x","immutable":"x","writeOnly":"x"}'
72+
)
73+
74+
6875
def test_dump_default(mut_resource):
6976
"""By default, everything is dumped."""
7077
assert mut_resource.model_dump() == {

0 commit comments

Comments
 (0)