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

Add support for allow_none and test #106

Merged
merged 2 commits into from
Feb 25, 2020
Merged
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
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