@@ -78,6 +78,13 @@ class Meta(ComplexAttribute):
7878
7979
8080class Extension (BaseModel ):
81+ def __init_subclass__ (cls , ** kwargs ):
82+ super ().__init_subclass__ (** kwargs )
83+ if not hasattr (cls , "scim_schema" ):
84+ raise AttributeError (
85+ f"{ cls .__name__ } did not define a scim_schema attribute"
86+ )
87+
8188 @classmethod
8289 def to_schema (cls ):
8390 """Build a :class:`~scim2_models.Schema` from the current extension class."""
@@ -120,7 +127,7 @@ def __new__(cls, name, bases, attrs, **kwargs):
120127 else [extensions ]
121128 )
122129 for extension in extensions :
123- schema = extension .model_fields [ "schemas" ]. default [ 0 ]
130+ schema = extension .scim_schema
124131 attrs .setdefault ("__annotations__" , {})[extension .__name__ ] = Annotated [
125132 Optional [extension ],
126133 WrapSerializer (extension_serializer ),
@@ -136,6 +143,18 @@ def __new__(cls, name, bases, attrs, **kwargs):
136143
137144
138145class Resource (BaseModel , Generic [AnyExtension ], metaclass = ResourceMetaclass ):
146+ def __init_subclass__ (cls , ** kwargs ):
147+ super ().__init_subclass__ (** kwargs )
148+ if not hasattr (cls , "scim_schema" ):
149+ raise AttributeError (
150+ f"{ cls .__name__ } did not define a scim_schema attribute"
151+ )
152+
153+ def init_schemas ():
154+ return [cls .scim_schema ]
155+
156+ cls .model_fields ["schemas" ].default_factory = init_schemas
157+
139158 schemas : list [str ]
140159 """The "schemas" attribute is a REQUIRED attribute and is an array of
141160 Strings containing URIs that are used to indicate the namespaces of the
@@ -186,9 +205,7 @@ def get_extension_models(cls) -> dict[str, type]:
186205 else extension_models
187206 )
188207
189- by_schema = {
190- ext .model_fields ["schemas" ].default [0 ]: ext for ext in extension_models
191- }
208+ by_schema = {ext .scim_schema : ext for ext in extension_models }
192209 return by_schema
193210
194211 @staticmethod
@@ -197,7 +214,7 @@ def get_by_schema(
197214 ) -> Optional [type ]:
198215 """Given a resource type list and a schema, find the matching resource type."""
199216 by_schema = {
200- resource_type .model_fields [ "schemas" ]. default [ 0 ] .lower (): resource_type
217+ resource_type .scim_schema .lower (): resource_type
201218 for resource_type in (resource_types or [])
202219 }
203220 if with_extensions :
@@ -274,7 +291,7 @@ def compare_field_infos(fi1, fi2):
274291def model_to_schema (model : type [BaseModel ]):
275292 from scim2_models .rfc7643 .schema import Schema
276293
277- schema_urn = model .model_fields [ "schemas" ]. default [ 0 ]
294+ schema_urn = model .scim_schema
278295 field_infos = dedicated_attributes (model )
279296 attributes = [
280297 model_attribute_to_attribute (model , attribute_name )
0 commit comments