Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/connexion/options.py: 47%
57 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
1"""
2This module defines a Connexion specific options class to pass to the Connexion App or API.
3"""
5import logging
6from typing import Optional # NOQA
8try:
9 from py_swagger_ui import swagger_ui_path
10except ImportError:
11 swagger_ui_path = None
13NO_UI_MSG = """The swagger_ui directory could not be found.
14 Please install connexion with extra install: pip install connexion[swagger-ui]
15 or provide the path to your local installation by passing swagger_path=<your path>
16"""
18logger = logging.getLogger("connexion.options")
21class SwaggerUIOptions:
22 """Class holding swagger UI specific options."""
24 def __init__(self, options=None, oas_version=(2,)):
25 self._options = {}
26 self.oas_version = oas_version
27 self.swagger_ui_local_path = swagger_ui_path
28 if self.oas_version >= (3, 0, 0):
29 self.openapi_spec_name = "/openapi.json"
30 else:
31 self.openapi_spec_name = "/swagger.json"
33 if options:
34 self._options.update(filter_values(options))
36 def extend(self, new_values=None):
37 # type: (Optional[dict]) -> SwaggerUIOptions
38 """
39 Return a new instance of `ConnexionOptions` using as default the currently
40 defined options.
41 """
42 if new_values is None:
43 new_values = {}
45 options = dict(self._options)
46 options.update(filter_values(new_values))
47 return SwaggerUIOptions(options, self.oas_version)
49 def as_dict(self):
50 return self._options
52 @property
53 def openapi_spec_available(self):
54 # type: () -> bool
55 """
56 Whether to make available the OpenAPI Specification under
57 `openapi_spec_path`.
59 Default: True
60 """
61 deprecated_option = self._options.get("swagger_json", True)
62 serve_spec = self._options.get("serve_spec", deprecated_option)
63 if "swagger_json" in self._options:
64 deprecation_warning = (
65 "The 'swagger_json' option is deprecated. "
66 "Please use 'serve_spec' instead"
67 )
68 logger.warning(deprecation_warning)
69 return serve_spec
71 @property
72 def openapi_console_ui_available(self):
73 # type: () -> bool
74 """
75 Whether to make the OpenAPI Console UI available under the path
76 defined in `openapi_console_ui_path` option.
78 Default: True
79 """
80 if (
81 self._options.get("swagger_ui", True)
82 and self.openapi_console_ui_from_dir is None
83 ):
84 logger.warning(NO_UI_MSG)
85 return False
86 return self._options.get("swagger_ui", True)
88 @property
89 def openapi_spec_path(self):
90 # type: () -> str
91 """
92 Path to mount the OpenAPI Console UI and make it accessible via a browser.
94 Default: /openapi.json for openapi3, otherwise /swagger.json
95 """
96 return self._options.get("openapi_spec_path", self.openapi_spec_name)
98 @property
99 def openapi_console_ui_path(self):
100 # type: () -> str
101 """
102 Path to mount the OpenAPI Console UI and make it accessible via a browser.
104 Default: /ui
105 """
106 return self._options.get("swagger_url", "/ui")
108 @property
109 def openapi_console_ui_from_dir(self):
110 # type: () -> str
111 """
112 Custom OpenAPI Console UI directory from where Connexion will serve
113 the static files.
115 Default: Connexion's vendored version of the OpenAPI Console UI.
116 """
117 return self._options.get("swagger_path", self.swagger_ui_local_path)
119 @property
120 def openapi_console_ui_config(self):
121 # type: () -> dict
122 """
123 Custom OpenAPI Console UI config.
125 Default: None
126 """
127 return self._options.get("swagger_ui_config", None)
129 @property
130 def openapi_console_ui_index_template_variables(self):
131 # type: () -> dict
132 """
133 Custom variables passed to the OpenAPI Console UI template.
135 Default: {}
136 """
137 return self._options.get("swagger_ui_template_arguments", {})
140def filter_values(dictionary):
141 # type: (dict) -> dict
142 """
143 Remove `None` value entries in the dictionary.
145 :param dictionary:
146 :return:
147 """
148 return {key: value for key, value in dictionary.items() if value is not None}