55import os
66import re
77import sys
8- from copy import deepcopy
98from importlib import import_module
109from typing import Optional , List , Dict , Union , Any , Type , Callable
1110
1413
1514if sys .version_info >= (3 , 10 ):
1615 from importlib .metadata import entry_points
17- else :
16+ else : # pragma: no cover
1817 from importlib_metadata import entry_points # type: ignore
1918
2019from .blueprint import APIBlueprint
@@ -108,10 +107,10 @@ def __init__(
108107 # Set security schemes, responses, paths and components
109108 self .security_schemes = security_schemes
110109
111- responses = responses or {}
112110 # Convert key to string
113- self .responses = convert_responses_key_to_string (responses )
111+ self .responses = convert_responses_key_to_string (responses or {} )
114112
113+ # Initialize instance variables
115114 self .paths : Dict = dict ()
116115 self .components_schemas : Dict = dict ()
117116 self .components = Components ()
@@ -132,7 +131,7 @@ def __init__(
132131 self .operation_id_callback : Callable = operation_id_callback
133132
134133 # Set OpenAPI extensions
135- self .openapi_extensions = openapi_extensions or dict ()
134+ self .openapi_extensions = openapi_extensions or {}
136135
137136 # Set HTTP Response of validation errors within OpenAPI
138137 self .validation_error_status = str (validation_error_status )
@@ -147,7 +146,7 @@ def __init__(
147146 self .cli .add_command (openapi_command ) # type: ignore
148147
149148 # Initialize specification JSON
150- self .spec_json : Dict = dict ()
149+ self .spec_json : Dict = {}
151150
152151 def _init_doc (self ) -> None :
153152 """
@@ -174,6 +173,7 @@ def _init_doc(self) -> None:
174173 )
175174
176175 ui_templates = []
176+ # Iterate over all entry points in the "flask_openapi3.plugins" group
177177 for entry_point in entry_points (group = "flask_openapi3.plugins" ):
178178 try :
179179 module_path = entry_point .value
@@ -186,7 +186,7 @@ def _init_doc(self) -> None:
186186 bp = plugin_register (doc_url = self .doc_url .lstrip ("/" ))
187187 self .register_blueprint (bp , url_prefix = self .doc_prefix )
188188 ui_templates .append ({"name" : plugin_name , "display_name" : plugin_display_name })
189- except (ModuleNotFoundError , AttributeError ):
189+ except (ModuleNotFoundError , AttributeError ): # pragma: no cover
190190 import traceback
191191 print (f"Warning: plugin '{ entry_point .value } ' registration failed." )
192192 traceback .print_exc ()
@@ -281,6 +281,7 @@ def register_api(self, api: APIBlueprint) -> None:
281281 if tag .name not in self .tag_names :
282282 # Append tag to the list of tags
283283 self .tags .append (tag )
284+
284285 # Append tag name to the list of tag names
285286 self .tag_names .append (tag .name )
286287
@@ -309,6 +310,7 @@ def register_api_view(self, api_view: APIView, view_kwargs: Optional[Dict[Any, A
309310 if tag .name not in self .tag_names :
310311 # Append tag to the list of tags
311312 self .tags .append (tag )
313+
312314 # Append tag name to the list of tag names
313315 self .tag_names .append (tag .name )
314316
@@ -369,14 +371,12 @@ def _collect_openapi_info(
369371 method: HTTP method for the operation. Defaults to GET.
370372 """
371373 if doc_ui is True :
372- if responses is None :
373- new_responses = {}
374- else :
375- # Convert key to string
376- new_responses = convert_responses_key_to_string (responses )
374+ # Convert key to string
375+ new_responses = convert_responses_key_to_string (responses or {})
376+
377377 # Global response: combine API responses
378- combine_responses = deepcopy ( self .responses )
379- combine_responses . update ( ** new_responses )
378+ combine_responses = { ** self .responses , ** new_responses }
379+
380380 # Create operation
381381 operation = get_operation (
382382 func ,
@@ -386,34 +386,34 @@ def _collect_openapi_info(
386386 )
387387 # Set external docs
388388 operation .externalDocs = external_docs
389+
389390 # Unique string used to identify the operation.
390391 operation .operationId = operation_id or self .operation_id_callback (
391392 name = func .__name__ , path = rule , method = method
392393 )
394+
393395 # Only set `deprecated` if True, otherwise leave it as None
394396 operation .deprecated = deprecated
397+
395398 # Add security
396399 operation .security = security
400+
397401 # Add servers
398402 operation .servers = servers
403+
399404 # Store tags
400- if tags is None :
401- tags = []
402- parse_and_store_tags (tags , self .tags , self .tag_names , operation )
403- # Parse parameters
404- header , cookie , path , query , form , body , raw = parse_parameters (
405- func ,
406- components_schemas = self .components_schemas ,
407- operation = operation
408- )
405+ parse_and_store_tags (tags or [], self .tags , self .tag_names , operation )
406+
409407 # Parse response
410408 get_responses (combine_responses , self .components_schemas , operation )
409+
411410 # Convert a route parameter format from /pet/<petId> to /pet/{petId}
412411 uri = re .sub (r"<([^<:]+:)?" , "{" , rule ).replace (">" , "}" )
412+
413413 # Parse method
414414 parse_method (uri , method , self .paths , operation )
415- return header , cookie , path , query , form , body , raw
416- else :
415+
417416 # Parse parameters
418- header , cookie , path , query , form , body , raw = parse_parameters (func , doc_ui = False )
419- return header , cookie , path , query , form , body , raw
417+ return parse_parameters (func , components_schemas = self .components_schemas , operation = operation )
418+ else :
419+ return parse_parameters (func , doc_ui = False )
0 commit comments