yaal-coop / scim2-models

SCIM resources serialization and validation with Pydantic
https://scim2-models.readthedocs.io
Apache License 2.0
6 stars 2 forks source link

Is it intended to call `mark_with_schema()` after setting attributes in a Resource? #48

Closed ccoors closed 1 week ago

ccoors commented 2 weeks ago

Hi,

I have found that modifying a Resource can lead to serialization issues when creating complex attributes:

user = User(id="1", user_name="ABC")
user.meta = Meta(
    resource_type="User",
    location="/v2/Users/foo",
)
user.model_dump(scim_ctx=Context.RESOURCE_CREATION_RESPONSE)

This fails with

[...] Error calling function scim_serializer: AttributeError: 'Meta' object has no attribute '_schema'

If I call

user.mark_with_schema()

before the model_dump call, serialization works. Is this intentional? If I leave out the scim_ctx, it works correctly without calling user.mark_with_schema().

azmeuk commented 1 week ago

mark_with_schema is a simple way to give a URN to sub-attributes, so they can be included or excluded with the model_dump attributes or excluded_attributes parameters. Attributes are marked at validation time, but as pydantic validate_assignment is not enabled, user.meta = Meta(...) does not validate the model and mark_with_schema is not called. I am not satisfied with how this is implemented now (hence #42) and I think validate_assignment could be enabled by default, but it breaks the tests at the moment.

If I leave out the scim_ctx, it works correctly without calling user.mark_with_schema().

Yes, I guess most of the unit test does not use the scim_ctx attribute, because most of them were written before the Context implementation, so in the end they may not really test real world use cases.