|
1 | 1 | """Base SCIM object classes with schema identification.""" |
2 | 2 |
|
3 | 3 | from typing import Annotated |
| 4 | +from typing import Any |
| 5 | +from typing import Optional |
4 | 6 |
|
5 | 7 | from .annotations import Required |
6 | 8 | from .base import BaseModel |
| 9 | +from .base import validate_attribute_urn |
| 10 | +from .context import Context |
7 | 11 |
|
8 | 12 |
|
9 | | -class SchemaObject(BaseModel): |
| 13 | +class ScimObject(BaseModel): |
10 | 14 | schemas: Annotated[list[str], Required.true] |
11 | 15 | """The "schemas" attribute is a REQUIRED attribute and is an array of |
12 | 16 | Strings containing URIs that are used to indicate the namespaces of the |
13 | 17 | SCIM schemas that define the attributes present in the current JSON |
14 | 18 | structure.""" |
15 | 19 |
|
| 20 | + def _prepare_model_dump( |
| 21 | + self, |
| 22 | + scim_ctx: Optional[Context] = Context.DEFAULT, |
| 23 | + attributes: Optional[list[str]] = None, |
| 24 | + excluded_attributes: Optional[list[str]] = None, |
| 25 | + **kwargs: Any, |
| 26 | + ) -> dict[str, Any]: |
| 27 | + kwargs = super()._prepare_model_dump(scim_ctx, **kwargs) |
| 28 | + kwargs["context"]["scim_attributes"] = [ |
| 29 | + validate_attribute_urn(attribute, self.__class__) |
| 30 | + for attribute in (attributes or []) |
| 31 | + ] |
| 32 | + kwargs["context"]["scim_excluded_attributes"] = [ |
| 33 | + validate_attribute_urn(attribute, self.__class__) |
| 34 | + for attribute in (excluded_attributes or []) |
| 35 | + ] |
| 36 | + return kwargs |
16 | 37 |
|
17 | | -class ScimObject(SchemaObject): ... |
| 38 | + def model_dump( |
| 39 | + self, |
| 40 | + *args: Any, |
| 41 | + scim_ctx: Optional[Context] = Context.DEFAULT, |
| 42 | + attributes: Optional[list[str]] = None, |
| 43 | + excluded_attributes: Optional[list[str]] = None, |
| 44 | + **kwargs: Any, |
| 45 | + ) -> dict: |
| 46 | + """Create a model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump`. |
| 47 | +
|
| 48 | + :param scim_ctx: If a SCIM context is passed, some default values of |
| 49 | + Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM |
| 50 | + messages. Pass :data:`None` to get the default Pydantic behavior. |
| 51 | + :param attributes: A multi-valued list of strings indicating the names of resource |
| 52 | + attributes to return in the response, overriding the set of attributes that |
| 53 | + would be returned by default. |
| 54 | + :param excluded_attributes: A multi-valued list of strings indicating the names of resource |
| 55 | + attributes to be removed from the default set of attributes to return. |
| 56 | + """ |
| 57 | + dump_kwargs = self._prepare_model_dump( |
| 58 | + scim_ctx, attributes, excluded_attributes, **kwargs |
| 59 | + ) |
| 60 | + if scim_ctx: |
| 61 | + dump_kwargs.setdefault("mode", "json") |
| 62 | + return super(BaseModel, self).model_dump(*args, **dump_kwargs) |
| 63 | + |
| 64 | + def model_dump_json( |
| 65 | + self, |
| 66 | + *args: Any, |
| 67 | + scim_ctx: Optional[Context] = Context.DEFAULT, |
| 68 | + attributes: Optional[list[str]] = None, |
| 69 | + excluded_attributes: Optional[list[str]] = None, |
| 70 | + **kwargs: Any, |
| 71 | + ) -> str: |
| 72 | + """Create a JSON model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump_json`. |
| 73 | +
|
| 74 | + :param scim_ctx: If a SCIM context is passed, some default values of |
| 75 | + Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM |
| 76 | + messages. Pass :data:`None` to get the default Pydantic behavior. |
| 77 | + :param attributes: A multi-valued list of strings indicating the names of resource |
| 78 | + attributes to return in the response, overriding the set of attributes that |
| 79 | + would be returned by default. |
| 80 | + :param excluded_attributes: A multi-valued list of strings indicating the names of resource |
| 81 | + attributes to be removed from the default set of attributes to return. |
| 82 | + """ |
| 83 | + dump_kwargs = self._prepare_model_dump( |
| 84 | + scim_ctx, attributes, excluded_attributes, **kwargs |
| 85 | + ) |
| 86 | + return super(BaseModel, self).model_dump_json(*args, **dump_kwargs) |
0 commit comments