• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spec-first / connexion / 11758446876

09 Nov 2024 06:19PM UTC coverage: 96.797%. First build
11758446876

Pull #1992

github

web-flow
Merge 839eed973 into 6a859b317
Pull Request #1992: Upgrade dependencies on v2 banch

23 of 25 new or added lines in 3 files covered. (92.0%)

3083 of 3185 relevant lines covered (96.8%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/connexion/options.py
1
"""
2
This module defines a Connexion specific options class to pass to the Connexion App or API.
3
"""
4

5
import logging
1✔
6
from typing import Optional  # NOQA
1✔
7

8
try:
1✔
9
    from swagger_ui_bundle import swagger_ui_path as default_template_dir
1✔
10
except ImportError:
×
NEW
11
    default_template_dir = None
×
12

13
from connexion.decorators.uri_parsing import AbstractURIParser
1✔
14

15
NO_UI_MSG = """The swagger_ui directory could not be found.
1✔
16
    Please install connexion with extra install: pip install connexion[swagger-ui]
17
    or provide the path to your local installation by passing swagger_path=<your path>
18
"""
19

20
logger = logging.getLogger("connexion.options")
1✔
21

22

23
class ConnexionOptions:
1✔
24
    """Class holding connexion specific options."""
25

26
    def __init__(self, options=None, oas_version=(2,)):
1✔
27
        self._options = {}
1✔
28
        self.oas_version = oas_version
1✔
29
        if self.oas_version >= (3, 0, 0):
1✔
30
            self.openapi_spec_name = '/openapi.json'
1✔
31
            self.swagger_ui_local_path = default_template_dir
1✔
32
        else:
33
            self.openapi_spec_name = '/swagger.json'
1✔
34
            self.swagger_ui_local_path = default_template_dir
1✔
35

36
        if options:
1✔
37
            self._options.update(filter_values(options))
1✔
38

39
    def extend(self, new_values=None):
1✔
40
        # type: (Optional[dict]) -> ConnexionOptions
41
        """
42
        Return a new instance of `ConnexionOptions` using as default the currently
43
        defined options.
44
        """
45
        if new_values is None:
1✔
46
            new_values = {}
1✔
47

48
        options = dict(self._options)
1✔
49
        options.update(filter_values(new_values))
1✔
50
        return ConnexionOptions(options, self.oas_version)
1✔
51

52
    def as_dict(self):
1✔
53
        return self._options
1✔
54

55
    @property
1✔
56
    def openapi_spec_available(self):
1✔
57
        # type: () -> bool
58
        """
59
        Whether to make available the OpenAPI Specification under
60
        `openapi_spec_path`.
61

62
        Default: True
63
        """
64
        deprecated_option = self._options.get('swagger_json', True)
1✔
65
        serve_spec = self._options.get('serve_spec', deprecated_option)
1✔
66
        if 'swagger_json' in self._options:
1✔
67
            deprecation_warning = ("The 'swagger_json' option is deprecated. "
1✔
68
                                   "Please use 'serve_spec' instead")
69
            logger.warning(deprecation_warning)
1✔
70
        return serve_spec
1✔
71

72
    @property
1✔
73
    def openapi_console_ui_available(self):
1✔
74
        # type: () -> bool
75
        """
76
        Whether to make the OpenAPI Console UI available under the path
77
        defined in `openapi_console_ui_path` option.
78

79
        Default: True
80
        """
81
        if (self._options.get('swagger_ui', True) and
1✔
82
                self.openapi_console_ui_from_dir is None):
83
            logger.warning(NO_UI_MSG)
×
84
            return False
×
85
        return self._options.get('swagger_ui', True)
1✔
86

87
    @property
1✔
88
    def openapi_spec_path(self):
1✔
89
        # type: () -> str
90
        """
91
        Path to mount the OpenAPI Console UI and make it accessible via a browser.
92

93
        Default: /openapi.json for openapi3, otherwise /swagger.json
94
        """
95
        return self._options.get('openapi_spec_path', self.openapi_spec_name)
1✔
96

97
    @property
1✔
98
    def openapi_console_ui_path(self):
1✔
99
        # type: () -> str
100
        """
101
        Path to mount the OpenAPI Console UI and make it accessible via a browser.
102

103
        Default: /ui
104
        """
105
        return self._options.get('swagger_url', '/ui')
1✔
106

107
    @property
1✔
108
    def openapi_console_ui_from_dir(self):
1✔
109
        # type: () -> str
110
        """
111
        Custom OpenAPI Console UI directory from where Connexion will serve
112
        the static files.
113

114
        Default: Connexion's vendored version of the OpenAPI Console UI.
115
        """
116
        return self._options.get('swagger_path', self.swagger_ui_local_path)
1✔
117

118
    @property
1✔
119
    def openapi_console_ui_config(self):
1✔
120
        # type: () -> dict
121
        """
122
        Custom OpenAPI Console UI config.
123

124
        Default: None
125
        """
126
        return self._options.get('swagger_ui_config', None)
1✔
127

128
    @property
1✔
129
    def openapi_console_ui_index_template_variables(self):
1✔
130
        # type: () -> dict
131
        """
132
        Custom variables passed to the OpenAPI Console UI template.
133

134
        Default: {}
135
        """
136
        return self._options.get('swagger_ui_template_arguments', {})
1✔
137

138
    @property
1✔
139
    def uri_parser_class(self):
1✔
140
        # type: () -> AbstractURIParser
141
        """
142
        The class to use for parsing URIs into path and query parameters.
143
        Default: None
144
        """
145
        return self._options.get('uri_parser_class', None)
1✔
146

147

148
def filter_values(dictionary):
1✔
149
    # type: (dict) -> dict
150
    """
151
    Remove `None` value entries in the dictionary.
152

153
    :param dictionary:
154
    :return:
155
    """
156
    return {key: value for key, value in dictionary.items() if value is not None}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc