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

Does SwaggerView allow multiple methods in the class? #92

Open
wobeng opened this issue Apr 24, 2017 · 7 comments
Open

Does SwaggerView allow multiple methods in the class? #92

wobeng opened this issue Apr 24, 2017 · 7 comments

Comments

@wobeng
Copy link

@wobeng wobeng commented Apr 24, 2017

Does SwaggerView allow multiple methods in the class?

Regarding https://github.com/rochacbruno/flasgger/blob/master/examples/marshmallow_apispec.py

I only see one method at a time

@rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Apr 24, 2017

Yes it supports as it is just a class inheriting form MethodView, and you can use the docstring on each method to overriide the class values.

Loading

@wobeng
Copy link
Author

@wobeng wobeng commented Apr 24, 2017

Hi rochacbruno

Thanks for your response. How do I use marshmallow schema in other methods if I have a different schema for each method? For example in the code below, how do I specify a different schema for "get" method and a different schema for "post"

from flask import Flask, jsonify, request

from flasgger import Schema, Swagger, SwaggerView, fields

app = Flask(__name__)
app.config['SWAGGER'] = {
    "title": "API using Marshmallow",
    "uiversion": 2
}

Swagger(app)


class User(Schema):
    username = fields.Str(required=True)
    age = fields.Int(required=True, min=18)
    tags = fields.List(fields.Str())


class UserPostView(SwaggerView):

    parameters = User
    responses = {
        200: {
            'description': 'A single user',
            'schema': User
        }
    }
    tags = ['users']
    summary = "Will be overwritten by first line of docstring"
    description = "will be overwritten by otehr lines"

    def post(self):
        """
        A simple post
        Do it
        ---
        # This value overwrites the attributes above
        """
        return jsonify(request.json)

    def get(self):
        """
        A simple get
        Do it
        ---
        # This value overwrites the attributes above
        """
        return {}
app.add_url_rule(
    '/user',
    view_func=UserPostView.as_view('user'),
    methods=['POST','GET']
)

if __name__ == "__main__":
    app.run(host="0.0.0.0",debug=True)

Loading

@rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Apr 24, 2017

class UserPostView(SwaggerView):
   ...
   definitions = {'User': User, 'Another': Another}

    def post(self):
        """
        A simple post
        Do it
        ---
        responses:
          200:
            schema: 
              $ref: '#/definitions/User'
        """
        return jsonify(request.json)

    def get(self):
        """
        A simple get
        Do it
        ---
        responses:
          200:
            schema: 
              $ref: '#/definitions/Another'
        """
        return {}

right now that is the only way, we can think in a better approach and implement it.

Loading

@wobeng
Copy link
Author

@wobeng wobeng commented Apr 24, 2017

you are the man!

one last thing...how will I do definitions for parameters?

Loading

@rochacbruno
Copy link
Member

@rochacbruno rochacbruno commented Apr 24, 2017

@wobeng the same way

    def get(self):
        """
        A simple get
        Do it
        ---
        parameters:
            in: query
            ....
        responses:
          200:
            schema: 
              $ref: '#/definitions/Another'
        """

Loading

@wobeng
Copy link
Author

@wobeng wobeng commented Apr 24, 2017

Sorry for not clarifying. I mean something like this

from flask import Flask, jsonify, request

from flasgger import Schema, Swagger, SwaggerView, fields

app = Flask(__name__)
app.config['SWAGGER'] = {
    "title": "API using Marshmallow",
    "uiversion": 2
}

Swagger(app)


class User(Schema):
    username = fields.Str(required=True)
    age = fields.Int(required=True, min=18)
    tags = fields.List(fields.Str())

class Another(Schema):
    username2 = fields.Str(required=True)
    age2 = fields.Int(required=True, min=18)
    tags2 = fields.List(fields.Str())

parameters = [
    {
      "name": "palette",
      "in": "path",
      "type": "string",
      "enum": [
        "all",
        "rgb",
        "cmyk"
      ],
      "required": True,
      "default": "all"
    }
  ]

parameters2 = [
    {
      "name": "palette",
      "in": "path",
      "type": "string",
      "enum": [
        "all",
        "rgb",
        "cmyk"
      ],
      "required": True,
      "default": "all"
    }
  ]

class UserPostView(SwaggerView):

    definitions = {'User': User, 'Another': Another, "parameters" : parameters, "parameters2" : parameters2}

    def post(self):
        """
        A simple post
        Do it
        ---
        parameters:
            schema:
              $ref: '#/definitions/parameters'
        responses:
          200:
            schema:
              $ref: '#/definitions/User'
        """
        return jsonify(request.json)

    def get(self):
        """
        A simple get
        Do it
        ---
        parameters:
            schema:
              $ref: '#/definitions/parameters2'
        responses:
          200:
            schema:
              $ref: '#/definitions/Another'
        """
        return {}

app.add_url_rule(
    '/user',
    view_func=UserPostView.as_view('user'),
    methods=['POST','GET']
)

if __name__ == "__main__":
    app.run(host="0.0.0.0",debug=True)

Loading

@ongopongo
Copy link

@ongopongo ongopongo commented Aug 26, 2020

how will I do definitions for parameters?

Here is how I have referenced Marshmallow schemas from within the parameters part, and from within the responses part, of a SwaggerView method's docstring YAML:

class FooEndpoint(SwaggerView):

    definitions = {
        "Foo":        Foo,        # note: Foo        is a Marshmallow schema class
        "FooPutBody": FooPutBody  # note: FooPutBody is a Marshmallow schema class
    }

    # ...

    def put(self, foo_id):
        """
        Update a Foo
        ---
        parameters:
            - in: path
              name: foo_id
              description: The `_id` of the `Foo` you want to update
              required: true
              type: uuid
            - in: body
              name: properties
              description: Properties you want to update of the `Foo`
              required: true
              schema: { $ref: '#/definitions/FooPutBody' }
        responses:
            200:
                description: The updated `Foo`
                schema: { $ref: '#/definitions/Foo' }
        """
        # function body...

Loading

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
3 participants