Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/connexion/options.py: 69%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

51 statements  

1""" 

2This module defines a Connexion specific options class to pass to the Connexion App or API. 

3""" 

4 

5import dataclasses 

6import logging 

7import typing as t 

8 

9try: 

10 from swagger_ui_bundle import swagger_ui_path as default_template_dir 

11except ImportError: 

12 default_template_dir = None 

13 

14NO_UI_MSG = """The swagger_ui directory could not be found. 

15 Please install connexion with extra install: pip install connexion[swagger-ui] 

16 or provide the path to your local installation by passing swagger_path=<your path> 

17""" 

18 

19logger = logging.getLogger("connexion.options") 

20 

21 

22@dataclasses.dataclass 

23class SwaggerUIOptions: 

24 """Options to configure the Swagger UI. 

25 

26 :param serve_spec: Whether to serve the Swagger / OpenAPI Specification 

27 :param spec_path: Where to serve the Swagger / OpenAPI Specification 

28 

29 :param swagger_ui: Whether to serve the Swagger UI 

30 :param swagger_ui_path: Where to serve the Swagger UI 

31 :param swagger_ui_config: Options to configure the Swagger UI. See 

32 https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration 

33 for an overview of the available options. 

34 :param swagger_ui_template_dir: Directory with static files to use to serve Swagger UI 

35 :param swagger_ui_template_arguments: Arguments passed to the Swagger UI template. Useful 

36 when providing your own template dir with additional template arguments. 

37 """ 

38 

39 serve_spec: bool = True 

40 spec_path: t.Optional[str] = None 

41 

42 swagger_ui: bool = True 

43 swagger_ui_config: dict = dataclasses.field(default_factory=dict) 

44 swagger_ui_path: str = "/ui" 

45 swagger_ui_template_dir: t.Optional[str] = None 

46 swagger_ui_template_arguments: dict = dataclasses.field(default_factory=dict) 

47 

48 

49class SwaggerUIConfig: 

50 """Class holding swagger UI specific options.""" 

51 

52 def __init__( 

53 self, 

54 options: t.Optional[SwaggerUIOptions] = None, 

55 oas_version: t.Tuple[int, ...] = (2,), 

56 ): 

57 if oas_version >= (3, 0, 0): 

58 self.spec_path = "/openapi.json" 

59 else: 

60 self.spec_path = "/swagger.json" 

61 

62 if options is not None and not isinstance(options, SwaggerUIOptions): 

63 raise ValueError( 

64 f"`swaggger_ui_options` should be of type `SwaggerUIOptions`, " 

65 f"but received {type(options)} instead." 

66 ) 

67 

68 self._options = options or SwaggerUIOptions() 

69 

70 @property 

71 def openapi_spec_available(self) -> bool: 

72 """Whether to make the OpenAPI Specification available.""" 

73 return self._options.serve_spec 

74 

75 @property 

76 def openapi_spec_path(self) -> str: 

77 """Path to host the Swagger UI.""" 

78 return self._options.spec_path or self.spec_path 

79 

80 @property 

81 def swagger_ui_available(self) -> bool: 

82 """Whether to make the Swagger UI available.""" 

83 if self._options.swagger_ui and self.swagger_ui_template_dir is None: 

84 logger.warning(NO_UI_MSG) 

85 return False 

86 return self._options.swagger_ui 

87 

88 @property 

89 def swagger_ui_path(self) -> str: 

90 """Path to mount the Swagger UI and make it accessible via a browser.""" 

91 return self._options.swagger_ui_path 

92 

93 @property 

94 def swagger_ui_template_dir(self) -> str: 

95 """Directory with static files to use to serve Swagger UI.""" 

96 return self._options.swagger_ui_template_dir or default_template_dir 

97 

98 @property 

99 def swagger_ui_config(self) -> dict: 

100 """Options to configure the Swagger UI.""" 

101 return self._options.swagger_ui_config 

102 

103 @property 

104 def swagger_ui_template_arguments(self) -> dict: 

105 """Arguments passed to the Swagger UI template.""" 

106 return self._options.swagger_ui_template_arguments