Skip to content

Commit c016cc0

Browse files
bltravisbtravisebsco
authored andcommitted
Fix path normalization for array object nested objects
1 parent 0a52c12 commit c016cc0

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/folio_migration_tools/mapping_file_transformation/mapping_file_mapper_base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ def map_object_props(
561561
index_or_id,
562562
level: int,
563563
):
564+
def _normalize_local_object_path(path: str) -> str:
565+
# When mapping nested object fields under an array item (for example,
566+
# "poLines[0].cost.currency") into a temporary item object, keep only
567+
# the local path segment ("cost.currency").
568+
return path.split("].", 1)[1] if "]." in path else path
569+
564570
temp_object: dict = {}
565571
for child_property_name, child_property in schema_property["properties"].items():
566572
sub_prop_path = f"{schema_property_name}.{child_property_name}"
@@ -607,12 +613,12 @@ def map_object_props(
607613
if p := self.get_prop(
608614
legacy_object, sub_prop_path, index_or_id, child_property.get("default", "")
609615
):
610-
set_at_path(folio_object, sub_prop_path, p)
616+
set_at_path(folio_object, _normalize_local_object_path(sub_prop_path), p)
611617
# temp_object[child_property_name] = p
612618
elif p := self.get_prop(
613619
legacy_object, sub_prop_path, index_or_id, child_property.get("default", "")
614620
):
615-
set_at_path(folio_object, sub_prop_path, p)
621+
set_at_path(folio_object, _normalize_local_object_path(sub_prop_path), p)
616622
if temp_object:
617623
set_deep(folio_object, schema_property_name, temp_object)
618624
# folio_object[schema_property_name] = temp_object

tests/test_mapping_file_mapper_base.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6343,3 +6343,62 @@ def test_field_mapping_errors_report_is_json_serializable(mocked_file_mapper):
63436343
assert "FieldMappingErrors" in payload
63446344
assert isinstance(payload["FieldMappingErrors"], dict)
63456345
assert payload["FieldMappingErrors"]["Required properties missing in poLines item\torderFormat,source"] == 1
6346+
6347+
6348+
def test_nested_object_fields_under_array_item_are_local_to_item(
6349+
mocked_folio_client, mocked_file_mapper
6350+
):
6351+
schema = {
6352+
"$schema": "http://json-schema.org/draft-04/schema#",
6353+
"type": "object",
6354+
"properties": {
6355+
"id": {"type": "string"},
6356+
"lines": {
6357+
"type": "array",
6358+
"items": {
6359+
"type": "object",
6360+
"properties": {
6361+
"title": {"type": "string"},
6362+
"source": {"type": "string"},
6363+
"cost": {
6364+
"type": "object",
6365+
"properties": {
6366+
"currency": {"type": "string"},
6367+
"listUnitPrice": {"type": "number"},
6368+
},
6369+
},
6370+
},
6371+
"required": ["title", "source"],
6372+
},
6373+
},
6374+
},
6375+
"required": [],
6376+
}
6377+
record = {
6378+
"id": "rec-1",
6379+
"line_title": "Example title",
6380+
"line_source": "API",
6381+
}
6382+
the_map = {
6383+
"data": [
6384+
{"folio_field": "legacyIdentifier", "legacy_field": "id", "value": "", "description": ""},
6385+
{"folio_field": "lines[0].title", "legacy_field": "line_title", "value": "", "description": ""},
6386+
{"folio_field": "lines[0].source", "legacy_field": "line_source", "value": "", "description": ""},
6387+
{"folio_field": "lines[0].cost.currency", "legacy_field": "", "value": "USD", "description": ""},
6388+
{"folio_field": "lines[0].cost.listUnitPrice", "legacy_field": "", "value": 10.5, "description": ""},
6389+
]
6390+
}
6391+
mapper = MappingFileMapperBase(
6392+
mocked_folio_client,
6393+
schema,
6394+
the_map,
6395+
None,
6396+
FOLIONamespaces.orders,
6397+
mocked_classes.get_mocked_library_config(),
6398+
mocked_file_mapper.task_configuration,
6399+
)
6400+
6401+
folio_rec, _ = mapper.do_map(record, record["id"], FOLIONamespaces.orders)
6402+
6403+
assert folio_rec["lines"][0]["cost"]["currency"] == "USD"
6404+
assert folio_rec["lines"][0]["cost"]["listUnitPrice"] == 10.5

0 commit comments

Comments
 (0)