python-scim / scim2-models

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

Got error when calling `.model_dump()` with multiple user extensions #57

Closed Meow-Knight closed 4 months ago

Meow-Knight commented 4 months ago

I'm new with your lib and want to use it into my SCIM project. In my project, I have user schema with this structure:

After assigning data to the SCIM user instance, I wanted to get the data in dictionary format by calling .model_dump(), but it raised an exception. I don’t know whether it’s my mistake or a real error, so I’m posting it here to get help from you.


Here is my detailed python script:

from scim2_models import Schema, EnterpriseUser, User
from typing import Union

custom_user_structure = {
    "id": "urn:ietf:params:scim:schemas:extension:custom:2.0:User",
    "name": "BaseCustomUser",
    "description": "Custom User Account",
    "attributes": [
        {
            "name": "driversLicense",
            "type": "string",
            "multiValued": False,
            "description": "Drivers License",
            "required": False,
            "caseExact": False,
            "mutability": "readWrite",
            "returned": "default",
            "uniqueness": "none"
        }
    ]
}
custom_user_schema = Schema.model_validate(custom_user_structure)
custom_user_schema_cls = custom_user_schema.make_model()
prototype = User[Union[EnterpriseUser, custom_user_schema_cls]]
data = {
    "id": "12345",
    "urn:ietf:params:scim:schemas:extension:custom:2.0:User": {
        "driversLicense": "ABCD12345"
    },
    "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": {
        "employeeNumber": "EID123456"
    }
}
user = prototype.model_validate(data)
user.model_dump()

And it throws an exception:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 38, in <module>
  File "/Users/my-project/venv/lib/python3.11/site-packages/scim2_models/base.py", line 674, in model_dump
    return super().model_dump(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/my-project/venv/lib/python3.11/site-packages/pydantic/main.py", line 347, in model_dump
    return self.__pydantic_serializer__.to_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Error calling function `set_extension_schemas`: AttributeError: model_fields

[!NOTE] Additional information may be can help: If I use only 1 extend schema, it will run correctly

prototype = User[custom_user_schema_cls]
user = prototype()
user.id = "12345"
user[custom_user_schema_cls] = custom_user_schema_cls(drivers_license="ABCD12345")
user.model_dump()

Result:

{
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:custom:2.0:User"
],
"id": "12345",
"urn:ietf:params:scim:schemas:extension:custom:2.0:User": {
"schemas": [
"urn:ietf:params:scim:schemas:extension:custom:2.0:User"
], # redundant field
"driversLicense": "ABCD12345"
}
}
azmeuk commented 4 months ago

Thank you for your detailed bug report. This is fixed in 0.1.14, including the redundant schemas field.