From daf495b27eea3779c8a1d34b60b24744e9f79fd5 Mon Sep 17 00:00:00 2001 From: jrcastro2 Date: Mon, 21 Oct 2024 15:55:41 +0200 Subject: [PATCH] names: allow resolver to return multiple values * update props json schema to allow multiple types --- .../names/jsonschemas/names/name-v1.0.0.json | 30 ++++++++++++---- .../contrib/names/services.py | 36 +++++++++++-------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/invenio_vocabularies/contrib/names/jsonschemas/names/name-v1.0.0.json b/invenio_vocabularies/contrib/names/jsonschemas/names/name-v1.0.0.json index 752bfb75..5cea25fa 100644 --- a/invenio_vocabularies/contrib/names/jsonschemas/names/name-v1.0.0.json +++ b/invenio_vocabularies/contrib/names/jsonschemas/names/name-v1.0.0.json @@ -15,20 +15,38 @@ "description": "Identifier of the name scheme.", "$ref": "local://definitions-v1.0.0.json#/identifier" }, - "name": {"type": "string"}, - "given_name": {"type": "string"}, - "family_name": {"type": "string"}, + "name": { + "type": "string" + }, + "given_name": { + "type": "string" + }, + "family_name": { + "type": "string" + }, "identifiers": { "description": "Identifiers for the person.", "type": "array", - "items": {"$ref": "local://definitions-v1.0.0.json#/identifiers_with_scheme"}, + "items": { + "$ref": "local://definitions-v1.0.0.json#/identifiers_with_scheme" + }, "uniqueItems": true }, "props": { "type": "object", "patternProperties": { "^.*$": { - "type": "string" + "oneOf": [ + { + "type": "string" + }, + { + "type": "boolean" + }, + { + "type": "array" + } + ] } } }, @@ -50,4 +68,4 @@ "uniqueItems": true } } -} +} \ No newline at end of file diff --git a/invenio_vocabularies/contrib/names/services.py b/invenio_vocabularies/contrib/names/services.py index b2ac2941..c0c7efad 100644 --- a/invenio_vocabularies/contrib/names/services.py +++ b/invenio_vocabularies/contrib/names/services.py @@ -19,11 +19,12 @@ class NamesService(record_type.service_cls): """Name service.""" - def resolve(self, identity, id_, id_type): + def resolve(self, identity, id_, id_type, many=False): """Get the record with a given identifier. - This method assumes that the are no duplicates in the system - (i.e. only one name record can have a pair of identifier:scheme). + param id_: The identifier value. + param id_type: The identifier type. + param many: If True, return a list of records. """ search_query = dsl.Q( "bool", @@ -36,20 +37,25 @@ def resolve(self, identity, id_, id_type): # max_records = 1, we assume there cannot be duplicates # the loading process needs to make sure of that - results = self._read_many(identity, search_query, max_records=1) + if many: + results = self._read_many(identity, search_query) + else: + results = self._read_many(identity, search_query, max_records=1) + # cant use the results_item because it returns dicts intead of records total = results.hits.total["value"] if total == 0: # Not a PID but trated as such raise PIDDoesNotExistError(pid_type=id_type, pid_value=id_) - - # (0 < #hits <= max_records) = 1 - record = self.record_cls.loads(results[0].to_dict()) - self.require_permission(identity, "read", record=record) - - return self.result_item( - self, - identity, - record, - links_tpl=self.links_item_tpl, - ) + if many: + return self.result_list(self, identity, results) + else: + record = self.record_cls.loads(results[0].to_dict()) + self.require_permission(identity, "read", record=record) + + return self.result_item( + self, + identity, + record, + links_tpl=self.links_item_tpl, + )