Skip to content

Commit

Permalink
fixes for inheritance and recursive classes
Browse files Browse the repository at this point in the history
includes tests for condition
  • Loading branch information
pbutler committed Jul 24, 2017
1 parent b875a74 commit b6fbd30
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,17 @@ def is_recursive(cls, role=DEFAULT_ROLE):
:param str role: A current role.
"""
for pcls in cls.__bases__:
for field in pcls.resolve_and_walk(through_document_fields=True,
role=role, visited_documents=set([pcls])):
if isinstance(field, DocumentField):
if field.document_cls == cls or issubclass(field.document_cls, cls):
return True

for field in cls.resolve_and_walk(through_document_fields=True,
role=role, visited_documents=set([cls])):
if isinstance(field, DocumentField):
if field.document_cls == cls:
if field.document_cls == cls or issubclass(field.document_cls, cls):
return True
return False

Expand Down
59 changes: 59 additions & 0 deletions tests/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,62 @@ class Options:

schema = Any.get_schema()
assert normalize(schema) == normalize(expected_schema)


def test_recursive_inheritance_with_base():
class Base(Document):
children = DocumentField("Any", as_ref=True)

class A(Base):
class Options:
definition_id = "A"
a = StringField(required=True)

class B(Base):
class Options:
definition_id = "B"
b = StringField(required=True)

class Any(A, B):
class Options:
definition_id = "Any"
inheritance_mode = ONE_OF

expected_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'definitions': {
'A': {
'type': 'object',
'properties': {
'children': {'$ref': '#/definitions/Any'},
'a': {'type': 'string'}
},
'required': ['a'],
'additionalProperties': False
},
'B': {
'type': 'object',
'properties': {
'children': {'$ref': '#/definitions/Any'},
'b': {'type': 'string'}
},
'required': ['b'],
'additionalProperties': False
},
'Any': {
'oneOf': [
{'$ref': '#/definitions/A'},
{'$ref': '#/definitions/B'},
{
'type': 'object',
'properties': {},
'additionalProperties': False
}
]
}
},
'$ref': '#/definitions/Any'
}

schema = Any.get_schema()
assert normalize(schema) == normalize(expected_schema)

0 comments on commit b6fbd30

Please sign in to comment.