diff --git a/examples/request_body.py b/examples/request_body.py new file mode 100644 index 00000000..12a04e8c --- /dev/null +++ b/examples/request_body.py @@ -0,0 +1,78 @@ +""" +In this example `openapi` version is used instead of `swagger` version. +""" +from flask import Flask, jsonify, request +from flasgger import Swagger, swag_from + +app = Flask(__name__) +swag = Swagger(app, config={ + 'headers': [], + 'specs': [ + { + 'endpoint': 'apispec', + 'route': '/apispec.json' + } + ], + 'openapi': '3.0.1' +}) + + +@swag.definition('Pet') +class Pet(object): + """ + Pet Object + --- + properties: + name: + type: string + """ + def __init__(self, name): + self.name = str(name) + + def dump(self): + return dict(vars(self).items()) + + +@app.route('/requestBody', methods=['POST']) +@swag_from() +def request_body_endpoint(): + """ + An endpoint for testing requestBody documentation. + --- + description: Post a request body + requestBody: + content: + application/json: + schema: + $ref: '#/definitions/Pet' + required: true + responses: + 200: + description: The posted request body + content: + application/json: + schema: + $ref: '#/definitions/Pet' + """ + return jsonify(request.json) + + +def test_swag(client, specs_data): + """ + This test is runs automatically in Travis CI + + :param client: Flask app test client + :param specs_data: {'url': {swag_specs}} for every spec in app + """ + for url, spec in specs_data.items(): + assert 'Pet' in spec['definitions'] + + assert 'paths' in spec + paths = spec['paths'] + for path_def in paths.values(): + for method_def in path_def.values(): + assert 'requestBody' in method_def + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/examples/use_openapi.py b/examples/use_openapi.py new file mode 100644 index 00000000..abfdd637 --- /dev/null +++ b/examples/use_openapi.py @@ -0,0 +1,34 @@ +""" +In this example `openapi` version is used instead of `swagger` version. +""" +from flask import Flask +from flasgger import Swagger + +app = Flask(__name__) +swag = Swagger(app, config={ + 'headers': [], + 'specs': [ + { + 'endpoint': 'apispec', + 'route': '/apispec.json' + } + ], + 'openapi': '3.0.1' +}) + + +def test_swag(client, specs_data): + """ + This test is runs automatically in Travis CI + + :param client: Flask app test client + :param specs_data: {'url': {swag_specs}} for every spec in app + """ + for spec in specs_data.values(): + assert 'openapi' in spec + assert '3.0.1' == spec['openapi'] + assert 'swagger' not in spec + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/flasgger/base.py b/flasgger/base.py index b6f07952..d36fef52 100644 --- a/flasgger/base.py +++ b/flasgger/base.py @@ -118,9 +118,6 @@ def get(self): The Swagger view get method outputs to /apispecs_1.json """ data = { - "swagger": self.config.get('swagger') or self.config.get( - 'swagger_version', "2.0" - ), # try to get from config['SWAGGER']['info'] # then config['SWAGGER']['specs'][x] # then config['SWAGGER'] @@ -145,6 +142,14 @@ def get(self): "definitions": self.config.get('definitions') or defaultdict(dict) } + openapi_version = self.config.get('openapi') + if openapi_version: + data["openapi"] = openapi_version + else: + data["swagger"] = self.config.get('swagger') or self.config.get( + 'swagger_version', "2.0" + ) + # Support extension properties in the top level config top_level_extension_options = get_vendor_extension_fields(self.config) if top_level_extension_options: @@ -210,6 +215,16 @@ def get(self): verb=verb, prefix_ids=prefix_ids) + request_body = swag.get('requestBody') + if request_body: + content = request_body.get("content", {}) + extract_definitions( + list(content.values()), + endpoint=rule.endpoint, + verb=verb, + prefix_ids=prefix_ids + ) + responses = None if 'responses' in swag: responses = swag.get('responses', {}) @@ -237,6 +252,8 @@ def get(self): operation['summary'] = swag.get('summary') if swag.get('description'): operation['description'] = swag.get('description') + if request_body: + operation['requestBody'] = request_body if responses: operation['responses'] = responses # parameters - swagger ui dislikes empty parameter lists