Skip to content

Commit

Permalink
Merge pull request #81 from fuhrysteve/implement-field-subclass-check
Browse files Browse the repository at this point in the history
[build] Implement field subclass check
  • Loading branch information
fuhrysteve authored Aug 10, 2019
2 parents 87a66c1 + ce7a9ef commit ee7fcaa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ before_install:

install:
- pip install -r requirements-tox.txt
- pip install tox-travis coveralls
- pip install -U tox-travis coveralls
script: make tox
after_success: coveralls
14 changes: 9 additions & 5 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,26 @@ def _from_python_type(self, obj, field, pytype):
json_schema["items"] = self._get_schema_for_field(obj, list_inner(field))
return json_schema

def _get_pytype(self, field, mapping):
"""Get pytype based on field subclass"""
for map_class, pytype in mapping.items():
if issubclass(field.__class__, map_class):
return pytype
raise UnsupportedValueError("unsupported field type %s" % field)

def _get_schema_for_field(self, obj, field):
"""Get schema and validators for field."""
mapping = self._get_default_mapping(obj)
if hasattr(field, "_jsonschema_type_mapping"):
schema = field._jsonschema_type_mapping()
elif "_jsonschema_type_mapping" in field.metadata:
schema = field.metadata["_jsonschema_type_mapping"]
elif field.__class__ in mapping:
pytype = mapping[field.__class__]
else:
pytype = self._get_pytype(field, mapping)
if isinstance(pytype, basestring):
schema = getattr(self, pytype)(obj, field)
else:
schema = self._from_python_type(obj, field, pytype)
else:
raise UnsupportedValueError("unsupported field type %s" % field)

# Apply any and all validators that field may have
for validator in field.validators:
if validator.__class__ in FIELD_VALIDATORS:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ class UserSchema(Schema):
}


def test_field_subclass():
"""JSON schema generation should not fail on sublcass marshmallow field."""

class CustomField(fields.Field):
pass

class TestSchema(Schema):
myfield = CustomField()

schema = TestSchema()
with pytest.raises(UnsupportedValueError):
_ = validate_and_dump(schema)


def test_readonly():
class TestSchema(Schema):
id = fields.Integer(required=True)
Expand Down

0 comments on commit ee7fcaa

Please sign in to comment.