From f034b4665a24e07a2885ed6807c2ff4b6b6162ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szczepan=20Cie=C5=9Blik?= Date: Mon, 2 Nov 2015 23:09:53 +0100 Subject: [PATCH] Fixed schema generation for lists. --- jsonmodels/parsers.py | 2 +- tests/fixtures/schema_circular2.json | 50 ++++++++++++++++++++++++++++ tests/test_circular_references.py | 8 ++--- 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/schema_circular2.json diff --git a/jsonmodels/parsers.py b/jsonmodels/parsers.py index 4f43c4c..ff41e48 100644 --- a/jsonmodels/parsers.py +++ b/jsonmodels/parsers.py @@ -59,7 +59,7 @@ def build_json_schema(value, parent_builder=None): def build_json_schema_object(cls, parent_builder=None): builder = ObjectBuilder(cls, parent_builder) - if builder.is_definition: + if builder.count_type(builder.type) > 1: return builder for name, field in cls.iterate_over_fields(): if isinstance(field, fields.EmbeddedField): diff --git a/tests/fixtures/schema_circular2.json b/tests/fixtures/schema_circular2.json new file mode 100644 index 0000000..4c47e7b --- /dev/null +++ b/tests/fixtures/schema_circular2.json @@ -0,0 +1,50 @@ +{ + "additionalProperties": false, + "definitions": { + "tests_test_circular_references_directory": { + "additionalProperties": false, + "properties": { + "children": { + "items": { + "oneOf": [ + "#/definitions/tests_test_circular_references_directory", + "#/definitions/tests_test_circular_references_file" + ] + }, + "type": "list" + }, + "name": { + "type": "string" + } + }, + "type": "object" + }, + "tests_test_circular_references_file": { + "additionalProperties": false, + "properties": { + "name": { + "type": "string" + }, + "size": { + "type": "float" + } + }, + "type": "object" + } + }, + "properties": { + "children": { + "items": { + "oneOf": [ + "#/definitions/tests_test_circular_references_directory", + "#/definitions/tests_test_circular_references_file" + ] + }, + "type": "list" + }, + "name": { + "type": "string" + } + }, + "type": "object" +} diff --git a/tests/test_circular_references.py b/tests/test_circular_references.py index a6a8e56..ecd6966 100644 --- a/tests/test_circular_references.py +++ b/tests/test_circular_references.py @@ -1,5 +1,3 @@ -import pytest - from jsonmodels import models, fields from jsonmodels.utilities import compare_schemas @@ -43,6 +41,8 @@ class Filesystem(models.Base): children = fields.ListField([Directory, File]) -@pytest.mark.xfail def test_generate_circular_schema2(): - Filesystem.to_json_schema() + schema = Filesystem.to_json_schema() + + pattern = get_fixture('schema_circular2.json') + assert compare_schemas(pattern, schema) is True