swag_from with "schema": MyMarshmallowSchema always generates Swagger 2.0 style references
kiranzo opened this issue · 0 comments
So, I have a Flask project where I want to generate Swagger docs with Flasgger, Apispec and Marshmallow.
Since I want to use requestBody, I moved to OpenAPI 3.0.2, and when I did it, Swagger showed me errors saying that it can't find my schemas, because the definitions themselves moved under components/schemas/MyMarshmallow
(without the "Schema" part), but for endpoints, schemas are still in "$ref": "#/definitions/MyMarshmallowSchema"
.
The code that leads to this:
from flask import Flask
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flasgger import APISpec, Swagger
from app.my_api import api_blueprint
spec = APISpec(
title="My Test API",
version="1.0.0",
openapi_version="3.0.2",
info=dict(description="My Test API"),
plugins=[
FlaskPlugin(),
MarshmallowPlugin(),
],
)
flask_app = Flask(__name__)
# just in case
flask_app.config['SWAGGER'] = {
'uiversion': 3,
'title': 'My Test API',
'openapi': '3.0.2'
}
template = spec.to_flasgger(
flask_app,
definitions=[MyMarshmallowSchema]
)
swag = Swagger(flask_app, template=template)
flask_app.register_blueprint(api_blueprint)
flask_app.run(debug=True)
and swag_from
for my endpoint:
from schemas import MyMarshmallowSchema
from flasgger import swag_from
api_blueprint = Blueprint("my_blueprint", __name__, url_prefix="/api")
@api_blueprint.route("/my-route", methods=["GET"])
@swag_from({
"tags": ["mytag"],
"responses": {
HTTPStatus.OK.value: {
"description": "test",
"schema": MyMarshmallowSchema
},
}
})
def test():
........
Basically, this results in a broken swagger, because the schema definition is de facto under components/schemas/MyMarshmallow
(OpenAPI 3.0.2 style) , but methods expect it to be under "$ref": "#/definitions/MyMarshmallowSchema"
(Swagger 2.0 style).
Since this came from the swag_from
, I think it's Flasgger's fault, and I already told it I'm using OpenAPI 3.0.2 in APISpec object and in Flask 'SWAGGER' config part.
I solved this by manually changing the definition to "schema": {"$ref": "#/components/schemas/MyMarshmallow" }
in swag_from
, but I don't think this is the correct behaviour.