Skip to content

Commit e8b61b2

Browse files
authored
Merge create and update model to get read only properties (#715)
* Merge create and update model to get read only properties
1 parent b47d86d commit e8b61b2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/rpdk/core/contract/resource_client.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ def prune_properties_if_not_exist_in_path(output_model, input_model, paths):
6969
return output_document["properties"]
7070

7171

72+
def prune_properties_which_dont_exist_in_path(model, paths):
73+
"""Prunes model to properties present in path. This method removes any property
74+
from the model which does not exists in the paths
75+
76+
This assumes properties will always have an object (dict) as a parent.
77+
The function returns the model after pruning all but the path which exists
78+
in the paths tuple from the input_model
79+
"""
80+
document = {"properties": model.copy()}
81+
for model_path in model.keys():
82+
path_tuple = ("properties", model_path)
83+
if path_tuple not in paths:
84+
_prop, resolved_path, parent = traverse(document, path_tuple)
85+
key = resolved_path[-1]
86+
del parent[key]
87+
return document["properties"]
88+
89+
7290
def path_exists(document, path):
7391
try:
7492
_prop, _resolved_path, _parent = traverse(document, path)
@@ -426,7 +444,12 @@ def generate_update_example(self, create_model):
426444
if self.create_only_paths:
427445
self.validate_update_example_keys(unique_identifiers, update_example)
428446
update_example.update(unique_identifiers)
429-
return update_example
447+
create_model_with_read_only_properties = (
448+
prune_properties_which_dont_exist_in_path(
449+
create_model, self.read_only_paths
450+
)
451+
)
452+
return {**create_model_with_read_only_properties, **update_example}
430453
overrides = self._overrides.get("UPDATE", self._overrides.get("CREATE", {}))
431454
example = override_properties(self.update_strategy.example(), overrides)
432455
return {**create_model, **example}

tests/contract/test_resource_client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
prune_properties,
1616
prune_properties_from_model,
1717
prune_properties_if_not_exist_in_path,
18+
prune_properties_which_dont_exist_in_path,
1819
)
1920
from rpdk.core.exceptions import InvalidProjectError
2021
from rpdk.core.test import (
@@ -329,6 +330,21 @@ def test_prune_properties_if_not_exist_in_path():
329330
assert model == previous_model
330331

331332

333+
def test_prune_properties_which_dont_exist_in_path():
334+
model = {
335+
"spam": "eggs",
336+
"one": "two",
337+
"array": ["first", "second"],
338+
}
339+
model1 = prune_properties_which_dont_exist_in_path(
340+
model,
341+
[
342+
("properties", "one"),
343+
],
344+
)
345+
assert model1 == {"one": "two"}
346+
347+
332348
def test_init_sam_cli_client():
333349
patch_sesh = patch(
334350
"rpdk.core.contract.resource_client.create_sdk_session", autospec=True
@@ -1210,10 +1226,12 @@ def test_generate_invalid_update_example_with_inputs(resource_client_inputs):
12101226

12111227
def test_generate_update_example_with_primary_identifier(resource_client_inputs_schema):
12121228
created_resource = resource_client_inputs_schema.generate_create_example()
1229+
# adding read only property to denote a realistic scenario
1230+
created_resource["b"] = 2
12131231
updated_resource = resource_client_inputs_schema.generate_update_example(
12141232
created_resource
12151233
)
1216-
assert updated_resource == {"a": 1, "c": 2}
1234+
assert updated_resource == {"a": 1, "c": 2, "b": 2}
12171235

12181236

12191237
def test_generate_update_example_with_composite_key(

0 commit comments

Comments
 (0)