Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
jsonschema
Commits
463795b65da8
Commit
0679282d
authored
Mar 19, 2018
by
Denis Laxalde
Browse files
Return a generic "item" schema for non-inlined (but composite) relation
parent
1fa1ca7902c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
cubicweb_jsonschema/mappers/collection.py
View file @
463795b6
...
...
@@ -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
(
...
...
test/test_mappers.py
View file @
463795b6
...
...
@@ -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
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment