Skip to content

Commit

Permalink
Merge pull request #106 from avilaton/feature/account-allow-none
Browse files Browse the repository at this point in the history
Add support for allow_none and test
  • Loading branch information
fuhrysteve authored Feb 25, 2020
2 parents 4505a0b + 4c09a86 commit 47418df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def _from_python_type(self, obj, field, pytype):
if field.default is not missing:
json_schema["default"] = field.default

if field.allow_none:
previous_type = json_schema["type"]
json_schema["type"] = [previous_type, "null"]

# NOTE: doubled up to maintain backwards compatibility
metadata = field.metadata.get("metadata", {})
metadata.update(field.metadata)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ class TestSchema(Schema):
}


def test_allow_none():
"""A field with allow_none set to True should have type null as additional."""

class TestSchema(Schema):
id = fields.Integer(required=True)
readonly_fld = fields.String(allow_none=True)

schema = TestSchema()

dumped = validate_and_dump(schema)

assert dumped["definitions"]["TestSchema"]["properties"]["readonly_fld"] == {
"title": "readonly_fld",
"type": ["string", "null"],
}


def test_dumps_iterable_enums():
mapping = {"a": 0, "b": 1, "c": 2}

Expand Down

0 comments on commit 47418df

Please sign in to comment.