Skip to content

Commit

Permalink
Merge pull request fuhrysteve#60 from fuhrysteve/removing_empty
Browse files Browse the repository at this point in the history
Combining fuhrysteve#57 with a couple other tweaks
  • Loading branch information
fuhrysteve authored Jun 6, 2018
2 parents b81aeaf + 1ed3fe4 commit 17137e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion marshmallow_jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
from .base import JSONSchema

__all__ = (
'JSONSchema'
'JSONSchema',
)
9 changes: 5 additions & 4 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from .validation import handle_length, handle_one_of, handle_range


__all__ = ['JSONSchema']
__all__ = (
'JSONSchema',
)


TYPE_MAP = {
Expand Down Expand Up @@ -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()):
Expand All @@ -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."""
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

0 comments on commit 17137e3

Please sign in to comment.