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

Marshmallow-schema'd body parameters lose 'required' and 'description' field spec #168

Closed
dlopuch opened this issue Nov 9, 2017 · 1 comment

Comments

@dlopuch
Copy link

commented Nov 9, 2017

Consider this code:

from marshmallow import Schema, fields
from apispec import APISpec

spec = APISpec(
        title='bug',
        version="0.0.1",
        plugins=(
            'apispec.ext.marshmallow',
        )
    )

class SomeBodyParam(Schema):
    name = fields.String(required=True)
    city = fields.String()
    age = fields.Integer()

spec.add_path(
    'foo/',
    {
        'post': {
            'parameters': [
                { 'name': 'some body param',
                  'in': 'body',
                  'schema': SomeBodyParam(),
                  'description': 'Description disapears and so does required',
                  'required': True
                  }
            ]
        }
    }
)

spec.to_dict()

The output is:

{'definitions': {},
 'info': {'title': 'bug', 'version': '0.0.1'},
 'parameters': {},
 'paths': OrderedDict([('foo/',
               {'post': {'parameters': [{'in': 'body',
                   'name': 'body',
                   'required': False,
                   'schema': {'properties': {'name': {'type': 'string'}, 'city': {'type': 'string'}, 'age': {'type': 'integer', 'format': 'int32'}},
                    'required': ['name'],
                    'type': 'object'}}]}})]),
 'swagger': '2.0',
 'tags': []}

Bug:

  • The required field switched from True to False
  • The description field got lost

The name parameter also gets renamed, but I believe this is per-spec.

I believe the cause is here: https://github.com/marshmallow-code/apispec/blob/0.27.0/apispec/ext/marshmallow/__init__.py#L148

Adding a , required=parameter['required'] if 'required' in parameter else False ought to fix the required bit. Do something similar for description, but need to add it to swagger.py:fields2parameters() signature and include it in the dict constructor in there.

That sound like a reasonable approach?

@yoichi

This comment has been minimized.

Copy link
Collaborator

commented Nov 10, 2017

@dlopuch I am grateful for the clear bug report.

I am worried that the current implementation of fields2parameters is already complicated,
but I have no idea about other ways. Could you try the fix in that way?

Regards,

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.