|
7 | 7 |
|
8 | 8 | """
|
9 | 9 | import codecs
|
| 10 | +import json |
10 | 11 | import logging
|
11 | 12 | import os
|
12 | 13 | import re
|
13 |
| -from typing import Optional |
14 |
| - |
15 |
| -import yaml |
16 |
| -import json |
17 |
| - |
18 | 14 | from collections import defaultdict
|
19 | 15 | from functools import partial, wraps
|
| 16 | +from typing import Optional |
20 | 17 |
|
21 |
| -from flask import (abort, Blueprint, current_app, jsonify, Markup, redirect, |
22 |
| - render_template, request, Response, url_for) |
| 18 | +import yaml |
| 19 | +from flask import ( |
| 20 | + abort, |
| 21 | + Blueprint, |
| 22 | + current_app, |
| 23 | + jsonify, |
| 24 | + Markup, |
| 25 | + redirect, |
| 26 | + render_template, |
| 27 | + request, |
| 28 | + Response, |
| 29 | + url_for, |
| 30 | +) |
23 | 31 | from flask.json import JSONEncoder
|
24 | 32 | from flask.views import MethodView
|
25 | 33 | from werkzeug.datastructures import Authorization
|
|
32 | 40 | from mistune import markdown
|
33 | 41 |
|
34 | 42 | from . import __version__
|
35 |
| -from .constants import (OAS3_SUB_COMPONENTS, OPTIONAL_FIELDS, |
36 |
| - OPTIONAL_OAS3_FIELDS) |
37 |
| -from .utils import (extract_definitions, extract_schema, get_schema_specs, |
38 |
| - get_specs, get_vendor_extension_fields, is_openapi3, |
39 |
| - LazyString, parse_definition_docstring, parse_imports, |
40 |
| - swag_annotation, validate) |
| 43 | +from .constants import OAS3_SUB_COMPONENTS, OPTIONAL_FIELDS, OPTIONAL_OAS3_FIELDS |
| 44 | +from .utils import ( |
| 45 | + convert_responses_to_openapi3, |
| 46 | + extract_definitions, |
| 47 | + extract_schema, |
| 48 | + get_schema_specs, |
| 49 | + get_specs, |
| 50 | + get_vendor_extension_fields, |
| 51 | + is_openapi3, |
| 52 | + LazyString, |
| 53 | + parse_definition_docstring, |
| 54 | + parse_imports, |
| 55 | + swag_annotation, |
| 56 | + validate, |
| 57 | +) |
41 | 58 |
|
42 | 59 |
|
43 | 60 | def NO_SANITIZER(text):
|
@@ -546,20 +563,29 @@ def get_operations(swag, path_verb=None):
|
546 | 563 | operation['requestBody'] = request_body
|
547 | 564 | if callbacks:
|
548 | 565 | operation['callbacks'] = callbacks
|
549 |
| - if responses: |
550 |
| - operation['responses'] = responses |
551 | 566 | # parameters - swagger ui dislikes empty parameter lists
|
552 | 567 | if len(params) > 0:
|
553 | 568 | operation['parameters'] = params
|
| 569 | + |
| 570 | + media_types = ['application/json'] |
554 | 571 | # other optionals
|
555 | 572 | for key in optional_fields:
|
556 | 573 | if key in swag:
|
557 | 574 | value = swag.get(key)
|
558 | 575 | if key in ('produces', 'consumes'):
|
559 | 576 | if not isinstance(value, (list, tuple)):
|
560 | 577 | value = [value]
|
| 578 | + if key == 'produces': |
| 579 | + media_types = value |
561 | 580 |
|
562 | 581 | operation[key] = value
|
| 582 | + |
| 583 | + if responses: |
| 584 | + if is_openapi3(openapi_version): |
| 585 | + convert_responses_to_openapi3(responses, media_types) |
| 586 | + |
| 587 | + operation['responses'] = responses |
| 588 | + |
563 | 589 | if path_verb:
|
564 | 590 | operations[path_verb] = operation
|
565 | 591 | else:
|
@@ -616,8 +642,12 @@ def get_operations(swag, path_verb=None):
|
616 | 642 | paths[srule][key] = val
|
617 | 643 | self.apispecs[endpoint] = data
|
618 | 644 |
|
| 645 | + # if is_openapi3(openapi_version): |
| 646 | + # del data['definitions'] |
619 | 647 | if is_openapi3(openapi_version):
|
620 |
| - del data['definitions'] |
| 648 | + # Copy definitions to components/schemas |
| 649 | + if definitions: |
| 650 | + data.setdefault('components', {}).setdefault('schemas', {}).update(definitions) |
621 | 651 |
|
622 | 652 | return data
|
623 | 653 |
|
@@ -786,7 +816,7 @@ def update_schemas_parsers(self, doc, schemas, parsers, definitions):
|
786 | 816 | '''
|
787 | 817 | Schemas and parsers would be updated here from doc
|
788 | 818 | '''
|
789 |
| - if self.is_openapi3(): |
| 819 | + if is_openapi3(self.config.get('openapi')): |
790 | 820 | # 'json' to comply with self.SCHEMA_LOCATIONS's {'body':'json'}
|
791 | 821 | location = 'json'
|
792 | 822 | json_schema = None
|
|
0 commit comments