From 6a47e065d4a836819dd409366d33f454bb33c78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20R=C4=83ng?= Date: Tue, 19 May 2020 15:59:03 +0700 Subject: [PATCH] Fix marshmallow partial loading should skip JSON schema required fields --- marshmallow_jsonschema/base.py | 6 +++++- tests/test_dump.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index 89614fc..25a5cce 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -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 diff --git a/tests/test_dump.py b/tests/test_dump.py index 5784b32..6b1d23e 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -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']