Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanovich committed Nov 23, 2015
1 parent d825a89 commit e92c234
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jsl/fields/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def _get_definitions_and_schema(self, role=DEFAULT_ROLE, res_scope=EMPTY_SCOPE,
new_role = role
document_definitions, document_schema = document_cls.get_definitions_and_schema(
role=new_role, res_scope=res_scope, ordered=ordered, ref_documents=ref_documents)
if self.as_ref:
if self.as_ref and not document_cls.is_recursive(role=new_role):
document_definitions[definition_id] = document_schema
return document_definitions, res_scope.create_ref(definition_id)
else:
Expand Down
47 changes: 44 additions & 3 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Main(Document):

class A(Document):
name = StringField()
a = DocumentField('A')
a = DocumentField('A', as_ref=True)

class B(Document):
c = DocumentField('C')
Expand Down Expand Up @@ -307,11 +307,11 @@ class C(Document):

def test_recursive_definitions_4():
class Main(Document):
a = DocumentField('A')
a = DocumentField('A', as_ref=True)

class A(Document):
name = StringField()
b = DocumentField('B')
b = DocumentField('B', as_ref=True)

class B(Document):
c = DocumentField(Main)
Expand Down Expand Up @@ -401,6 +401,47 @@ class Options(object):
})


def test_recursive_definitions_6():
# regression test for https://github.com/aromanovich/jsl/issues/16

class Children(Document):
class Options(object):
definition_id = 'children'
children = OneOfField([
DocumentField('A',),
])

class A(Document):
class Options(object):
definition_id = 'a'
derived_from = DocumentField(Children, as_ref=True)

assert s(A.get_schema()) == s({
'$schema': 'http://json-schema.org/draft-04/schema#',
'definitions': {
'a': {
'type': 'object',
'properties': {
'derived_from': {
'$ref': '#/definitions/children',
},
},
'additionalProperties': False,
},
'children': {
'type': 'object',
'properties': {
'children': {
'oneOf': [{'$ref': '#/definitions/a'}],
},
},
'additionalProperties': False,
},
},
'$ref': '#/definitions/a',
})


def test_resolve_field():
class X(Document):
with Scope('role_1') as s_1:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,14 @@ def test_document_field():
attrs = {
'get_definitions_and_schema.return_value': ({}, expected_schema),
'get_definition_id.return_value': 'document.Document',
'is_recursive.return_value': False,
}
document_cls_mock.configure_mock(**attrs)

f = fields.DocumentField(document_cls_mock)
definitions, schema = f.get_definitions_and_schema()
assert schema == expected_schema
assert not definitions

definitions, schema = f.get_definitions_and_schema(ref_documents=set([document_cls_mock]))
assert s(schema) == {'$ref': '#/definitions/document.Document'}
Expand All @@ -330,6 +332,19 @@ def test_document_field():
assert definitions == {'document.Document': expected_schema}
assert s(schema) == {'$ref': '#/definitions/document.Document'}

attrs = {
'get_definitions_and_schema.return_value': ({}, expected_schema),
'get_definition_id.return_value': 'document.Document',
'is_recursive.return_value': True,
}
document_cls_mock.reset_mock()
document_cls_mock.configure_mock(**attrs)

f = fields.DocumentField(document_cls_mock, as_ref=True)
definitions, schema = f.get_definitions_and_schema()
assert schema == expected_schema
assert not definitions


def test_recursive_document_field():
class Tree(Document):
Expand Down

0 comments on commit e92c234

Please sign in to comment.