Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Added
^^^^^
- Proper path validation for :attr:`~scim2_models.SearchRequest.attributes`, :attr:`~scim2_models.SearchRequest.excluded_attributes` and :attr:`~scim2_models.SearchRequest.sort_by`.
- Implement :meth:`~scim2_models.PatchOp.patch`

Fixed
^^^^^
Expand Down
46 changes: 43 additions & 3 deletions doc/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,49 @@ This can be used by client applications that intends to dynamically discover ser
:language: json
:caption: schema-group.json

Bulk and Patch operations
=========================
Patch operations
================

:class:`~scim2_models.PatchOp` allows you to apply patch operations to modify SCIM resources.
The :meth:`~scim2_models.PatchOp.patch` method applies operations in sequence and returns whether the resource was modified. The return code is a boolean indicating whether the object have been modified by the operations.

.. note::
:class:`~scim2_models.PatchOp` takes a type parameter that should be the class of the resource
that is expected to be patched.

.. code-block:: python

>>> from scim2_models import User, PatchOp, PatchOperation
>>> user = User(user_name="john.doe", nick_name="Johnny")

>>> payload = {
... "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
... "Operations": [
... {"op": "replace", "path": "nickName", "value": "John" },
... {"op": "add", "path": "emails", "value": [{"value": "john@example.com"}]},
... ]
... }
>>> patch = PatchOp[User].model_validate(
... payload, scim_ctx=Context.RESOURCE_PATCH_REQUEST
... )

>>> modified = patch.patch(user)
>>> print(modified)
True
>>> print(user.nick_name)
John
>>> print(user.emails[0].value)
john@example.com

.. warning::

Patch operations are validated in the :attr:`~scim2_models.Context.RESOURCE_PATCH_REQUEST`
context. Make sure to validate patch operations with the correct context to
ensure proper validation of mutability and required constraints.

Bulk operations
===============

.. todo::

Bulk and Patch operations are not implemented yet, but any help is welcome!
Bulk operations are not implemented yet, but any help is welcome!
10 changes: 2 additions & 8 deletions scim2_models/rfc7644/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ def __new__(

def get_resource_class(obj) -> Optional[type[Resource]]:
"""Extract the resource class from generic type parameter."""
metadata = getattr(obj.__class__, "__pydantic_generic_metadata__", None)
if not metadata or not metadata.get("args"):
return None

metadata = getattr(obj.__class__, "__pydantic_generic_metadata__", {"args": [None]})
resource_class = metadata["args"][0]
if isinstance(resource_class, type) and issubclass(resource_class, Resource):
return resource_class

return None
return resource_class
Loading
Loading