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

Required extensions #77

Open azmeuk opened 2 months ago

azmeuk commented 2 months ago

Due to the following code, extension payload is required in resource creation payloads: https://github.com/yaal-coop/scim2-models/blob/972bd8f01c157fc5720d90461e60d5da26fb7748/scim2_models/rfc7643/resource.py#L139

>>> from scim2_models import User, EnterpriseUser, Context
>>> payload = {
...     "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
...     "id": "new-user",
...     "userName": "new-user@example.com",
...     "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
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/eloi/dev/yaal/scim2-models/scim2_models/base.py", line 661, in model_validate
    return super().model_validate(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/eloi/.cache/pypoetry/virtualenvs/scim2-models-TH2HRh4a-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 595, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for User[EnterpriseUser]
  Field 'EnterpriseUser' has returnability 'always' but value is missing or null [type=returned_error, input_value={'schemas': ['urn:ietf:pa...:46459/Users/new-user'}}, input_type=dict]

However it feels like in some cases this code should be OK.

RFC7643 §6 indicates that sometimes extensions are required, and sometimes they are optional:

 required  A Boolean value that specifies whether or not the schema
    extension is required for the resource type.  If true, a
    resource of this type MUST include this schema extension and
    also include any attributes declared as required in this schema
    extension.  If false, a resource of this type MAY omit this
    schema extension.  REQUIRED.

scim2-models should offer a way to mark extensions to be required or optional. In the meantime, a good default could be to consider extensions as optional, so the previous code would work.