Skip to content

Commit f10e601

Browse files
author
Oleksandr Bazarnov
committed
updated to account type for the given duplicated key
1 parent 138b607 commit f10e601

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

airbyte_cdk/sources/declarative/parsers/manifest_deduplicator.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

166164
def _handle_duplicates(manifest: DefinitionsType, duplicates: DuplicatesType) -> None:
@@ -212,6 +210,7 @@ def _add_duplicate(
212210

213211
def _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

391399
def _create_schema_ref(ref_key: str) -> Dict[str, str]:

unit_tests/sources/declarative/parsers/test_manifest_reference_resolver.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def test_deduplicate_manifest_when_multiple_url_base_are_resolved_and_most_frequ
330330
}
331331
expected = {
332332
"type": "DeclarativeSource",
333-
"definitions": {"shared": {"url_base": "https://example.com/v2/"}},
333+
"definitions": {"shared": {"HttpRequester": {"url_base": "https://example.com/v2/"}}},
334334
"streams": [
335335
{
336336
"type": "DeclarativeStream",
@@ -361,7 +361,7 @@ def test_deduplicate_manifest_when_multiple_url_base_are_resolved_and_most_frequ
361361
"type": "SimpleRetriever",
362362
"requester": {
363363
"type": "HttpRequester",
364-
"url_base": {"$ref": "#/definitions/shared/url_base"},
364+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
365365
"path": "B",
366366
"http_method": "GET",
367367
},
@@ -405,7 +405,7 @@ def test_deduplicate_manifest_when_multiple_url_base_are_resolved_and_most_frequ
405405
"type": "SimpleRetriever",
406406
"requester": {
407407
"type": "HttpRequester",
408-
"url_base": {"$ref": "#/definitions/shared/url_base"},
408+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
409409
"path": "D",
410410
"http_method": "GET",
411411
},
@@ -427,7 +427,7 @@ def test_deduplicate_manifest_when_multiple_url_base_are_resolved_and_most_frequ
427427
"type": "SimpleRetriever",
428428
"requester": {
429429
"type": "HttpRequester",
430-
"url_base": {"$ref": "#/definitions/shared/url_base"},
430+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
431431
"path": "E",
432432
"http_method": "GET",
433433
},
@@ -484,7 +484,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
484484
content = {
485485
"type": "DeclarativeSource",
486486
"definitions": {
487-
"shared": {"url_base": "https://example.com/v2/"},
487+
"shared": {"HttpRequester": {"url_base": "https://example.com/v2/"}},
488488
"streams": {
489489
"A": {
490490
"type": "DeclarativeStream",
@@ -600,7 +600,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
600600
},
601601
"requester_B": {
602602
"type": "HttpRequester",
603-
"url_base": {"$ref": "#/definitions/shared/url_base"},
603+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
604604
},
605605
},
606606
"streams": [
@@ -645,7 +645,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
645645
}
646646
expected = {
647647
"type": "DeclarativeSource",
648-
"definitions": {"shared": {"url_base": "https://example.com/v2/"}},
648+
"definitions": {"shared": {"HttpRequester": {"url_base": "https://example.com/v2/"}}},
649649
"streams": [
650650
{
651651
"type": "DeclarativeStream",
@@ -676,7 +676,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
676676
"type": "SimpleRetriever",
677677
"requester": {
678678
"type": "HttpRequester",
679-
"url_base": {"$ref": "#/definitions/shared/url_base"},
679+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
680680
"path": "B",
681681
"http_method": "GET",
682682
},
@@ -720,7 +720,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
720720
"type": "SimpleRetriever",
721721
"requester": {
722722
"type": "HttpRequester",
723-
"url_base": {"$ref": "#/definitions/shared/url_base"},
723+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
724724
"path": "D",
725725
"http_method": "GET",
726726
},
@@ -742,7 +742,7 @@ def test_deduplicate_manifest_with_shared_definitions_url_base_are_present():
742742
"type": "SimpleRetriever",
743743
"requester": {
744744
"type": "HttpRequester",
745-
"url_base": {"$ref": "#/definitions/shared/url_base"},
745+
"url_base": {"$ref": "#/definitions/shared/HttpRequester/url_base"},
746746
"path": "E",
747747
"http_method": "GET",
748748
},

0 commit comments

Comments
 (0)