Skip to content
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

extension payloads are not required on response contexts #78

Merged
merged 1 commit into from
Sep 20, 2024
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
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
)
Loading