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

Use python 3.7 new dataclass to specify Schema #204

Open
jorgeCollinet opened this issue Jun 28, 2018 · 2 comments
Open

Use python 3.7 new dataclass to specify Schema #204

jorgeCollinet opened this issue Jun 28, 2018 · 2 comments

Comments

@jorgeCollinet
Copy link

@jorgeCollinet jorgeCollinet commented Jun 28, 2018

With the new python 3.7 dataclass (https://www.python.org/dev/peps/pep-0557/)

It would be nice to automatically infer the response schema from it , instead of:

from marshmallow import Schema, fields, post_load
from flasgger import Swagger, SwaggerView

class FooSchema(Schema):
    url = fields.Str(required=True)

class FooView(SwaggerView):
    responses = {
        200: {
            'description': 'Foo Example',
            'schema': FooSchema
        }
    }
    . . . . 

do

from flasgger import Swagger, SwaggerView

@dataclass
class Foo:
    url: str

class FooView(SwaggerView):
    responses = {
        200: {
            'description': 'Foo Example',
            'schema': Foo
        }
    }
    . . . . 
@lovasoa
Copy link

@lovasoa lovasoa commented Feb 5, 2019

I published a library that allows the usage of flasgger with dataclasses.

marshmallow-dataclass

You can use it almost as in your example:

from flasgger import Swagger, SwaggerView
from marshmallow_dataclass import dataclass # Importing from marshmallow_dataclass instead of dataclasses

@dataclass
class Foo:
    url: str

class FooView(SwaggerView):
    responses = {
        200: {
            'description': 'Foo Example',
            'schema': Foo.Schema
        }
    }
@CptSpaceToaster
Copy link

@CptSpaceToaster CptSpaceToaster commented Oct 13, 2019

Because these schemas deserialize content into a dataclass instead of a dictionary, constructed Schemas can not be used as input parameters for a SwaggerView:

class FooView(SwaggerView):
    parameters = Foo.Schema() # will fail because Foo.Schema is not an iterable

When passing in a callable, there seems to be another error in the OpenAPIConverter not implementing a schema_name_resolver in 0.9.3:

class FooView(SwaggerView):
    parameters = Foo.Schema # TypeError: 'NoneType' object is not callable

This error is related to #310 and a fix is in the works.

To temporarily move forward, either provide a schema_name_resolver in flasgger/marshmallow_apispec or update to a prerelease containing the fix, such as flasgger==0.9.4dev0

Then, this should work

class FooView(SwaggerView):
    parameters = Foo.Schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.