no classification
no tag
no datas
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?
Just change openapi version to 2.0.0. It works for me
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!