@@ -49,10 +49,8 @@ def deduplicate_minifest(resolved_manifest: ManifestType) -> ManifestType:
4949
5050 # prepare the `definitions` tag
5151 _prepare_definitions (_manifest )
52- # collect duplicates for a given manifest
53- duplicates = _collect_duplicates (_manifest )
5452 # replace duplicates with references, if any
55- _handle_duplicates (_manifest , duplicates )
53+ _handle_duplicates (_manifest , _collect_duplicates ( _manifest ) )
5654 # post processing the manifest
5755 _reference_schemas (_manifest )
5856
@@ -147,20 +145,20 @@ def _replace_duplicates_with_refs(manifest: ManifestType, duplicates: Duplicates
147145 """
148146 for _ , occurrences in duplicates .items ():
149147 # take the component's name as the last part of it's path
150- key , value = _get_key_value_from_occurances (occurrences )
151- is_shared_def = _is_shared_definition (manifest , key )
148+ type_key , key , value = _get_key_value_from_occurances (occurrences )
149+ is_shared_def = _is_shared_definition (manifest , type_key , key )
152150
153151 # Add to definitions if not there already
154152 if not is_shared_def :
155- _add_to_shared_definitions (manifest , key , value )
153+ _add_to_shared_definitions (manifest , type_key , key , value )
156154
157155 # Replace occurrences with references
158156 for path , parent_obj , value in occurrences :
159157 if is_shared_def :
160- if value == _get_shared_definition_value (manifest , key ):
161- parent_obj [key ] = _create_shared_definition_ref (key )
158+ if value == _get_shared_definition_value (manifest , type_key , key ):
159+ parent_obj [key ] = _create_shared_definition_ref (type_key , key )
162160 else :
163- parent_obj [key ] = _create_shared_definition_ref (key )
161+ parent_obj [key ] = _create_shared_definition_ref (type_key , key )
164162
165163
166164def _handle_duplicates (manifest : DefinitionsType , duplicates : DuplicatesType ) -> None :
@@ -212,6 +210,7 @@ def _add_duplicate(
212210
213211def _add_to_shared_definitions (
214212 manifest : DefinitionsType ,
213+ type_key : str ,
215214 key : str ,
216215 value : Any ,
217216) -> DefinitionsType :
@@ -223,9 +222,11 @@ def _add_to_shared_definitions(
223222 key: The key to use
224223 value: The value to add
225224 """
225+ if type_key not in manifest [DEF_TAG ][SHARED_TAG ].keys ():
226+ manifest [DEF_TAG ][SHARED_TAG ][type_key ] = {}
226227
227- if key not in manifest [DEF_TAG ][SHARED_TAG ]:
228- manifest [DEF_TAG ][SHARED_TAG ][key ] = value
228+ if key not in manifest [DEF_TAG ][SHARED_TAG ][ type_key ]. keys () :
229+ manifest [DEF_TAG ][SHARED_TAG ][type_key ][ key ] = value
229230
230231 return manifest
231232
@@ -327,7 +328,7 @@ def _hash_object(node: Dict[str, Any]) -> Optional[str]:
327328 return None
328329
329330
330- def _is_shared_definition (manifest : DefinitionsType , key : str ) -> bool :
331+ def _is_shared_definition (manifest : DefinitionsType , type_key : str , key : str ) -> bool :
331332 """
332333 Check if the key already exists in the shared definitions.
333334
@@ -338,10 +339,16 @@ def _is_shared_definition(manifest: DefinitionsType, key: str) -> bool:
338339 Returns:
339340 True if the key exists in the shared definitions, False otherwise
340341 """
341- return key in manifest [DEF_TAG ][SHARED_TAG ]
342342
343+ if type_key in manifest [DEF_TAG ][SHARED_TAG ].keys ():
344+ # Check if the key exists in the shared definitions
345+ if key in manifest [DEF_TAG ][SHARED_TAG ][type_key ].keys ():
346+ return True
343347
344- def _get_shared_definition_value (manifest : DefinitionsType , key : str ) -> Any :
348+ return False
349+
350+
351+ def _get_shared_definition_value (manifest : DefinitionsType , type_key : str , key : str ) -> Any :
345352 """
346353 Get the value of a shared definition by its key.
347354
@@ -351,31 +358,32 @@ def _get_shared_definition_value(manifest: DefinitionsType, key: str) -> Any:
351358 Returns:
352359 The value of the shared definition
353360 """
354- if key in manifest [DEF_TAG ][SHARED_TAG ]:
355- return manifest [DEF_TAG ][SHARED_TAG ][key ]
361+ if type_key in manifest [DEF_TAG ][SHARED_TAG ].keys ():
362+ if key in manifest [DEF_TAG ][SHARED_TAG ][type_key ].keys ():
363+ return manifest [DEF_TAG ][SHARED_TAG ][type_key ][key ]
356364 else :
357365 raise ManifestDeduplicationException (
358366 f"Key { key } not found in shared definitions. Please check the manifest."
359367 )
360368
361369
362- def _get_key_value_from_occurances (occurrences : DuplicateOccurancesType ) -> Tuple [str , Any ]:
370+ def _get_key_value_from_occurances (occurrences : DuplicateOccurancesType ) -> Tuple [str , str , Any ]:
363371 """
364372 Get the key from the occurrences list.
365373
366374 Args:
367375 occurrences: The occurrences list
368376
369377 Returns:
370- The key and value from the occurrences
378+ The key, type and value from the occurrences
371379 """
372380
373381 # Take the value from the first occurrence, as they are the same
374- path , _ , value = occurrences [0 ]
375- return path [- 1 ], value # Return the component's name as the last part of its path
382+ path , obj , value = occurrences [0 ]
383+ return obj [ "type" ], path [- 1 ], value # Return the component's name as the last part of its path
376384
377385
378- def _create_shared_definition_ref (ref_key : str ) -> Dict [str , str ]:
386+ def _create_shared_definition_ref (type_key : str , key : str ) -> Dict [str , str ]:
379387 """
380388 Create a reference object for the shared definitions using the specified key.
381389
@@ -385,7 +393,7 @@ def _create_shared_definition_ref(ref_key: str) -> Dict[str, str]:
385393 Returns:
386394 A reference object in the proper format
387395 """
388- return {"$ref" : f"#/{ DEF_TAG } /{ SHARED_TAG } /{ ref_key } " }
396+ return {"$ref" : f"#/{ DEF_TAG } /{ SHARED_TAG } /{ type_key } / { key } " }
389397
390398
391399def _create_schema_ref (ref_key : str ) -> Dict [str , str ]:
0 commit comments