55from typing import Dict , List , Optional , Tuple , Union , cast
66
77from dlt_openapi .config import Config
8- from dlt_openapi .detector .base_detector import BaseDetector
8+ from dlt_openapi .detector .base_detector import GLOBAL_WARNING_KEY , BaseDetector
99from dlt_openapi .detector .default import utils
1010from dlt_openapi .detector .default .primary_key import detect_primary_key_by_name
1111from dlt_openapi .parser .endpoints import Endpoint , EndpointCollection , Response , TransformerSetting
3636 RE_UNIQUE_KEY ,
3737)
3838from .utils import to_int
39+ from .warnings import (
40+ BaseDetectionWarning ,
41+ DataResponseNoBodyWarning ,
42+ DataResponseUndetectedWarning ,
43+ PrimaryKeyNotFoundWarning ,
44+ UnresolvedPathParametersWarning ,
45+ )
3946
4047Tree = Dict [str , Union ["str" , "Tree" ]]
4148
4249
4350class DefaultDetector (BaseDetector ):
51+
52+ warnings : Dict [str , List [BaseDetectionWarning ]] = {}
53+
4454 def __init__ (self , config : Config ) -> None :
4555 self .config = config
4656
4757 def run (self , open_api : OpenapiParser ) -> None :
4858 """Run the detector"""
59+ self .warnings = {}
4960
5061 # discover stuff from responses
5162 self .detect_paginators_and_responses (open_api .endpoints )
@@ -62,6 +73,11 @@ def run(self, open_api: OpenapiParser) -> None:
6273 # and sort resources by table name
6374 open_api .endpoints .endpoints .sort (key = lambda e : e .detected_table_name )
6475
76+ # add some warnings
77+ for e in open_api .endpoints .endpoints :
78+ if params := e .unresolvable_path_param_names :
79+ self ._add_warning (UnresolvedPathParametersWarning (params ), e )
80+
6581 def detect_resource_names (self , endpoints : EndpointCollection ) -> None :
6682 """iterate all endpoints and find a strategy to select the right resource name"""
6783
@@ -144,9 +160,9 @@ def detect_paginators_and_responses(self, endpoints: EndpointCollection) -> None
144160 endpoint .detected_data_response .detected_payload = self .detect_response_payload (
145161 endpoint .detected_data_response , expect_list = expect_list
146162 )
147- self .detect_primary_key (endpoint .detected_data_response , endpoint .path )
163+ self .detect_primary_key (endpoint , endpoint .detected_data_response , endpoint .path )
148164
149- def detect_primary_key (self , response : Response , path : str ) -> None :
165+ def detect_primary_key (self , e : Endpoint , response : Response , path : str ) -> None :
150166 """detect the primary key from the payload"""
151167 if not response .detected_payload :
152168 return
@@ -179,6 +195,9 @@ def detect_primary_key(self, response: Response, path: str) -> None:
179195 elif uuid_paths :
180196 response .detected_primary_key = uuid_paths [0 ]
181197
198+ if not response .detected_primary_key :
199+ self ._add_warning (PrimaryKeyNotFoundWarning (), e )
200+
182201 def detect_main_response (self , endpoint : Endpoint ) -> Optional [Response ]:
183202 """Get main response and pagination for endpoint"""
184203
@@ -191,6 +210,12 @@ def detect_main_response(self, endpoint: Endpoint) -> Optional[Response]:
191210 if response .status_code .startswith ("2" ) and not main_response :
192211 main_response = response
193212
213+ if not main_response :
214+ self ._add_warning (DataResponseUndetectedWarning (), endpoint )
215+
216+ if main_response and not main_response .schema :
217+ self ._add_warning (DataResponseNoBodyWarning (), endpoint )
218+
194219 return main_response
195220
196221 def detect_response_payload (self , response : Response , expect_list : bool ) -> Optional [DataPropertyPath ]:
@@ -397,3 +422,11 @@ def find_nearest_list_parent(endpoint: Endpoint) -> Optional[Endpoint]:
397422 endpoint .detected_parent = find_nearest_list_parent (endpoint )
398423 if endpoint .detected_parent :
399424 endpoint .detected_parent .detected_children .append (endpoint )
425+
426+ def get_warnings (self ) -> Dict [str , List [BaseDetectionWarning ]]:
427+ return self .warnings
428+
429+ def _add_warning (self , warning : BaseDetectionWarning , e : Optional [Endpoint ] = None ) -> None :
430+ key = e .id if e else GLOBAL_WARNING_KEY
431+ warning_list = self .warnings .setdefault (key , [])
432+ warning_list .append (warning )
0 commit comments