News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Return values are not shown in Swagger-UI

posted on 2023-11-12 14:31     read(788)     comment(0)     like(18)     collect(3)


I have created a very simple demo to demonstrate that the return values are not shown in swagger-ui:

Requirements.txt

flask-apispec
flask-marshmallow
flask-restful

Versions:

apispec==3.3.0
Flask==1.1.1
flask-apispec==0.8.6
flask-marshmallow==0.11.0
Flask-RESTful==0.3.8
marshmallow==3.5.0
webargs==5.5.3

Code:

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import Flask
from flask_apispec import use_kwargs, marshal_with, FlaskApiSpec, MethodResource
from flask_marshmallow import Marshmallow
from flask_restful import Api
from marshmallow import fields

app = Flask(__name__)
api = Api(app)
ma = Marshmallow(app)


class Todo:
    def __init__(self, todo_id, description):
        self.todo_id = todo_id
        self.description = description

    def __getitem__(self, item):
        return getattr(self, item)


class TodoInputSchema(ma.Schema):
    token = fields.Str()


class TodoSchema(ma.Schema):
    class Meta:
        fields = ("todo_id", "description")


class Demo(MethodResource):
    def __init__(self):
        super(Demo, self).__init__()

    @use_kwargs(TodoInputSchema)
    @marshal_with(TodoSchema, code=200)
    def get(self):
        schema = TodoSchema()
        todo = Todo(1, "get Milk")
        return schema.dump(todo)


api.add_resource(Demo, '/')
app.config.update({
    'APISPEC_SPEC': APISpec(
        title='todo',
        version='v1',
        plugins=[MarshmallowPlugin()],
        openapi_version='3.0.2'
    ),
    'APISPEC_SWAGGER_URL': '/swagger/',
})
docs = FlaskApiSpec(app)

docs.register(Demo)

if __name__ == '__main__':
    app.run(debug=True, port=3333)

Run: http://127.0.0.1:3333/swagger-ui

Why is the return object not shown in Swagger UI?

enter image description here


solution


Just change openapi version to 2.0.0. It works for me



Category of website: technical article > Q&A

Author:qs

link:http://www.pythonblackhole.com/blog/article/245168/12a44a5ea2a2b92d60a5/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

18 0
collect article
collected

Comment content: (supports up to 255 characters)