Skip to content

Commit

Permalink
Fix marshmallow partial loading should skip JSON schema required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lehainam-dev committed May 19, 2020
1 parent 3f62e63 commit 6a47e06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ def get_properties(self, obj):

def get_required(self, obj):
"""Fill out required field."""
if obj.partial == True:
return []

required = []
partial = obj.partial or []

for field_name, field in sorted(obj.fields.items()):
if field.required:
if field.required and field.name not in partial:
required.append(field.name)

return required or missing
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,17 @@ class Meta:
properties_names = [k for k in keys]

assert properties_names == ["d", "c", "a"]

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

json_schema = JSONSchema()
schema = TestSchema(partial=True)
dumped = json_schema.dump(schema)
assert dumped['definitions']['TestSchema']['required'] == []

schema = TestSchema(partial=('foo', ))
dumped = json_schema.dump(schema)
assert dumped['definitions']['TestSchema']['required'] == ['id']

0 comments on commit 6a47e06

Please sign in to comment.