Skip to content

Commit

Permalink
support for strings in Nested columns
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhrysteve committed Aug 6, 2016
1 parent c6c148f commit 6f51771
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ check:
coverage:
coverage erase
coverage run --source marshmallow_jsonschema -m py.test -v
coverage report -m

pypitest:
python setup.py sdist upload -r pypitest
Expand Down
9 changes: 7 additions & 2 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import decimal

from marshmallow import fields, missing, Schema, validate
from marshmallow.compat import text_type, binary_type
from marshmallow.class_registry import get_class
from marshmallow.compat import text_type, binary_type, basestring

from .validation import handle_length, handle_one_of, handle_range

Expand Down Expand Up @@ -144,7 +145,11 @@ def _from_python_type(cls, field, pytype):

@classmethod
def _from_nested_schema(cls, field):
schema = cls().dump(field.nested()).data
if isinstance(field.nested, basestring):
nested = get_class(field.nested)
else:
nested = field.nested
schema = cls().dump(nested()).data

if field.metadata.get('metadata', {}).get('description'):
schema['description'] = (
Expand Down
17 changes: 17 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,30 @@ class TestNestedSchema(Schema):
assert nested_dmp['title'] == 'Title1'


def test_nested_string_to_cls():
class TestSchema(Schema):
foo = fields.Integer(required=True)

class TestNestedSchema(Schema):
foo2 = fields.Integer(required=True)
nested = fields.Nested('TestSchema')
schema = TestNestedSchema()
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data
_validate_schema(dumped)
nested_json = dumped['properties']['nested']
assert nested_json['properties']['foo']['format'] == 'integer'
assert nested_json['type'] == 'object'


def test_one_of_validator():
schema = UserSchema()
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data
_validate_schema(dumped)
assert dumped['properties']['sex']['enum'] == ['male', 'female']


def test_range_validator():
schema = Address()
json_schema = JSONSchema()
Expand Down

0 comments on commit 6f51771

Please sign in to comment.