Skip to content

Commit

Permalink
Merge pull request #39 from frol/fix-nested-field2property
Browse files Browse the repository at this point in the history
Fixed #37: Introspect fields of an instance (if passed) instead of falling back to a class
  • Loading branch information
sloria committed Nov 27, 2015
2 parents 1095380 + bd0c847 commit f6c4745
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
30 changes: 25 additions & 5 deletions apispec/ext/marshmallow/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def field2property(field, spec=None, use_refs=True, dump=True):
elif spec:
schema = resolve_schema_dict(spec, field.schema)
else:
schema = schema2jsonschema(field.schema.__class__)
schema = schema2jsonschema(field.schema)
if field.many:
ret['type'] = 'array'
ret['items'] = schema
Expand All @@ -113,15 +113,25 @@ def field2property(field, spec=None, use_refs=True, dump=True):
return ret


def schema2parameters(schema_cls, **kwargs):
def schema2parameters(schema, **kwargs):
"""Return an array of Swagger parameters given a given marshmallow
:class:`Schema <marshmallow.Schema>`. 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/wordnik/swagger-spec/blob/master/versions/2.0.md#parameterObject
"""
fields = schema_cls._declared_fields
if hasattr(schema, 'fields'):
fields = schema.fields
elif hasattr(schema, '_declared_fields'):
fields = schema._declared_fields
else:
raise ValueError("Schema %r doesn't have either `fields` or `_declared_fields`")

# Prevent circular import
from apispec.ext.marshmallow import resolve_schema_cls
schema_cls = resolve_schema_cls(schema)

return fields2parameters(fields, schema_cls, **kwargs)


Expand Down Expand Up @@ -223,8 +233,18 @@ def property2parameter(prop, name='body', required=False, multiple=False, locati
return ret


def schema2jsonschema(schema_cls, spec=None, use_refs=True, dump=True):
fields = schema_cls._declared_fields
def schema2jsonschema(schema, spec=None, use_refs=True, dump=True):
if hasattr(schema, 'fields'):
fields = schema.fields
elif hasattr(schema, '_declared_fields'):
fields = schema._declared_fields
else:
raise ValueError("Schema %r doesn't have either `fields` or `_declared_fields`")

# Prevent circular import
from apispec.ext.marshmallow import resolve_schema_cls
schema_cls = resolve_schema_cls(schema)

return fields2jsonschema(fields, schema_cls, spec=spec, use_refs=use_refs, dump=dump)


Expand Down
13 changes: 13 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,19 @@ def test_schema2jsonschema_with_nested_fields(self):
props = res['properties']
assert props['category']['items'] == swagger.schema2jsonschema(CategorySchema)

def test_schema2jsonschema_with_nested_fields_with_adhoc_changes(self):
category_schema = CategorySchema()
category_schema.fields['id'].required = True

class PetSchema(Schema):
category = fields.Nested(category_schema, many=True, ref='#/definitions/Category')
name = fields.Str()

res = swagger.schema2jsonschema(PetSchema, use_refs=False)
props = res['properties']
assert props['category']['items'] == swagger.schema2jsonschema(category_schema)
assert set(props['category']['items']['required']) == {'id', 'name'}


spec = APISpec(
title='Pets',
Expand Down

0 comments on commit f6c4745

Please sign in to comment.