Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix marshmallow partial loading should skip JSON schema required fields #113

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']