diff --git a/cubicweb_jsonschema/mappers/relations.py b/cubicweb_jsonschema/mappers/relations.py index 3795cd099628df85b984bcbcf54e0748dea91e96_Y3ViaWN3ZWJfanNvbnNjaGVtYS9tYXBwZXJzL3JlbGF0aW9ucy5weQ==..2e98697b8139ede38417beea035b7b7ddd765bd0_Y3ViaWN3ZWJfanNvbnNjaGVtYS9tYXBwZXJzL3JlbGF0aW9ucy5weQ== 100644 --- a/cubicweb_jsonschema/mappers/relations.py +++ b/cubicweb_jsonschema/mappers/relations.py @@ -721,8 +721,13 @@ # XXX copy of CollectionItemMapper's method def links(self, schema_role=None, **kwargs): kwargs['anchor'] = '#' - return super(ETypeRelationItemMapper, self).links( - schema_role=schema_role, **kwargs) + for linkcls in super(ETypeRelationItemMapper, self).links( + schema_role=schema_role, **kwargs): + yield linkcls + search_link = self._cw.vreg['links'].select_or_none( + 'jsonschema.schema-search', self._cw) + if search_link: + yield search_link @JSONSchemaSerializer.register diff --git a/cubicweb_jsonschema/views/link.py b/cubicweb_jsonschema/views/link.py index 3795cd099628df85b984bcbcf54e0748dea91e96_Y3ViaWN3ZWJfanNvbnNjaGVtYS92aWV3cy9saW5rLnB5..2e98697b8139ede38417beea035b7b7ddd765bd0_Y3ViaWN3ZWJfanNvbnNjaGVtYS92aWV3cy9saW5rLnB5 100644 --- a/cubicweb_jsonschema/views/link.py +++ b/cubicweb_jsonschema/views/link.py @@ -218,3 +218,31 @@ 'properties': search_terms, }, } + + +class SchemaSearchRelatedLink(Link): + __regid__ = 'jsonschema.schema-search' + + def description_object(self, resource): + request = resource.request + entity = resource.__parent__.rset.one() + rschema = entity.e_schema.rdef(resource.rtype, resource.role) + target_type = getattr(rschema, neg_role(resource.role)) + mapper = self._cw.vreg['mappers'].select( + 'jsonschema.entity', self._cw, etype=target_type, + rtype=resource.rtype, role=resource.role, + target_types={entity.cw_etype}, + ) + search_terms = mapper.json_schema(VIEW_ROLE)['properties'] + href = ( + request.resource_path(resource, 'schema') + + '?searchBy=' + ','.join( + '%s:{%s}' % (p, p) for p in search_terms) + ) + return { + 'rel': 'search', + 'href': href, + 'hrefSchema': { + 'properties': search_terms, + }, + } diff --git a/test/hypermedia-walkthrough.rst b/test/hypermedia-walkthrough.rst index 3795cd099628df85b984bcbcf54e0748dea91e96_dGVzdC9oeXBlcm1lZGlhLXdhbGt0aHJvdWdoLnJzdA==..2e98697b8139ede38417beea035b7b7ddd765bd0_dGVzdC9oeXBlcm1lZGlhLXdhbGt0aHJvdWdoLnJzdA== 100644 --- a/test/hypermedia-walkthrough.rst +++ b/test/hypermedia-walkthrough.rst @@ -554,6 +554,18 @@ "additionalProperties": false, "links": [ { + "rel": "search", + "href": "/book/.../topics/schema?searchBy=name:{name}", + "hrefSchema": { + "properties": { + "name": { + "type": "string", + "title": "name" + } + } + } + }, + { "href": "/book/.../topics/{id}", "anchor": "#", "rel": "item" @@ -635,7 +647,21 @@ } ] } - } + }, + "links": [ + { + "rel": "search", + "href": "/book/.../topics/schema?searchBy=name:{name}", + "hrefSchema": { + "properties": { + "name": { + "title": "name", + "type": "string" + } + } + } + } + ] } } >>> possible_topics = r.json['items']['properties']['id']['oneOf'] @@ -693,7 +719,7 @@ .. code-block:: python >>> from uritemplate import URITemplate - >>> item_link = topics_schema['items']['links'][0] + >>> item_link = topics_schema['items']['links'][1] >>> item_uritemplate = URITemplate(item_link['href']) >>> item_uri = item_uritemplate.expand(topics[1]) >>> item_response = client.get(item_uri, @@ -832,7 +858,21 @@ } ] } - } + }, + "links": [ + { + "rel": "search", + "href": "/book/.../topics/schema?searchBy=name:{name}", + "hrefSchema": { + "properties": { + "name": { + "type": "string", + "title": "name" + } + } + } + } + ] } } @@ -870,7 +910,21 @@ } ] } - } + }, + "links": [ + { + "rel": "search", + "href": "/book/.../topics/schema?searchBy=name:{name}", + "hrefSchema": { + "properties": { + "name": { + "type": "string", + "title": "name" + } + } + } + } + ] } } @@ -924,7 +978,21 @@ } ] } - } + }, + "links": [ + { + "rel": "search", + "href": "/book/.../topics/schema?searchBy=name:{name}", + "hrefSchema": { + "properties": { + "name": { + "title": "name", + "type": "string" + } + } + } + } + ] } }