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 · 6 comments

Comments

Projects
None yet
2 participants
@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

This comment has been minimized.

Show comment
Hide comment
@rochacbruno

rochacbruno Apr 24, 2017

Owner

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.

Owner

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.

@wobeng

This comment has been minimized.

Show comment
Hide comment
@wobeng

wobeng 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)

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)
@rochacbruno

This comment has been minimized.

Show comment
Hide comment
@rochacbruno

rochacbruno Apr 24, 2017

Owner
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.

Owner

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.

@wobeng

This comment has been minimized.

Show comment
Hide comment
@wobeng

wobeng Apr 24, 2017

you are the man!

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

wobeng commented Apr 24, 2017

you are the man!

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

@rochacbruno

This comment has been minimized.

Show comment
Hide comment
@rochacbruno

rochacbruno Apr 24, 2017

Owner

@wobeng the same way

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

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'
        """
@wobeng

This comment has been minimized.

Show comment
Hide comment
@wobeng

wobeng 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)

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment