Skip to content

Commit

Permalink
Safe guard against matching non-existing subtypes (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwahlin authored Nov 20, 2024
1 parent 7f24fda commit f35451c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 5 additions & 7 deletions whelktool/src/main/groovy/whelk/datatool/form/MatchForm.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package whelk.datatool.form

import groovy.transform.Memoized
import groovy.transform.PackageScope
import whelk.Document
import whelk.JsonLd
Expand All @@ -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
Expand Down Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<String> subTypes = getSubtypes(baseType, jsonLd) as Set
mappings[baseType] = subTypes
if (!subTypes.isEmpty()) {
mappings[baseType] = subTypes
}
return new DocumentUtil.Nop()
}
}
Expand Down
18 changes: 18 additions & 0 deletions whelktool/src/test/groovy/whelk/datatool/form/MatchFormSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit f35451c

Please sign in to comment.