@@ -35,10 +35,46 @@ def object_replacement(
3535 """
3636 test_obj = context .resource_manager .create_and_register (model )
3737
38+ # Dirty hotfix, waiting to path management in scim2-models
39+ # to have a more generic way to do things
40+ # https://github.com/python-scim/scim2-models/issues/111
41+ # Store original immutable values to preserve them
42+ original_immutable_values = {}
43+ for field_name , field_info in model .model_fields .items (): # pragma: no cover
44+ # Check if field is immutable
45+ is_immutable = any (
46+ isinstance (metadata , Mutability ) and metadata == Mutability .immutable
47+ for metadata in field_info .metadata
48+ )
49+ if is_immutable :
50+ original_value = getattr (test_obj , field_name , None )
51+ if original_value is not None :
52+ original_immutable_values [field_name ] = original_value
53+
3854 modified_obj = fill_with_random_values (
39- context , test_obj , mutability = [Mutability .read_write , Mutability .write_only ]
55+ context ,
56+ test_obj ,
57+ mutability = [Mutability .read_write , Mutability .write_only , Mutability .immutable ],
4058 )
4159
60+ # Dirty hotfix, waiting to path management in scim2-models
61+ # to have a more generic way to do things
62+ # https://github.com/python-scim/scim2-models/issues/111
63+ # Restore original immutable values for non-complex fields
64+ if modified_obj is not None :
65+ for (
66+ field_name ,
67+ original_value ,
68+ ) in original_immutable_values .items (): # pragma: no cover
69+ # Only restore if it's not a complex list (like members)
70+ if not (
71+ isinstance (original_value , list )
72+ and original_value
73+ and hasattr (original_value [0 ], "__class__" )
74+ and hasattr (original_value [0 ].__class__ , "model_fields" )
75+ ):
76+ setattr (modified_obj , field_name , original_value )
77+
4278 if modified_obj is None :
4379 raise ValueError (
4480 f"Could not modify { model .__name__ } object with mutable fields"
0 commit comments