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

Body parameters renamed "body" #184

Closed
mathewmarcus opened this issue Mar 5, 2018 · 1 comment

Comments

@mathewmarcus
Copy link
Contributor

commented Mar 5, 2018

When adding a body parameter, i.e. a parameter where in="body", via a docstring, the parameter is automatically renamed to "body".

For example:

from apispec import APISpec
from flask import Flask
from marshmallow import Schema, fields
import json

spec = APISpec(
    title='Swagger Petstore',
    version='1.0.0',
    plugins=[
        'apispec.ext.flask',
        'apispec.ext.marshmallow',
    ],
)

app = Flask(__name__)


class CategorySchema(Schema):
    id = fields.Int()
    name = fields.Str(required=True)


class PetSchema(Schema):
    category = fields.Nested(CategorySchema, many=True)
    name = fields.Str()


@app.route('/random', methods=['POST'])
def random_pet():
    """A cute furry animal endpoint.
    ---
    post:
        description: Get a random pet
        parameters:
            - name: pet
              required: true
              in: body
              schema: PetSchema
    """
    pass


with app.test_request_context():
    spec.add_path(view=random_pet)

print(json.dumps((spec.to_dict())))

this yields the following swagger

{
    "info": {
        "title": "Swagger Petstore",
        "version": "1.0.0"
    },
    "paths": {
        "/random": {
            "post": {
                "description": "Get a random pet",
                "parameters": [
                    {
                        "in": "body",
                        "required": false,
                        "name": "body",
                        "schema": {
                            "type": "object",
                            "properties": {
                                "category": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "name": {
                                                "type": "string"
                                            },
                                            "id": {
                                                "type": "integer",
                                                "format": "int32"
                                            }
                                        },
                                        "required": [
                                            "name"
                                        ]
                                    }
                                },
                                "name": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                ]
            }
        }
    },
    "tags": [],
    "swagger": "2.0",
    "definitions": {},
    "parameters": {}
}

Essentially, even though the parameter is explicity named pet, it is automatically renamed body.

This appears to stem from the fact that when calling schema2parameters,
resolve_parameters does not pass along the name kwarg. As a result, when schema2parameters calls field2parameters, name automatically becomes "body", because of the kwarg default value.

@mathewmarcus mathewmarcus changed the title Body parameters renamed "in" Body parameters renamed "body" Mar 5, 2018

@mathewmarcus

This comment has been minimized.

Copy link
Contributor Author

commented Mar 5, 2018

Just saw this is probably related to #168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.