File: _lib2to3.py

package info (click to toggle)
sphinxcontrib-openapi 0.8.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: python: 7,575; makefile: 15
file content (377 lines) | stat: -rw-r--r-- 13,623 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Partial OpenAPI v2.x (fka Swagger) to OpenAPI v3.x converter."""

import functools
import urllib

import picobox


__all__ = [
    "convert",
]


def convert(spec):
    """Convert a given OAS 2 spec to OAS 3."""

    return Lib2to3().convert(spec)


def _is_vendor_extension(key):
    """Return 'True' if a given key is a vendor extension."""

    return key.startswith("x-")


def _get_properties(node, properties, *, vendor_extensions=False):
    """Return a subset of 'node' properties w/ or wo/ vendor extensions."""

    return {
        key: value
        for key, value in node.items()
        if any([key in properties, vendor_extensions and _is_vendor_extension(key)])
    }


def _get_schema_properties(node, *, except_for=None):
    """Find and return 'Schema Object' properties."""

    except_for = except_for or set()
    schema = _get_properties(
        node,
        {
            "additionalProperties",
            "allOf",
            "default",
            "description",
            "discriminator",
            "enum",
            "example",
            "exclusiveMaximum",
            "exclusiveMinimum",
            "externalDocs",
            "format",
            "items",
            "maxItems",
            "maxLength",
            "maxProperties",
            "maximum",
            "minItems",
            "minLength",
            "minProperties",
            "minimum",
            "multipleOf",
            "pattern",
            "properties",
            "readOnly",
            "required",
            "title",
            "type",
            "uniqueItems",
            "xml",
        }
        - set(except_for),
    )

    if "discriminator" in schema:
        schema["discriminator"] = {"propertyName": schema["discriminator"]}
    return schema


def _items_wo_vendor_extensions(node):
    """Iterate over 'node' properties excluding vendor extensions."""

    for key, value in node.items():
        if _is_vendor_extension(key):
            continue
        yield key, value


class Lib2to3:
    _target_version = "3.0.3"
    _injector = picobox.Stack()

    def _insert_into_injector(name):
        def decorator(fn):
            @functools.wraps(fn)
            def wrapper(self, node, *args, **kwargs):
                with Lib2to3._injector.push(picobox.Box(), chain=True) as box:
                    box.put(name, factory=lambda: node, scope=picobox.threadlocal)
                    return fn(self, node, *args, **kwargs)

            return wrapper

        return decorator

    def __init__(self):
        self._schemes = set()

    @_insert_into_injector("spec")
    def convert(self, spec):
        # The following OAS 2 fields are ignored and not converted. Mostly due
        # to the fact that we expect *resolved* spec as input, and most of its
        # fields are used to group shared (i.e. referenced) objects that will
        # not exist in the resolved spec.
        #
        #  - definitions
        #  - parameters
        #  - responses
        #  - securityDefinitions
        #  - security
        #
        # By no means one must assume that these fields will never be
        # converted. I simply have no time to work on this, and for
        # sphixcontrib-openapi purposes it's not actually needed.

        converted = {
            "info": spec["info"],
            "openapi": self._target_version,
            "paths": self.convert_paths(spec["paths"]),
        }
        converted.update(
            _get_properties(spec, {"tags", "externalDocs"}, vendor_extensions=True),
        )

        servers = self.convert_servers(spec)
        if servers:
            converted["servers"] = servers

        return converted

    @_insert_into_injector("paths")
    def convert_paths(self, paths):
        converted = _get_properties(paths, {}, vendor_extensions=True)

        for endpoint, path in _items_wo_vendor_extensions(paths):
            converted[endpoint] = self.convert_path(path)

        return converted

    @_insert_into_injector("path")
    def convert_path(self, path):
        converted = _get_properties(path, {}, vendor_extensions=True)

        for key, value in _items_wo_vendor_extensions(path):
            if key == "parameters":
                converted[key] = self.convert_parameters(value)
            else:
                converted[key] = self.convert_operation(value)

        return converted

    @_insert_into_injector("operation")
    def convert_operation(self, operation):
        converted = _get_properties(
            operation,
            {
                "tags",
                "summary",
                "description",
                "externalDocs",
                "operationId",
                "deprecated",
                "security",
            },
            vendor_extensions=True,
        )

        # Memorize every encountered 'schemes'. Since this property does not
        # exist in OAS 3, it seems the best we can do is to use them in OAS 3
        # 'servers' object.
        self._schemes.update(operation.get("schemes", []))

        if "parameters" in operation:
            parameters = self.convert_parameters(operation["parameters"])

            # Both 'body' and 'formData' parameters are mutually exclusive,
            # therefore there's no way we may end up with both kinds at once.
            request_body = self.convert_request_body(operation)
            request_body = request_body or self.convert_request_body_formdata(operation)

            if parameters:
                converted["parameters"] = parameters

            if request_body:
                converted["requestBody"] = request_body

        converted["responses"] = self.convert_responses(operation["responses"])
        return converted

    @_injector.pass_("spec")
    def convert_request_body(self, operation, *, spec):
        # OAS 3 expects an explicitly specified mimetype of the request body.
        # It's not clear what to do if OAS 2 'consumes' is not defined. Let's
        # start with a glob pattern and figure out what a better option could
        # be later on.
        consumes = operation.get("consumes") or spec.get("consumes") or ["*/*"]

        for parameter in operation["parameters"]:
            if parameter["in"] == "body":
                # Since 'requestBody' is completely new and nested object in
                # OAS 3, it's not clear what should we insert possible vendor
                # extensions. Thus, let's ignore them until we figure it out.
                converted = _get_properties(parameter, {"description", "required"})
                converted["content"] = {
                    consume: {"schema": parameter["schema"]} for consume in consumes
                }
                return converted

        return None

    @_injector.pass_("spec")
    def convert_request_body_formdata(self, operation, *, spec):
        consumes = (
            operation.get("consumes")
            or spec.get("consumes")
            or ["application/x-www-form-urlencoded"]
        )
        supported = {
            "application/x-www-form-urlencoded",
            "multipart/form-data",
        }
        mimetypes = supported.intersection(consumes)
        schema = {"type": "object", "properties": {}}

        for parameter in operation["parameters"]:
            if parameter["in"] == "formData":
                schema["properties"][parameter["name"]] = _get_schema_properties(
                    parameter, except_for={"name", "in", "required"}
                )

                if parameter.get("required"):
                    schema.setdefault("required", []).append(parameter["name"])

                # Excerpt from OpenAPI 2.x spec:
                #
                # > If type is "file", the consumes MUST be either
                # > "multipart/form-data", "application/x-www-form-urlencoded"
                # > or both and the parameter MUST be in "formData".
                #
                # This is weird since HTTP does not allow file uploading in
                # 'application/x-www-form-urlencoded'. Moreover, Swagger
                # editor complains if 'file' is detected and there's no
                # 'multipart/form-data' in `consumes'.
                if parameter["type"] == "file":
                    mimetypes = ["multipart/form-data"]

        if not schema["properties"]:
            return None
        return {"content": {mimetype: {"schema": schema} for mimetype in mimetypes}}

    @_insert_into_injector("parameters")
    def convert_parameters(self, parameters):
        return [
            self.convert_parameter(parameter)
            for parameter in parameters
            # If a parameter is one of the backward compatible type, delegate
            # the call to the converter function. Incompatible types, such as
            # 'formData' and 'body', must be handled separately since they are
            # reflected in 'Operation Object' in OAS 3.
            if parameter["in"] in {"query", "header", "path"}
        ]

    @_insert_into_injector("parameter")
    def convert_parameter(self, parameter):
        schema = _get_schema_properties(
            parameter,
            # Some of 'Parameter Object' properties have the same name as some
            # of 'Schema Object' properties. Since we know for sure that in
            # this context they are part of 'Parameter Object', we should
            # ignore their meaning as part of 'Schema Object'.
            except_for={"name", "in", "description", "required"},
        )
        converted = {
            key: value for key, value in parameter.items() if key not in schema
        }
        converted["schema"] = schema
        collection_format = converted.pop("collectionFormat", None)

        if converted["in"] in {"path", "header"} and collection_format == "csv":
            converted["style"] = "simple"
        elif converted["in"] in {"query"} and collection_format:
            styles = {
                "csv": {"style": "form", "explode": False},
                "multi": {"style": "form", "explode": True},
                "ssv": {"style": "spaceDelimited"},
                "pipes": {"style": "pipeDelimited"},
                # OAS 3 does not explicitly say what is the alternative to
                # 'collectionFormat=tsv'. We have no other option but to ignore
                # it. Fortunately, we don't care much as it's not used by the
                # renderer.
                "tsv": {},
            }
            converted.update(styles[collection_format])

        return converted

    @_insert_into_injector("responses")
    def convert_responses(self, responses):
        converted = _get_properties(responses, {}, vendor_extensions=True)

        for status_code, response in _items_wo_vendor_extensions(responses):
            converted[status_code] = self.convert_response(response)

        return converted

    @_injector.pass_("spec")
    @_injector.pass_("operation")
    @_insert_into_injector("response")
    def convert_response(self, response, *, spec, operation):
        converted = _get_properties(response, {"description"}, vendor_extensions=True)

        # OAS 3 expects an explicitly specified mimetype in the response. It's
        # not clear what to do if OAS 2 'produces' is not defined. Let's start
        # with a glob pattern and figure out what a better option could be
        # later on.
        produces = operation.get("produces") or spec.get("produces") or ["*/*"]
        schema = response.get("schema")
        examples = response.get("examples")

        if schema or examples:
            content = converted.setdefault("content", {})

            if schema is not None:
                for mimetype in produces:
                    content.setdefault(mimetype, {})["schema"] = schema

            if examples is not None:
                # According to OAS2, mimetypes in 'examples' property MUST be
                # one of the operation's 'produces'.
                for mimetype, example in examples.items():
                    content.setdefault(mimetype, {})["example"] = example

        if "headers" in response:
            converted["headers"] = {
                key: dict(
                    _get_properties(value, "description", vendor_extensions=True),
                    schema=_get_schema_properties(value, except_for={"description"}),
                )
                for key, value in response["headers"].items()
            }

        return converted

    def convert_servers(self, spec):
        """Convert OAS2 '(host, basePath, schemes)' triplet into OAS3 'servers' node."""

        host = spec.get("host", "")
        basepath = spec.get("basePath", "")
        schemes = self._schemes.union(spec.get("schemes", set()))

        # Since 'host', 'basePath' and 'schemes' are optional in OAS 2, there
        # may be the case when they aren't set. If that's happened it means
        # there's nothing to convert, and thus we simply return an empty list.
        if not host and not basepath and not schemes:
            return []

        if not schemes:
            # If 'host' is not set, the url will contain a bare basePath.
            # According to OAS 3, it's a valid URL, and both the host and the
            # scheme must be assumed to be the same as the server that shared
            # this OAS 3 spec.
            return [{"url": urllib.parse.urljoin(host, basepath)}]

        return [
            {"url": urllib.parse.urlunsplit([scheme, host, basepath, None, None])}
            for scheme in sorted(schemes)
        ]