|
| 1 | +Build a patch from two resource states |
| 2 | +====================================== |
| 3 | + |
| 4 | +Use this guide when an application holds the state a peer is believed to have and the state it |
| 5 | +should have, and must send the modification. :meth:`~scim2_models.PatchOp.build_from` compares the |
| 6 | +two and returns the :class:`~scim2_models.PatchOp` that closes the gap. |
| 7 | + |
| 8 | +Build the patch |
| 9 | +--------------- |
| 10 | + |
| 11 | +Pass the state the peer holds first, then the state it should hold: |
| 12 | + |
| 13 | +.. doctest:: |
| 14 | + |
| 15 | + >>> from scim2_models import Name, PatchOp, User |
| 16 | + >>> distant = User(user_name="bjensen", name=Name(given_name="Barbara", family_name="Jensen")) |
| 17 | + >>> wanted = User(user_name="bjensen", name=Name(given_name="Babs", family_name="Jensen")) |
| 18 | + >>> patch = PatchOp.build_from(distant, wanted) |
| 19 | + >>> patch.model_dump()["Operations"] |
| 20 | + [{'op': 'replace', 'path': 'name.givenName', 'value': 'Babs'}] |
| 21 | + |
| 22 | +A complex attribute is compared sub-attribute by sub-attribute, and each one gets its own path. |
| 23 | +Targeting ``name`` as a whole would replace it entirely and drop what the operation does not |
| 24 | +carry. |
| 25 | + |
| 26 | +Leave alone what the wanted state does not name |
| 27 | +----------------------------------------------- |
| 28 | + |
| 29 | +Only the attributes the wanted state names take part in the comparison. This is what separates a |
| 30 | +patch from the :meth:`~scim2_models.Resource.replace` it stands for: an attribute the peer |
| 31 | +maintains and the application does not model survives the modification. |
| 32 | + |
| 33 | +.. doctest:: |
| 34 | + |
| 35 | + >>> distant = User(user_name="bjensen", nick_name="Barb", title="CEO") |
| 36 | + >>> wanted = User(user_name="bjensen", nick_name="Babs") |
| 37 | + >>> patch = PatchOp.build_from(distant, wanted) |
| 38 | + >>> patch.model_dump()["Operations"] |
| 39 | + [{'op': 'replace', 'path': 'nickName', 'value': 'Babs'}] |
| 40 | + |
| 41 | +Naming an attribute with no value says the opposite. ``title=None`` reads as "clear the title", |
| 42 | +where an unnamed ``title`` reads as "leave it alone": |
| 43 | + |
| 44 | +.. doctest:: |
| 45 | + |
| 46 | + >>> wanted = User(user_name="bjensen", title=None) |
| 47 | + >>> patch = PatchOp.build_from(distant, wanted) |
| 48 | + >>> patch.model_dump()["Operations"] |
| 49 | + [{'op': 'remove', 'path': 'title'}] |
| 50 | + |
| 51 | +The same rule reaches sub-attributes, and an extension is named by its schema URN: |
| 52 | + |
| 53 | +.. doctest:: |
| 54 | + |
| 55 | + >>> from scim2_models import EnterpriseUser |
| 56 | + >>> distant = User[EnterpriseUser](user_name="bjensen") |
| 57 | + >>> distant[EnterpriseUser] = EnterpriseUser(department="Tour") |
| 58 | + >>> wanted = User[EnterpriseUser](user_name="bjensen") |
| 59 | + >>> wanted[EnterpriseUser] = EnterpriseUser(department="Chess") |
| 60 | + >>> patch = PatchOp.build_from(distant, wanted) |
| 61 | + >>> str(patch.operations[0].path) |
| 62 | + 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department' |
| 63 | + |
| 64 | +Send nothing when nothing changed |
| 65 | +--------------------------------- |
| 66 | + |
| 67 | +Two states that agree have no patch to describe: an ``Operations`` array must hold at least one |
| 68 | +operation, per :rfc:`RFC7644 §3.5.2 <7644#section-3.5.2>`. :meth:`~scim2_models.PatchOp.build_from` |
| 69 | +returns :data:`None`, so an application tests it before sending a request: |
| 70 | + |
| 71 | +.. doctest:: |
| 72 | + |
| 73 | + >>> PatchOp.build_from(wanted, wanted) is None |
| 74 | + True |
| 75 | + |
| 76 | +Know how collections are compared |
| 77 | +--------------------------------- |
| 78 | + |
| 79 | +A multi-valued attribute is replaced as a whole. Only the sub-attributes the wanted entries name |
| 80 | +decide whether it changed, so the sub-attributes the peer alone maintains do not read as a |
| 81 | +difference: |
| 82 | + |
| 83 | +.. doctest:: |
| 84 | + |
| 85 | + >>> from scim2_models import Email |
| 86 | + >>> distant = User(emails=[Email(value="barb@example.com", type="work", primary=True)]) |
| 87 | + >>> wanted = User(emails=[Email(value="barb@example.com")]) |
| 88 | + >>> PatchOp.build_from(distant, wanted) is None |
| 89 | + True |
| 90 | + |
| 91 | +When the collection does change, the whole of it is replaced and the peer's own sub-attributes go |
| 92 | +with it: |
| 93 | + |
| 94 | +.. doctest:: |
| 95 | + |
| 96 | + >>> wanted = User(emails=[Email(value="babs@example.com")]) |
| 97 | + >>> patch = PatchOp.build_from(distant, wanted) |
| 98 | + >>> patch.model_dump()["Operations"] |
| 99 | + [{'op': 'replace', 'path': 'emails', 'value': [{'value': 'babs@example.com'}]}] |
| 100 | + |
| 101 | +The builder cannot do better: :rfc:`RFC7643 §2.4 <7643#section-2.4>` gives the entries of a |
| 102 | +multi-valued attribute no identity, so nothing distinguishes an entry that changed from an entry |
| 103 | +that was removed and another that was added. An application that knows how to identify its own |
| 104 | +entries writes those operations itself, targeting a sub-attribute through a filter such as |
| 105 | +``emails[type eq "work"].value``. |
| 106 | + |
| 107 | +Read what the patch never carries |
| 108 | +--------------------------------- |
| 109 | + |
| 110 | +A ``readOnly`` attribute is left out however much the two states differ: |
| 111 | +:rfc:`RFC7644 §3.5.2 <7644#section-3.5.2>` forbids a client to modify one, and naming it would |
| 112 | +make the patch invalid. That covers :attr:`~scim2_models.Resource.id`, |
| 113 | +:attr:`~scim2_models.Resource.meta` and :attr:`~scim2_models.User.groups`, along with the |
| 114 | +``readOnly`` sub-attributes of a complex attribute. |
| 115 | + |
| 116 | +An ``immutable`` attribute that holds no value yet is added, which |
| 117 | +:rfc:`RFC7644 §3.5.2 <7644#section-3.5.2>` allows. One that already holds a value cannot be |
| 118 | +modified, and :meth:`~scim2_models.PatchOp.build_from` raises a |
| 119 | +:class:`~scim2_models.MutabilityException` rather than building a request the peer must refuse. |
| 120 | + |
| 121 | +An attribute a server never returns, such as :attr:`~scim2_models.User.password`, reads as unset on |
| 122 | +the side of the peer. Every patch built from a state naming it carries it again. |
0 commit comments