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
@@ -62,6 +72,11 @@ def run(self, open_api: OpenapiParser) -> None:
6272 # and sort resources by table name
6373 open_api .endpoints .endpoints .sort (key = lambda e : e .detected_table_name )
6474
75+ # add some warnings
76+ for e in open_api .endpoints .endpoints :
77+ if params := e .unresolvable_path_param_names :
78+ self ._add_warning (UnresolvedPathParametersWarning (params ), e )
79+
6580 def detect_resource_names (self , endpoints : EndpointCollection ) -> None :
6681 """iterate all endpoints and find a strategy to select the right resource name"""
6782
@@ -144,9 +159,9 @@ def detect_paginators_and_responses(self, endpoints: EndpointCollection) -> None
144159 endpoint .detected_data_response .detected_payload = self .detect_response_payload (
145160 endpoint .detected_data_response , expect_list = expect_list
146161 )
147- self .detect_primary_key (endpoint .detected_data_response , endpoint .path )
162+ self .detect_primary_key (endpoint , endpoint .detected_data_response , endpoint .path )
148163
149- def detect_primary_key (self , response : Response , path : str ) -> None :
164+ def detect_primary_key (self , e : Endpoint , response : Response , path : str ) -> None :
150165 """detect the primary key from the payload"""
151166 if not response .detected_payload :
152167 return
@@ -179,6 +194,9 @@ def detect_primary_key(self, response: Response, path: str) -> None:
179194 elif uuid_paths :
180195 response .detected_primary_key = uuid_paths [0 ]
181196
197+ if not response .detected_primary_key :
198+ self ._add_warning (PrimaryKeyNotFoundWarning (), e )
199+
182200 def detect_main_response (self , endpoint : Endpoint ) -> Optional [Response ]:
183201 """Get main response and pagination for endpoint"""
184202
@@ -191,6 +209,12 @@ def detect_main_response(self, endpoint: Endpoint) -> Optional[Response]:
191209 if response .status_code .startswith ("2" ) and not main_response :
192210 main_response = response
193211
212+ if not main_response :
213+ self ._add_warning (DataResponseUndetectedWarning (), endpoint )
214+
215+ if main_response and not main_response .schema :
216+ self ._add_warning (DataResponseNoBodyWarning (), endpoint )
217+
194218 return main_response
195219
196220 def detect_response_payload (self , response : Response , expect_list : bool ) -> Optional [DataPropertyPath ]:
@@ -397,3 +421,11 @@ def find_nearest_list_parent(endpoint: Endpoint) -> Optional[Endpoint]:
397421 endpoint .detected_parent = find_nearest_list_parent (endpoint )
398422 if endpoint .detected_parent :
399423 endpoint .detected_parent .detected_children .append (endpoint )
424+
425+ def get_warnings (self ) -> Dict [str , List [BaseDetectionWarning ]]:
426+ return self .warnings
427+
428+ def _add_warning (self , warning : BaseDetectionWarning , e : Optional [Endpoint ] = None ) -> None :
429+ key = e .id if e else GLOBAL_WARNING_KEY
430+ warning_list = self .warnings .setdefault (key , [])
431+ warning_list .append (warning )
0 commit comments