From f35451ce22efca8927a1551db559004dae8c1158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalle=20W=C3=A5hlin?= <72360110+kwahlin@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:49:12 +0100 Subject: [PATCH] Safe guard against matching non-existing subtypes (#1525) --- .../whelk/datatool/form/MatchForm.groovy | 12 +++++------- .../whelk/datatool/form/MatchFormSpec.groovy | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/whelktool/src/main/groovy/whelk/datatool/form/MatchForm.groovy b/whelktool/src/main/groovy/whelk/datatool/form/MatchForm.groovy index 0e828ba6a9..85688e7905 100644 --- a/whelktool/src/main/groovy/whelk/datatool/form/MatchForm.groovy +++ b/whelktool/src/main/groovy/whelk/datatool/form/MatchForm.groovy @@ -1,6 +1,5 @@ package whelk.datatool.form -import groovy.transform.Memoized import groovy.transform.PackageScope import whelk.Document import whelk.JsonLd @@ -9,7 +8,6 @@ import whelk.datatool.util.DocumentComparator import whelk.datatool.util.IdLoader import whelk.util.DocumentUtil -import static whelk.JsonLd.GRAPH_KEY import static whelk.JsonLd.ID_KEY import static whelk.JsonLd.RECORD_KEY import static whelk.JsonLd.RECORD_TYPE @@ -78,9 +76,7 @@ class MatchForm { thing[ID_KEY] = getThingTmpId() record[THING_KEY] = [(ID_KEY): getThingTmpId()] - Map graph = [(GRAPH_KEY): [record, thing]] - - String ttl = toTurtleNoPrelude(graph, context) + String ttl = toTurtleNoPrelude([record, thing], context) return insertTypeMappings(insertIdMappings(insertVars(ttl))) } @@ -164,7 +160,7 @@ class MatchForm { if (node[TYPE_KEY] == ANY_TYPE) { node.remove(TYPE_KEY) } - if (asList(node.remove(MATCHING_MODE)).contains(SUBTYPES)) { + if (asList(node.remove(MATCHING_MODE)).contains(SUBTYPES) && baseTypeToSubtypes.containsKey(node[TYPE_KEY])) { def baseType = node.remove(TYPE_KEY) node[HAS_BASE_TYPE_TMP] = baseType } @@ -300,7 +296,9 @@ class MatchForm { if (node instanceof Map && node.containsKey(MATCHING_MODE) && ((List) node[MATCHING_MODE]).contains(SUBTYPES)) { def baseType = (String) node[TYPE_KEY] Set subTypes = getSubtypes(baseType, jsonLd) as Set - mappings[baseType] = subTypes + if (!subTypes.isEmpty()) { + mappings[baseType] = subTypes + } return new DocumentUtil.Nop() } } diff --git a/whelktool/src/test/groovy/whelk/datatool/form/MatchFormSpec.groovy b/whelktool/src/test/groovy/whelk/datatool/form/MatchFormSpec.groovy index 7cfa53db6d..9a8612a4a9 100644 --- a/whelktool/src/test/groovy/whelk/datatool/form/MatchFormSpec.groovy +++ b/whelktool/src/test/groovy/whelk/datatool/form/MatchFormSpec.groovy @@ -175,4 +175,22 @@ class MatchFormSpec extends Specification { expect: transform.getSparqlPattern(context) == expectedPattern } + + def "form to sparql pattern: no subtypes to match"() { + given: + def form = [ + 'bulk:formBlankNodeId': '#1', + '@type' : 'T1', + 'bulk:matchingMode' : ['bulk:Subtypes'] + ] + + def expectedPattern = "?graph :mainEntity ?1 .\n" + + "\n" + + "?1 a :T1 ." + + def transform = new MatchForm(form) + + expect: + transform.getSparqlPattern(context) == expectedPattern + } }