55from inspect import isclass
66from typing import TYPE_CHECKING
77from typing import Any
8- from typing import get_args
9- from typing import get_origin
108
9+ from pydantic import Base64Bytes
1110from scim2_models import ComplexAttribute
1211from scim2_models import Extension
13- from scim2_models import ExternalReference
1412from scim2_models import Mutability
1513from scim2_models import Reference
1614from scim2_models import Required
1715from scim2_models import Resource
18- from scim2_models import URIReference
1916from scim2_models .path import Path
20- from scim2_models .utils import UNION_TYPES
21- from scim2_models .utils import Base64Bytes
2217
2318if TYPE_CHECKING :
2419 from scim2_tester .utils import CheckContext
@@ -37,23 +32,13 @@ def get_random_example_value(path: Path[Any]) -> Any | None:
3732
3833
3934def get_model_from_ref_type (
40- context : "CheckContext" , ref_type : type , different_than : type [Resource [Any ]] | None
35+ context : "CheckContext" ,
36+ ref_type : type [Reference ],
37+ different_than : type [Resource [Any ]] | None ,
4138) -> type [Resource [Any ]]:
42- """Return "User" from "Union[Literal['User'], Literal['Group']]"."""
43-
44- def get_model_from_ref_type_ (ref_type : type ) -> Any :
45- if get_origin (ref_type ) in UNION_TYPES :
46- return [
47- get_model_from_ref_type_ (sub_ref_type )
48- for sub_ref_type in get_args (ref_type )
49- ]
50-
51- model_name = get_args (ref_type )[0 ]
52- model = context .client .get_resource_model (model_name )
53- return model
54-
55- models = get_model_from_ref_type_ (ref_type )
56- models = models if isinstance (models , list ) else [models ]
39+ """Return the Resource model referenced by a Reference type."""
40+ ref_names = ref_type .__reference_types__
41+ models = [context .client .get_resource_model (ref_name ) for ref_name in ref_names ]
5742 acceptable_models = [model for model in models if model != different_than ]
5843
5944 if not acceptable_models :
@@ -116,16 +101,17 @@ def generate_random_value(
116101 elif field_type is bool :
117102 value = random .choice ([True , False ])
118103
119- elif get_origin (field_type ) is Reference and get_args (field_type )[0 ] != Any :
120- ref_type = get_args (field_type )[0 ]
121- if ref_type not in (ExternalReference , URIReference ):
122- ref_model = get_model_from_ref_type (context , ref_type , different_than = model )
104+ elif isclass (field_type ) and issubclass (field_type , Reference ):
105+ ref_types = field_type .__reference_types__
106+ if not ref_types or "external" in ref_types or "uri" in ref_types :
107+ value = f"https://{ str (uuid .uuid4 ())} .test"
108+ else :
109+ ref_model = get_model_from_ref_type (
110+ context , field_type , different_than = model
111+ )
123112 ref_obj = context .resource_manager .create_and_register (ref_model )
124113 value = ref_obj .meta .location if ref_obj .meta else None
125114
126- else :
127- value = f"https://{ str (uuid .uuid4 ())} .test"
128-
129115 elif isclass (field_type ) and issubclass (field_type , Enum ):
130116 value = random .choice (list (field_type ))
131117
@@ -134,11 +120,6 @@ def generate_random_value(
134120 context , field_type (), mutability = mutability , required = required
135121 ) # type: ignore[arg-type]
136122
137- elif isclass (field_type ) and issubclass (field_type , Extension ):
138- value = fill_with_random_values (
139- context , field_type (), mutability = mutability , required = required
140- ) # type: ignore[arg-type]
141-
142123 elif field_type is None and isclass (model ) and issubclass (model , Extension ):
143124 value = fill_with_random_values (
144125 context , model (), mutability = mutability , required = required
0 commit comments