Skip to content

Commit 534b5dd

Browse files
committed
fix: required sub-attribute creation
1 parent 7ee86a5 commit 534b5dd

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

scim2_tester/filling.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from scim2_models import ExternalReference
1414
from scim2_models import Mutability
1515
from scim2_models import Reference
16+
from scim2_models import Required
1617
from scim2_models import Resource
1718
from scim2_models import URIReference
1819
from scim2_models.utils import UNION_TYPES
@@ -208,6 +209,15 @@ def fill_complex_attribute_with_random_values(
208209
For SCIM reference fields, correctly sets the value field to match
209210
the ID extracted from the reference URL.
210211
"""
212+
if not urns:
213+
urns = []
214+
for field_name in type(obj).model_fields:
215+
if type(obj).get_field_annotation(field_name, Required) == Required.true:
216+
alias = (
217+
type(obj).model_fields[field_name].serialization_alias or field_name
218+
)
219+
urns.append(alias)
220+
211221
fill_with_random_values(context, obj, urns)
212222
if "ref" in type(obj).model_fields and "value" in type(obj).model_fields:
213223
ref_type = type(obj).get_field_root_type("ref")

tests/test_filling.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from scim2_models import Group
99
from scim2_models import Mutability
1010
from scim2_models import PhoneNumber
11+
from scim2_models import Required
1112
from scim2_models import User
1213
from scim2_models.attributes import ComplexAttribute
1314
from scim2_models.resources.resource import Resource
@@ -265,3 +266,23 @@ class TestResource(Resource):
265266
assert filled_resource.test_attr.readonly_field is None, (
266267
f"readonly_field should be None but got {filled_resource.test_attr.readonly_field}"
267268
)
269+
270+
271+
def test_resource_manager_complex_attribute_required_subfields(testing_context):
272+
"""Test that ResourceManager doesn't collect URNs for required sub-attributes of complex fields."""
273+
274+
class TestName(ComplexAttribute):
275+
formatted: Annotated[str | None, Required.true] = None
276+
given_name: str | None = None
277+
278+
class TestUser(Resource):
279+
schemas: Annotated[list[str], Required.true] = ["urn:test:TestUser"]
280+
name: Annotated[TestName | None, Required.true] = None
281+
282+
obj = TestUser()
283+
urns = ["urn:test:TestUser:name"]
284+
filled = fill_with_random_values(testing_context, obj, urns)
285+
286+
assert filled.name is not None
287+
assert filled.name.formatted is not None
288+
assert filled.name.given_name is None

0 commit comments

Comments
 (0)