Skip to content

Commit

Permalink
fix: extension payloads are not required on response contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Sep 20, 2024
1 parent 972bd8f commit 338ea04
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
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
Fixed
^^^^^
- :class:`~scim2_models.ListResponse` pydantic discriminator issue introduced with pydantic 2.9.0. #75
- Extension payloads are not required on response contexts. #77

[0.2.1] - 2024-09-06
--------------------
Expand Down
5 changes: 3 additions & 2 deletions scim2_models/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def validate_attribute_urn(
return f"{schema}:{attribute_base}"


def contains_attribute_or_subattributes(attribute_urns: List[str], attribute_urn):
def contains_attribute_or_subattributes(attribute_urns: List[str], attribute_urn: str):
return attribute_urn in attribute_urns or any(
item.startswith(f"{attribute_urn}.") for item in attribute_urns
item.startswith(f"{attribute_urn}.") or item.startswith(f"{attribute_urn}:")
for item in attribute_urns
)
5 changes: 4 additions & 1 deletion scim2_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,10 @@ def get_attribute_urn(self, field_name: str) -> str:
"""
main_schema = self.model_fields["schemas"].default[0]
alias = self.model_fields[field_name].serialization_alias or field_name
return f"{main_schema}:{alias}"

# if alias contains a ':' this is an extension urn
full_urn = alias if ":" in alias else f"{main_schema}:{alias}"
return full_urn


class ComplexAttribute(BaseModel):
Expand Down
1 change: 0 additions & 1 deletion scim2_models/rfc7643/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def __new__(cls, name, bases, attrs, **kwargs):
schema = extension.model_fields["schemas"].default[0]
attrs.setdefault("__annotations__", {})[extension.__name__] = Annotated[
Optional[extension],
Returned.always,
WrapSerializer(extension_serializer),
]
attrs[extension.__name__] = Field(
Expand Down
24 changes: 24 additions & 0 deletions tests/test_resource_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,27 @@ def test_extensions_schemas():
],
"userName": "foobar",
}


def test_validate_items_without_extension():
"""A model with an optional extension should be able to validate a payload
without an extension payload.
https://github.com/yaal-coop/scim2-models/issues/77
"""

payload = {
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
"id": "new-user",
"userName": "[email protected]",
"meta": {
"resourceType": "User",
"created": "2010-01-23T04:56:22Z",
"lastModified": "2011-05-13T04:42:34Z",
"version": 'W\\/"3694e05e9dff590"',
"location": "http://localhost:46459/Users/new-user",
},
}
User[EnterpriseUser].model_validate(
payload, scim_ctx=Context.RESOURCE_CREATION_RESPONSE
)

0 comments on commit 338ea04

Please sign in to comment.