Skip to content

Commit 7d4faa6

Browse files
authored
Merge pull request #104 from dlt-hub/feat/better-detection-fallbacks
set paginator to auto if not detected, do not set json_path if not detected
2 parents 27ff264 + a748b1b commit 7d4faa6

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

dlt_init_openapi/parser/endpoints.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,18 @@ def render_pagination_args(self) -> Optional[Dict[str, Union[str, int]]]:
9797
return None
9898
return self.detected_pagination.paginator_config if self.detected_pagination else None
9999

100+
@property
101+
def render_auto_paginator(self) -> bool:
102+
"""if we could not figure out the paginator, set it to auto"""
103+
# we do not set auto if there is a global paginator, this is a bug in the underlying layer, we have to fix this
104+
# once rest_api is fixed
105+
if self.detected_global_pagination:
106+
return False
107+
return not (self.detected_pagination)
108+
100109
@property
101110
def data_json_path(self) -> str:
102-
return self.payload.json_path if self.payload else "$"
111+
return self.payload.json_path if self.payload else None
103112

104113
@property
105114
def transformer(self) -> Optional[TransformerSetting]:

dlt_init_openapi/renderer/default/templates/source.py.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def {{ source_name }}(
5151
{% endif %}
5252
{% endif %}
5353
"endpoint": {
54+
{% if endpoint.data_json_path %}
5455
"data_selector": "{{ endpoint.data_json_path }}",
56+
{% endif %}
5557
"path": "{{endpoint.path }}",
5658
{% if endpoint.transformer or endpoint.unresolvable_path_param_names or endpoint.unresolvable_query_param_names %}
5759
"params": {
@@ -84,6 +86,9 @@ def {{ source_name }}(
8486
{% endfor %}
8587
},
8688
{% endif %}
89+
{% if endpoint.render_auto_paginator %}
90+
"paginator": "auto",
91+
{% endif %}
8792
}
8893
},
8994
{% endfor %}

tests/integration/basics/test_transformer.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ def test_simple_transformer(resources: Dict[str, Any]) -> None:
1717
"table_name": "collection",
1818
"primary_key": "id",
1919
"write_disposition": "merge",
20-
"endpoint": {"data_selector": "$", "path": "/collection/"},
20+
"endpoint": {
21+
"data_selector": "$",
22+
"path": "/collection/",
23+
"paginator": "auto",
24+
},
2125
}
2226
assert resources["single_collection"] == {
2327
"name": "single_collection",
@@ -26,6 +30,7 @@ def test_simple_transformer(resources: Dict[str, Any]) -> None:
2630
"write_disposition": "merge",
2731
"endpoint": {
2832
"data_selector": "$",
33+
"paginator": "auto",
2934
"path": "/collection/{collection_id}",
3035
"params": {"collection_id": {"type": "resolve", "resource": "collections", "field": "id"}},
3136
},
@@ -47,7 +52,7 @@ def test_simple_transformer_with_deselected_parent() -> None:
4752
"primary_key": "id",
4853
"write_disposition": "merge",
4954
"selected": False,
50-
"endpoint": {"data_selector": "$", "path": "/collection/"},
55+
"endpoint": {"data_selector": "$", "path": "/collection/", "paginator": "auto"},
5156
}
5257
assert resources["single_collection"] == {
5358
"name": "single_collection",
@@ -58,6 +63,7 @@ def test_simple_transformer_with_deselected_parent() -> None:
5863
"data_selector": "$",
5964
"path": "/collection/{collection_id}",
6065
"params": {"collection_id": {"type": "resolve", "resource": "collections", "field": "id"}},
66+
"paginator": "auto",
6167
},
6268
}
6369

@@ -68,7 +74,7 @@ def test_match_by_path_var_only(resources: Dict[str, Any]) -> None:
6874
"table_name": "user",
6975
"primary_key": "user_id",
7076
"write_disposition": "merge",
71-
"endpoint": {"data_selector": "$", "path": "/users/"},
77+
"endpoint": {"data_selector": "$", "path": "/users/", "paginator": "auto"},
7278
}
7379
assert resources["single_user"] == {
7480
"name": "single_user",
@@ -77,6 +83,7 @@ def test_match_by_path_var_only(resources: Dict[str, Any]) -> None:
7783
"write_disposition": "merge",
7884
"endpoint": {
7985
"data_selector": "$",
86+
"paginator": "auto",
8087
"path": "/users/{user_id}",
8188
"params": {"user_id": {"type": "resolve", "resource": "users", "field": "user_id"}},
8289
},
@@ -89,16 +96,23 @@ def test_match_singularized_path(resources: Dict[str, Any]) -> None:
8996
"table_name": "invoice",
9097
"primary_key": "invoice_id",
9198
"write_disposition": "merge",
92-
"endpoint": {"data_selector": "$", "path": "/invoices/"},
99+
"endpoint": {"data_selector": "$", "path": "/invoices/", "paginator": "auto"},
93100
}
94101
assert resources["single_invoice"] == {
95102
"name": "single_invoice",
96103
"table_name": "invoice",
97104
"primary_key": "invoice_id",
98105
"write_disposition": "merge",
99106
"endpoint": {
107+
"paginator": "auto",
100108
"data_selector": "$",
101109
"path": "/invoice/{invoice_id}",
102-
"params": {"invoice_id": {"type": "resolve", "resource": "invoices", "field": "invoice_id"}},
110+
"params": {
111+
"invoice_id": {
112+
"type": "resolve",
113+
"resource": "invoices",
114+
"field": "invoice_id",
115+
}
116+
},
103117
},
104118
}

tests/integration/extracted/test_dotastats_cases.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ def test_simple_dotastats_load() -> None:
2121
"table_name": "team",
2222
"endpoint": {
2323
"path": "/teams",
24-
"data_selector": "$",
2524
},
2625
}

tests/integration/extracted/test_pokemon_cases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_simple_poke_load() -> None:
1111
assert source["resources"][0] == {
1212
"name": "pokemon",
1313
"table_name": "pokemon",
14-
"endpoint": {"path": "/api/v2/pokemon/", "data_selector": "$"},
14+
"endpoint": {"path": "/api/v2/pokemon/", "paginator": "auto"},
1515
}
1616

1717

@@ -72,6 +72,7 @@ def test_simple_child_table_poke_load() -> None:
7272
"primary_key": "id",
7373
"write_disposition": "merge",
7474
"endpoint": {
75+
# "paginator": "auto",
7576
"path": "/api/v2/pokemon/{name}/",
7677
"data_selector": "$",
7778
"params": {

tests/integration/extracted/test_quotesapi_cases.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def test_simple_quotesapi_load() -> None:
2323
"name": "list",
2424
"table_name": "list",
2525
"endpoint": {
26-
"data_selector": "$",
2726
"path": "/quote/list",
2827
},
2928
}

0 commit comments

Comments
 (0)