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']