Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apispec is broken with 8.5.4 #195

Open
CedricCabessa opened this issue May 9, 2022 · 0 comments
Open

apispec is broken with 8.5.4 #195

CedricCabessa opened this issue May 9, 2022 · 0 comments

Comments

@CedricCabessa
Copy link
Contributor

Hi, I'm using marshmallow-dataclass with Flask, the documentation is generated via api-spec

Since version 8.5.4 I observed a regression. The object model are not generated in the openapi file.

Here a exemple to reproduce (sorry I tried to minimise it, but it is a bit long)

from flask import Flask
from apispec import APISpec
from marshmallow_dataclass import dataclass
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin


@dataclass
class Foo:
    val: str

# UNCOMMENT THIS LINE MAKE IT WORKS
# Foo.Schema()


spec = APISpec(
    title="title",
    version="0.1",
    openapi_version="3.0.2",
    plugins=[FlaskPlugin(), MarshmallowPlugin()],
)

app = Flask("demo")


@app.route("/")
def hello():
    """Get api users
    ---
    get:
      summary: sum
      responses:
        200:
          content:
            application/json:
              schema: Foo
    """
    return Foo(val="bar")


with app.test_request_context():
    for rule in app.url_map.iter_rules():
        view = app.view_functions.get(rule.endpoint)
        spec.path(view=view)

print(spec.to_dict())

This snippet setup a flask project that return a payload documented by apispec

Running this project print an openapi yaml file

{'paths': {'/': {'get': {'summary': 'sum', 'responses': {'200': {'content': {'application/json': {'schema': {'$ref': '#/components/schemas/Foo'}}}}}}}, '/static/{filename}': {}}, 'info': {'title': 'title', 'version': '0.1'}, 'openapi': '3.0.2'}

However this file is invalid as $ref': '#/components/schemas/Foo' is never defined

What we need to do is instantiate the schema once with Foo.Schema() now we get

{'paths': {'/': {'get': {'summary': 'sum', 'responses': {'200': {'content': {'application/json': {'schema': {'$ref': '#/components/schemas/Foo'}}}}}}}, '/static/{filename}': {}}, 'info': {'title': 'title', 'version': '0.1'}, 'openapi': '3.0.2', 'components': {'schemas': {'Foo': {'type': 'object', 'properties': {'val': {'type': 'string'}}, 'required': ['val']}}}}

I tracked the regression on https://github.com/lovasoa/marshmallow_dataclass/pull/170/files#diff-3d998111751e2042da9789eb7a8181144cb7256d31e243ec09a23f639f10ee21R204

tl;dr: Delaying Schema instantiation break api-spec

Maybe the fix should be in api-spec or MarshmallowPlugin (I haven't dig yet)

Do you have suggestion on how to fix this ? cc @AleksanderPawlak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant