Commit 0679282d authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Return a generic "item" schema for non-inlined (but composite) relation

parent 1fa1ca7902c8
......@@ -291,7 +291,37 @@ class CompositeRelationItemMapper(CollectionItemMapper):
elif entity is not None:
self.etype = entity.cw_etype
def _inlined_relation(self):
"""Return True if relation from selection context is "inlined" per
jsonschema uicfg.
"""
rsection = self._cw.vreg['uicfg'].select('jsonschema', self._cw)
tags = set([
rsection.get(
self.etype, self.rtype, target_type, self.role)
for target_type in self.target_types
])
if len(tags) != 1:
raise ValueError(
'inconsistent rtag for {} with {} as {} (targets: {})'.format(
self.rtype, self.etype, self.role, self.target_types)
)
# We actually check for both "inlined" and "hidden", since a
# CompoundMapper may set a relation to "hidden" while we'll still want
# it to be considered as "inlined" in this context. XXX
return tags.pop() in ('inlined', 'hidden')
def schema_and_definitions(self, schema_role=None):
"""Return schema and definitions for an item of a relation collection.
In "creation" role, schema a oneOf with possible target types entity
JSON schema. Otherwise, depending on wether the relation in "inlined"
per jsonschema uicfg, either a full entity JSON Schema is returned or
a genertic collection item JSON Schema.
"""
if schema_role != CREATION_ROLE and not self._inlined_relation():
return super(CompositeRelationItemMapper,
self).schema_and_definitions(schema_role=schema_role)
items, definitions = [], ProtectedDict()
for target_type in self.target_types:
mapper = self.select_mapper(
......
......@@ -292,6 +292,28 @@ class RelatedCollectionMapperTC(CubicWebTC):
}
self.assertEqual(schema, expected)
def test_schema_view_for_composite_relation(self):
with self.admin_access.cnx() as cnx:
author = cnx.create_entity('Author', name=u'bob')
cnx.commit()
mapper = cnx.vreg['mappers'].select(
'jsonschema.collection', cnx, entity=author,
rtype='author', role='object')
schema = mapper.json_schema()
expected = {
'title': 'author-object',
'type': 'array',
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'title': {'type': 'string'},
'type': {'type': 'string'},
},
},
}
self.assertEqual(schema, expected)
def test_item_serialize(self):
with self.admin_access.cnx() as cnx:
book = cnx.create_entity(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment