-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for title and description metadata
- Refactor _resolve_additional_properties to reuse parts of it for title and description support - Add _get_schema_metadata that retrieves the 3 currently supported metadata options on a schema - Rename 'cls' params to something less confusing as 'cls' is a python keyword - Add documentation for title, description and additional_properties - Move addtional_properties tests to a new forlder "schema_metadata_tests" so they can be gathered in a common place Closes #41
- Loading branch information
Luke Marlin
committed
Oct 30, 2019
1 parent
3c579c7
commit 8f38e0f
Showing
7 changed files
with
295 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pytest | ||
from marshmallow import Schema, fields | ||
|
||
from marshmallow_jsonschema import UnsupportedValueError, JSONSchema | ||
from .. import validate_and_dump | ||
|
||
|
||
class TestDescriptionSchemaMetadata: | ||
|
||
def test_description_default(self): | ||
class TestSchema(Schema): | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
definition_to_test = dumped["definitions"]["TestSchema"] | ||
assert "description" not in definition_to_test | ||
|
||
@pytest.mark.parametrize("description_value", ("desc1", "desc2")) | ||
def test_description_from_meta(self, description_value): | ||
class TestSchema(Schema): | ||
class Meta: | ||
description = description_value | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
|
||
assert ( | ||
dumped["definitions"]["TestSchema"]["description"] | ||
== description_value | ||
) | ||
|
||
@pytest.mark.parametrize("invalid_value", [ | ||
True, | ||
4, | ||
{} | ||
]) | ||
def test_description_invalid_value(self, invalid_value): | ||
class TestSchema(Schema): | ||
class Meta: | ||
description = invalid_value | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
json_schema = JSONSchema() | ||
|
||
with pytest.raises(UnsupportedValueError): | ||
json_schema.dump(schema) | ||
|
||
|
||
class TestDescriptionNestedSchemaMetadata: | ||
|
||
def test_description_default(self): | ||
class TestNestedSchema(Schema): | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
definition_to_test = dumped["definitions"]["TestNestedSchema"] | ||
assert "description" not in definition_to_test | ||
|
||
@pytest.mark.parametrize("description_value", ("desc1", "desc2")) | ||
def test_description_from_meta(self, description_value): | ||
class TestNestedSchema(Schema): | ||
class Meta: | ||
description = description_value | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
|
||
assert ( | ||
dumped["definitions"]["TestNestedSchema"]["description"] | ||
== description_value | ||
) | ||
|
||
@pytest.mark.parametrize("invalid_value", [ | ||
True, | ||
4, | ||
{} | ||
]) | ||
def test_description_invalid_value(self, invalid_value): | ||
class TestNestedSchema(Schema): | ||
class Meta: | ||
description = invalid_value | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
json_schema = JSONSchema() | ||
|
||
with pytest.raises(UnsupportedValueError): | ||
json_schema.dump(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pytest | ||
from marshmallow import Schema, fields | ||
|
||
from marshmallow_jsonschema import UnsupportedValueError, JSONSchema | ||
from .. import validate_and_dump | ||
|
||
|
||
class TestDescriptionSchemaMetadata: | ||
|
||
def test_title_default(self): | ||
class TestSchema(Schema): | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
definition_to_test = dumped["definitions"]["TestSchema"] | ||
assert "title" not in definition_to_test | ||
|
||
@pytest.mark.parametrize("title_value", ("desc1", "desc2")) | ||
def test_title_from_meta(self, title_value): | ||
class TestSchema(Schema): | ||
class Meta: | ||
title = title_value | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
|
||
assert ( | ||
dumped["definitions"]["TestSchema"]["title"] | ||
== title_value | ||
) | ||
|
||
@pytest.mark.parametrize("invalid_value", [ | ||
True, | ||
4, | ||
{} | ||
]) | ||
def test_title_invalid_value(self, invalid_value): | ||
class TestSchema(Schema): | ||
class Meta: | ||
title = invalid_value | ||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
json_schema = JSONSchema() | ||
|
||
with pytest.raises(UnsupportedValueError): | ||
json_schema.dump(schema) | ||
|
||
|
||
class TestDescriptionNestedSchemaMetadata: | ||
|
||
def test_title_default(self): | ||
class TestNestedSchema(Schema): | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
definition_to_test = dumped["definitions"]["TestNestedSchema"] | ||
assert "title" not in definition_to_test | ||
|
||
@pytest.mark.parametrize("title_value", ("desc1", "desc2")) | ||
def test_title_from_meta(self, title_value): | ||
class TestNestedSchema(Schema): | ||
class Meta: | ||
title = title_value | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
|
||
assert ( | ||
dumped["definitions"]["TestNestedSchema"]["title"] | ||
== title_value | ||
) | ||
|
||
@pytest.mark.parametrize("invalid_value", [ | ||
True, | ||
4, | ||
{} | ||
]) | ||
def test_title_invalid_value(self, invalid_value): | ||
class TestNestedSchema(Schema): | ||
class Meta: | ||
title = invalid_value | ||
foo = fields.Integer() | ||
|
||
class TestSchema(Schema): | ||
nested = fields.Nested(TestNestedSchema()) | ||
|
||
schema = TestSchema() | ||
json_schema = JSONSchema() | ||
|
||
with pytest.raises(UnsupportedValueError): | ||
json_schema.dump(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
from marshmallow import Schema, fields | ||
|
||
from marshmallow_jsonschema import JSONSchema | ||
from .. import validate_and_dump | ||
|
||
|
||
@pytest.mark.parametrize("unchecked_value", (False, True)) | ||
def test_unhandled_metas_do_not_pollute_schema(unchecked_value): | ||
|
||
class TestSchema(Schema): | ||
class Meta: | ||
unhandled = unchecked_value | ||
|
||
foo = fields.Integer() | ||
|
||
schema = TestSchema() | ||
dumped = validate_and_dump(schema) | ||
|
||
definition_to_check = dumped["definitions"]["TestSchema"] | ||
|
||
assert "unhandled" not in definition_to_check |