Skip to content

Commit

Permalink
names: allow resolver to return multiple values
Browse files Browse the repository at this point in the history
* update props json schema to allow multiple types
  • Loading branch information
jrcastro2 committed Oct 21, 2024
1 parent 9d798c3 commit daf495b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
}
},
Expand All @@ -50,4 +68,4 @@
"uniqueItems": true
}
}
}
}
36 changes: 21 additions & 15 deletions invenio_vocabularies/contrib/names/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
)

0 comments on commit daf495b

Please sign in to comment.