diff --git a/marshmallow_jsonschema/__init__.py b/marshmallow_jsonschema/__init__.py index 8f3b233..9995cd8 100644 --- a/marshmallow_jsonschema/__init__.py +++ b/marshmallow_jsonschema/__init__.py @@ -6,5 +6,5 @@ from .base import JSONSchema __all__ = ( - 'JSONSchema' + 'JSONSchema', ) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index 9314088..8c74aaa 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -10,7 +10,9 @@ from .validation import handle_length, handle_one_of, handle_range -__all__ = ['JSONSchema'] +__all__ = ( + 'JSONSchema', +) TYPE_MAP = { @@ -105,7 +107,6 @@ def _get_default_mapping(self, obj): def get_properties(self, obj): """Fill out properties field.""" - mapping = self._get_default_mapping(obj) properties = {} for field_name, field in sorted(obj.fields.items()): @@ -122,7 +123,7 @@ def get_required(self, obj): if field.required: required.append(field.name) - return required + return required or missing def _from_python_type(self, obj, field, pytype): """Get schema definition from python type.""" @@ -242,6 +243,6 @@ def wrap(self, data): self._nested_schema_classes[name] = data root = { 'definitions': self._nested_schema_classes, - '$ref': '#/definitions/{}'.format(name) + '$ref': '#/definitions/{name}'.format(name=name) } return root diff --git a/tests/test_dump.py b/tests/test_dump.py index b099516..740224c 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -440,3 +440,13 @@ class TestSchema(Schema): 'type': 'string', 'description': 'Directly on the field!', } + + +def test_required_excluded_when_empty(): + + class TestSchema(Schema): + optional_value = fields.String() + schema = TestSchema() + json_schema = JSONSchema() + dumped = json_schema.dump(schema).data + assert 'required' not in dumped['definitions']['TestSchema']