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

Skip title if either fields are None #93

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def get_required(self, obj):

def _from_python_type(self, obj, field, pytype):
"""Get schema definition from python type."""
json_schema = {"title": field.attribute or field.name}
json_schema = {}

if field.attribute or field.name:
json_schema["title"] = field.attribute or field.name

for key, val in PY_TO_JSON_TYPES_MAP[pytype].items():
json_schema[key] = val
Expand Down
17 changes: 17 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ class TestSchema(Schema):
assert props["foo"]["maximum"] == 3


@pytest.mark.skipif(MARSHMALLOW_3, reason="fixed in marshmallow 3")
def test_doubly_nested_skipped_titles():
class TestSchema(Schema):
foo = fields.List(fields.List(fields.Integer()))

schema = TestSchema()

dumped = validate_and_dump(schema)

props = dumped["definitions"]["TestSchema"]["properties"]

assert props["foo"]["title"] == "foo"
assert props["foo"]["items"]["title"] == "foo"
with pytest.raises(KeyError):
props["foo"]["items"]["items"]["title"]


@pytest.mark.skipif(MARSHMALLOW_2, reason="marshmallow 3 only")
def test_range_marshmallow_3():
class TestSchema(Schema):
Expand Down