|
11 | 11 | from scim2_models import ComplexAttribute |
12 | 12 | from scim2_models import Extension |
13 | 13 | from scim2_models import ExternalReference |
| 14 | +from scim2_models import MultiValuedComplexAttribute |
14 | 15 | from scim2_models import Mutability |
15 | 16 | from scim2_models import Reference |
16 | 17 | from scim2_models import Resource |
@@ -123,6 +124,9 @@ def generate_random_value( |
123 | 124 | elif isclass(field_type) and issubclass(field_type, Enum): |
124 | 125 | value = random.choice(list(field_type)) |
125 | 126 |
|
| 127 | + elif isclass(field_type) and issubclass(field_type, MultiValuedComplexAttribute): |
| 128 | + value = fill_mvca_with_random_values(context, field_type()) # type: ignore[arg-type] |
| 129 | + |
126 | 130 | elif isclass(field_type) and issubclass(field_type, ComplexAttribute): |
127 | 131 | value = fill_with_random_values(context, field_type()) # type: ignore[arg-type] |
128 | 132 |
|
@@ -172,3 +176,24 @@ def fill_with_random_values( |
172 | 176 | set_value_by_urn(obj, urn, value) |
173 | 177 |
|
174 | 178 | return obj |
| 179 | + |
| 180 | + |
| 181 | +def fill_mvca_with_random_values( |
| 182 | + context: "CheckContext", |
| 183 | + obj: MultiValuedComplexAttribute, |
| 184 | +) -> Resource[Any] | None: |
| 185 | + """Fill a MultiValuedComplexAttribute with random values. |
| 186 | +
|
| 187 | + For SCIM reference fields, correctly sets the value field to match |
| 188 | + the ID extracted from the reference URL. |
| 189 | + """ |
| 190 | + fill_with_random_values(context, obj) |
| 191 | + ref_type = type(obj).get_field_root_type("ref") |
| 192 | + if ( |
| 193 | + get_origin(ref_type) is Reference |
| 194 | + and get_args(ref_type) |
| 195 | + and get_args(ref_type)[0] not in (URIReference, ExternalReference, Any) |
| 196 | + and (ref := getattr(obj, "ref", None)) |
| 197 | + ): |
| 198 | + obj.value = ref.rsplit("/", 1)[-1] |
| 199 | + return obj |
0 commit comments