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

schema2parameters - body + header #75

Closed
shaicantor opened this issue May 18, 2016 · 0 comments

Comments

@shaicantor
Copy link

commented May 18, 2016

Hey,

I have a schema and the default_in = body, but one of the fields has a headers location.
schema2parameters inserts the header field inside the body instead of returning one body param and one header param.

What do I do?

class AccountsSchema(Schema):
    user_id = fields.Int(required=True, location="headers")
    name = fields.Str(required=True, validate=validate.Length(min=1, max=255), location="json")
    status = fields.Str(required=True, validate=validate.OneOf(choices=["Active", "Paused"]), location="json")
def fields2parameters(fields, schema=None, spec=None, use_refs=True, dump=True,
                      default_in='body', name='body', required=False):
    """Return an array of OpenAPI parameters given a mapping between field names and
    :class:`Field <marshmallow.Field>` objects. If `default_in` is "body", then return an array
    of a single parameter; else return an array of a parameter for each included field in
    the :class:`Schema <marshmallow.Schema>`.

    https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
    """
    if default_in == 'body':
        if schema is not None:
            # Prevent circular import
            from apispec.ext.marshmallow import resolve_schema_dict
            prop = resolve_schema_dict(spec, schema, dump=dump)
        else:
            prop = fields2jsonschema(fields, spec=spec, use_refs=use_refs, dump=dump)

        return [{
            'in': default_in,
            'required': required,
            'name': name,
            'schema': prop,
        }]

    assert not getattr(schema, 'many', False), \
        "Schemas with many=True are only supported for 'json' location (aka 'in: body')"

    exclude_fields = getattr(getattr(schema, 'Meta', None), 'exclude', [])

    return [
        field2parameter(
            field_obj,
            name=_observed_name(field_obj, field_name),
            spec=spec,
            use_refs=use_refs,
            dump=dump,
            default_in=default_in
        )
            for field_name, field_obj in iteritems(fields)
            if (
                (field_name not in exclude_fields)
                and not (field_obj.dump_only and not dump)
            )
    ]

yoichi added a commit to yoichi/apispec that referenced this issue Sep 10, 2017

Handle multple fields with location='json' correctly.
Fixes marshmallow-code#75
* Merge results from field2parameter.
* Modify input data for test_field_required since required
  fields in body are treated differently.

yoichi added a commit to yoichi/apispec that referenced this issue Sep 10, 2017

Handle multiple fields with location='json' correctly.
Fixes marshmallow-code#75
* Merge results from field2parameter.
* Modify input data for test_field_required since required
  fields in body are treated differently.

@sloria sloria closed this in #153 Sep 27, 2017

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