From 6521c6b67e32a0e3eeb0459adc213ebe09bca136 Mon Sep 17 00:00:00 2001 From: Filip Date: Thu, 11 Jul 2024 23:44:45 +0200 Subject: [PATCH 1/6] Add ProcessingPolicy and extend options of unknownTermsPolicy #347 --- .../com/apicatalog/jsonld/JsonLdOptions.java | 30 +++++++++++++++++++ .../apicatalog/jsonld/api/ExpansionApi.java | 13 ++++++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java index c23591c7..a14d3e52 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java @@ -44,6 +44,12 @@ public enum RdfDirection { I18N_DATATYPE, COMPOUND_LITERAL } + + public enum ProcessingPolicy { + Ignore, // ignore, the current and default behavior + Fail, // stop processing with an error + Warn // print warning to log + } /* default values */ public static final boolean DEFAULT_RDF_STAR = false; @@ -126,6 +132,9 @@ public enum RdfDirection { private boolean uriValidation; private Duration timeout; + + // a policy on how proceed with undefined terms during expansion + private ProcessingPolicy undefinedTerms; public JsonLdOptions() { this(SchemeRouter.defaultInstance()); @@ -163,6 +172,7 @@ public JsonLdOptions(DocumentLoader loader) { this.documentCache = null; this.uriValidation = DEFAULT_URI_VALIDATION; this.timeout = null; + this.undefinedTerms = ProcessingPolicy.Ignore; } public JsonLdOptions(JsonLdOptions options) { @@ -195,6 +205,7 @@ public JsonLdOptions(JsonLdOptions options) { this.documentCache = options.documentCache; this.uriValidation = options.uriValidation; this.timeout = options.timeout; + this.undefinedTerms = options.undefinedTerms; } /** @@ -523,4 +534,23 @@ public Duration getTimeout() { public void setTimeout(Duration timeout) { this.timeout = timeout; } + + /** + * A processing policy on how proceed with undefined terms during expansion. + * + * @return the processing policy, never null + */ + public ProcessingPolicy getUndefinedTermsPolicy() { + return undefinedTerms; + } + + /** + * Set processing policy on how proceed with undefined terms during expansion. Ignore by default. + * + * @param the processing policy, never null + * + */ + public void setUndefinedTermsPolicy(ProcessingPolicy undefinedTerms) { + this.undefinedTerms = undefinedTerms; + } } \ No newline at end of file diff --git a/src/main/java/com/apicatalog/jsonld/api/ExpansionApi.java b/src/main/java/com/apicatalog/jsonld/api/ExpansionApi.java index 2e46ce99..29b15f3c 100644 --- a/src/main/java/com/apicatalog/jsonld/api/ExpansionApi.java +++ b/src/main/java/com/apicatalog/jsonld/api/ExpansionApi.java @@ -20,6 +20,7 @@ import com.apicatalog.jsonld.JsonLdError; import com.apicatalog.jsonld.JsonLdOptions; import com.apicatalog.jsonld.JsonLdVersion; +import com.apicatalog.jsonld.JsonLdOptions.ProcessingPolicy; import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.loader.DocumentLoader; @@ -157,4 +158,16 @@ public ExpansionApi rdfStar() { options.setRdfStar(true); return this; } + + /** + * Set a processing policy determining how to proceed when an undefined term is + * found during an expansion. An unknown term is ignored by default. + * + * @param policy a processing policy + * @return builder instance + */ + public ExpansionApi undefinedTermsPolicy(ProcessingPolicy policy) { + options.setUndefinedTermsPolicy(policy); + return this; + } } From 3ae160b3043af0107356719bb50090d56581367f Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 15 Jul 2024 10:51:47 +0200 Subject: [PATCH 2/6] Add UNDEFINED_TERM error code --- .../apicatalog/jsonld/JsonLdErrorCode.java | 27 ++++++++++------ .../com/apicatalog/jsonld/JsonLdOptions.java | 32 +++++++++++-------- .../jsonld/loader/LRUDocumentCacheTest.java | 19 ++++++----- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java b/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java index 1d6ac42b..35b58896 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java @@ -255,20 +255,24 @@ public enum JsonLdErrorCode { */ PROTECTED_TERM_REDEFINITION, - // Framing Error Codes https://www.w3.org/TR/json-ld11-framing/#error-handling /** * The frame is invalid. * - * @see invalid frame + * @see invalid + * frame */ INVALID_FRAME, /** - * The value for @embed is not one recognized for the object embed flag. + * The value for @embed is not one recognized for the object embed + * flag. * - * @see invalid @embed value + * @see invalid @embed + * value */ INVALID_KEYWORD_EMBED_VALUE, @@ -289,9 +293,14 @@ public enum JsonLdErrorCode { INVALID_ANNOTATION, // Custom - + PROCESSING_TIMEOUT_EXCEEDED, - + + /** + * An undefined term has been detected during expansion + */ + UNDEFINED_TERM, + UNSPECIFIED; private static final Map CODE_TO_MESSAGE; @@ -351,9 +360,10 @@ public enum JsonLdErrorCode { messages.put(PROTECTED_TERM_REDEFINITION, "An attempt was made to redefine a protected term"); messages.put(INVALID_FRAME, "The frame is invalid"); messages.put(INVALID_KEYWORD_EMBED_VALUE, "The value for @embed is not one recognized for the object embed flag"); - messages.put(INVALID_EMBEDDED_NODE, "An invalid embedded node has been detected."); + messages.put(INVALID_EMBEDDED_NODE, "An invalid embedded node has been detected"); messages.put(INVALID_ANNOTATION, "An invalid annotation has been detected"); - messages.put(PROCESSING_TIMEOUT_EXCEEDED, "A processing has exceeded a defined timeount"); + messages.put(PROCESSING_TIMEOUT_EXCEEDED, "A processing has exceeded a defined timeout"); + messages.put(UNDEFINED_TERM, "An undefined term has been found. Set policy to ignore to pass"); CODE_TO_MESSAGE = Collections.unmodifiableMap(messages); } @@ -362,4 +372,3 @@ public String toMessage() { return CODE_TO_MESSAGE.getOrDefault(this, "Processing error") + " [code=" + this + "]."; } } - diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java index a14d3e52..78ad2bf1 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java @@ -44,11 +44,14 @@ public enum RdfDirection { I18N_DATATYPE, COMPOUND_LITERAL } - + public enum ProcessingPolicy { - Ignore, // ignore, the current and default behavior - Fail, // stop processing with an error - Warn // print warning to log + /** ignore, the current and default behavior */ + Ignore, + /** stop processing with an error */ + Fail, + /** print warning to log */ + Warn } /* default values */ @@ -130,9 +133,9 @@ public enum ProcessingPolicy { private Cache documentCache; private boolean uriValidation; - + private Duration timeout; - + // a policy on how proceed with undefined terms during expansion private ProcessingPolicy undefinedTerms; @@ -511,7 +514,7 @@ public void setUriValidation(boolean enabled) { /** * A processing timeout. An exception is thrown when a processing time exceeds * the duration, if set. There is no currency that processing gets terminated - * immediately, but eventually. + * immediately, but eventually. * * Please note, the timeout does not include time consumed by * {@link DocumentLoader}. @@ -521,22 +524,22 @@ public void setUriValidation(boolean enabled) { public Duration getTimeout() { return timeout; } - + /** - * Set a pressing timeout. A processing is eventually terminated after the - * specified duration. Set null for no timeout. + * Set a pressing timeout. A processing is eventually terminated after the + * specified duration. Set null for no timeout. * * Please note, the timeout does not include time consumed by * {@link DocumentLoader}. - * + * * @param timeout to limit processing time */ public void setTimeout(Duration timeout) { this.timeout = timeout; } - + /** - * A processing policy on how proceed with undefined terms during expansion. + * A processing policy on how proceed with an undefined term during expansion. * * @return the processing policy, never null */ @@ -545,7 +548,8 @@ public ProcessingPolicy getUndefinedTermsPolicy() { } /** - * Set processing policy on how proceed with undefined terms during expansion. Ignore by default. + * Set processing policy on how proceed with an undefined term during expansion. + * Ignore by default. * * @param the processing policy, never null * diff --git a/src/test/java/com/apicatalog/jsonld/loader/LRUDocumentCacheTest.java b/src/test/java/com/apicatalog/jsonld/loader/LRUDocumentCacheTest.java index d2d73c16..94cd05c6 100644 --- a/src/test/java/com/apicatalog/jsonld/loader/LRUDocumentCacheTest.java +++ b/src/test/java/com/apicatalog/jsonld/loader/LRUDocumentCacheTest.java @@ -1,19 +1,18 @@ package com.apicatalog.jsonld.loader; -import com.apicatalog.jsonld.JsonLdError; -import com.apicatalog.jsonld.document.Document; -import com.apicatalog.jsonld.document.JsonDocument; -import jakarta.json.JsonValue; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.LinkedList; import java.util.List; -import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.apicatalog.jsonld.JsonLdError; +import com.apicatalog.jsonld.document.Document; +import com.apicatalog.jsonld.document.JsonDocument; + +import jakarta.json.JsonValue; public class LRUDocumentCacheTest { From c3b8c9da290a127af28dfe0f41352b471ca4564a Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 15 Jul 2024 12:31:52 +0200 Subject: [PATCH 3/6] Add test cases #347 --- .../jsonld/test/JsonLdTestCase.java | 89 ++++++++++--------- .../jsonld/test/JsonLdTestCaseOptions.java | 9 ++ .../com/apicatalog/jsonld/test/manifest.json | 42 +++++++++ .../jsonld/test/undefined-term-1-in.json | 5 ++ .../jsonld/test/undefined-term-2-in.json | 5 ++ .../jsonld/test/undefined-term-2-out.json | 10 +++ .../jsonld/test/undefined-term-3-in.json | 4 + .../jsonld/test/undefined-term-3-out.json | 10 +++ .../jsonld/test/undefined-term-context.json | 8 ++ 9 files changed, 139 insertions(+), 43 deletions(-) create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-1-in.json create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-in.json create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-out.json create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-in.json create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-out.json create mode 100644 src/test/resources/com/apicatalog/jsonld/test/undefined-term-context.json diff --git a/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCase.java b/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCase.java index aec25f28..c4323938 100644 --- a/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCase.java +++ b/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCase.java @@ -73,6 +73,8 @@ public final class JsonLdTestCase { private final String testsBase; + public JsonLdOptions.ProcessingPolicy undefinedTermPolicy = JsonLdOptions.ProcessingPolicy.Ignore; + private final DocumentLoader loader; public JsonLdTestCase(final String testsBase, final DocumentLoader loader) { @@ -89,43 +91,42 @@ public static final JsonLdTestCase of(JsonObject o, String manifestUri, String m testCase.uri = baseUri + manifestUri.substring(0, manifestUri.length() - ".jsonld".length()) + testCase.id; testCase.type = o.get(Keywords.TYPE).asJsonArray().stream() - .map(JsonString.class::cast) - .map(JsonString::getString) - .map(Type::of) - .collect(Collectors.toSet()); + .map(JsonString.class::cast) + .map(JsonString::getString) + .map(Type::of) + .collect(Collectors.toSet()); testCase.name = o.getString("name"); testCase.input = o.containsKey("input") - ? URI.create(baseUri + o.getString("input")) - : null; + ? URI.create(baseUri + o.getString("input")) + : null; testCase.context = o.containsKey("context") - ? URI.create(baseUri + o.getString("context")) - : null; + ? URI.create(baseUri + o.getString("context")) + : null; testCase.expect = o.containsKey("expect") - ? URI.create(baseUri + o.getString("expect")) - : null; + ? URI.create(baseUri + o.getString("expect")) + : null; testCase.frame = o.containsKey("frame") - ? URI.create(baseUri + o.getString("frame")) - : null; + ? URI.create(baseUri + o.getString("frame")) + : null; testCase.expectErrorCode = o.containsKey("expectErrorCode") - ? errorCode((o.getString("expectErrorCode"))) - : null; + ? errorCode((o.getString("expectErrorCode"))) + : null; testCase.options = o.containsKey("option") - ? JsonLdTestCaseOptions.of(o.getJsonObject("option"), baseUri) - : new JsonLdTestCaseOptions(); + ? JsonLdTestCaseOptions.of(o.getJsonObject("option"), baseUri) + : new JsonLdTestCaseOptions(); testCase.baseUri = baseUri; - testCase.contentType = o.containsKey("option") && o.getJsonObject("option").containsKey("contentType") - ? MediaType.of(o.getJsonObject("option").getString("contentType")) - : null; + ? MediaType.of(o.getJsonObject("option").getString("contentType")) + : null; if (testCase.contentType == null && testCase.input != null) { @@ -141,60 +142,63 @@ public static final JsonLdTestCase of(JsonObject o, String manifestUri, String m } testCase.redirectTo = o.containsKey("option") && o.getJsonObject("option").containsKey("redirectTo") - ? URI.create(baseUri + o.getJsonObject("option").getString("redirectTo")) - : null; + ? URI.create(baseUri + o.getJsonObject("option").getString("redirectTo")) + : null; testCase.httpStatus = o.containsKey("option") - ? o.getJsonObject("option").getInt("httpStatus", 301) - : null - ; + ? o.getJsonObject("option").getInt("httpStatus", 301) + : null; - if (o.containsKey("option") && o.getJsonObject("option").containsKey("httpLink")) { + if (o.containsKey("option") && o.getJsonObject("option").containsKey("httpLink")) { JsonValue links = o.getJsonObject("option").get("httpLink"); if (JsonUtils.isArray(links)) { testCase.httpLink = links.asJsonArray().stream() - .map(JsonString.class::cast) - .map(JsonString::getString) - .collect(Collectors.toSet()); + .map(JsonString.class::cast) + .map(JsonString::getString) + .collect(Collectors.toSet()); } else { testCase.httpLink = new HashSet<>(); - testCase.httpLink.add(((JsonString)links).getString()); + testCase.httpLink.add(((JsonString) links).getString()); } } + testCase.undefinedTermPolicy = o.containsKey("option") + ? JsonLdOptions.ProcessingPolicy.valueOf(o.getJsonObject("option").getString("undefinedTermPolicy", JsonLdOptions.ProcessingPolicy.Fail.name())) + : JsonLdOptions.ProcessingPolicy.Ignore; + return testCase; } public JsonLdOptions getOptions() { - final DocumentLoader rewriter = - new UriBaseRewriter( - baseUri, - testsBase, - loader - ); + final DocumentLoader rewriter = new UriBaseRewriter( + baseUri, + testsBase, + loader); JsonLdOptions jsonLdOptions = new JsonLdOptions(rewriter); jsonLdOptions.setOrdered(true); options.setup(jsonLdOptions); + return jsonLdOptions; } public static final JsonLdErrorCode errorCode(String errorCode) { - if (errorCode == null || StringUtils.isBlank(errorCode)) { + if (errorCode == null || StringUtils.isBlank(errorCode)) { return null; } /* - * Because scoped contexts can lead to contexts being reloaded, - * replace the recursive context inclusion error with a context overflow error. + * Because scoped contexts can lead to contexts being reloaded, replace the + * recursive context inclusion error with a context overflow error. * - * @see Changes since JSON-LD Community Group Final Report + * @see Changes + * since JSON-LD Community Group Final Report */ if ("recursive context inclusion".equalsIgnoreCase(errorCode)) { return JsonLdErrorCode.CONTEXT_OVERFLOW; @@ -206,7 +210,7 @@ public static final JsonLdErrorCode errorCode(String errorCode) { return JsonLdErrorCode.UNSPECIFIED; } - return JsonLdErrorCode.valueOf(StringUtils.strip(errorCode).toUpperCase().replace(" ", "_").replace("-", "_").replaceAll("\\_\\@", "_KEYWORD_" )); + return JsonLdErrorCode.valueOf(StringUtils.strip(errorCode).toUpperCase().replace(" ", "_").replace("-", "_").replaceAll("\\_\\@", "_KEYWORD_")); } public enum Type { @@ -220,8 +224,7 @@ public enum Type { POSITIVE_EVALUATION_TEST, NEGATIVE_EVALUATION_TEST, - POSITIVE_SYNTAX_TEST - ; + POSITIVE_SYNTAX_TEST; static Type of(String value) { @@ -252,7 +255,7 @@ static Type of(String value) { return POSITIVE_SYNTAX_TEST; } - throw new IllegalArgumentException("Unknown test @type '" + value + "'"); + throw new IllegalArgumentException("Unknown test @type '" + value + "'"); } } diff --git a/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCaseOptions.java b/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCaseOptions.java index f675e071..b168a312 100644 --- a/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCaseOptions.java +++ b/src/test/java/com/apicatalog/jsonld/test/JsonLdTestCaseOptions.java @@ -40,6 +40,7 @@ public class JsonLdTestCaseOptions { public Boolean omitGraph; public Boolean numericId; public Boolean rdfStar; + public JsonLdOptions.ProcessingPolicy undefinedTerms; public static final JsonLdTestCaseOptions of(JsonObject o, String baseUri) { @@ -93,6 +94,10 @@ public static final JsonLdTestCaseOptions of(JsonObject o, String baseUri) { if (o.containsKey("rdfstar")) { options.rdfStar = o.getBoolean("rdfstar"); } + + if (o.containsKey("undefinedTermPolicy")) { + options.undefinedTerms = JsonLdOptions.ProcessingPolicy.valueOf(o.getString("undefinedTermPolicy")); + } return options; } @@ -146,5 +151,9 @@ public void setup(JsonLdOptions options) { if (rdfStar != null) { options.setRdfStar(rdfStar); } + + if (undefinedTerms != null) { + options.setUndefinedTermsPolicy(undefinedTerms); + } } } diff --git a/src/test/resources/com/apicatalog/jsonld/test/manifest.json b/src/test/resources/com/apicatalog/jsonld/test/manifest.json index 647edc57..da5c4c1f 100644 --- a/src/test/resources/com/apicatalog/jsonld/test/manifest.json +++ b/src/test/resources/com/apicatalog/jsonld/test/manifest.json @@ -236,6 +236,48 @@ "name": "Issue #333: type coercion: @container in @container", "input": "issue333-in.json", "expect": "issue333-out.json" + }, + { + "@id": "#t0019", + "@type": [ + "jld:NegativeEvaluationTest", + "jld:ExpandTest" + ], + "name": "Test undefined term fail policy", + "input": "undefined-term-1-in.json", + "expectErrorCode": "undefined term", + "option": { + "expandContext": "undefined-term-context.json", + "undefinedTermPolicy": "Fail" + } + }, + { + "@id": "#t0020", + "@type": [ + "jld:PositiveEvaluationTest", + "jld:ExpandTest" + ], + "name": "Test undefined term fail policy", + "input": "undefined-term-2-in.json", + "expect": "undefined-term-2-out.json", + "option": { + "expandContext": "undefined-term-context.json", + "undefintedTermPolicy": "Ignore" + } + }, + { + "@id": "#t0021", + "@type": [ + "jld:PositiveEvaluationTest", + "jld:ExpandTest" + ], + "name": "Test undefined term fail policy", + "input": "undefined-term-3-in.json", + "expect": "undefined-term-3-out.json", + "option": { + "expandContext": "undefined-term-context.json", + "undefintedTermPolicy": "Fail" + } } ] } diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-1-in.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-1-in.json new file mode 100644 index 00000000..e14c9048 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-1-in.json @@ -0,0 +1,5 @@ +{ + "uri": "https://example.com/id", + "description": "test", + "name": "john" +} \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-in.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-in.json new file mode 100644 index 00000000..e14c9048 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-in.json @@ -0,0 +1,5 @@ +{ + "uri": "https://example.com/id", + "description": "test", + "name": "john" +} \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-out.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-out.json new file mode 100644 index 00000000..133c34ae --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-2-out.json @@ -0,0 +1,10 @@ +[ + { + "http://www.w3.org/2000/01/rdf-schema#comment": [ + { + "@value": "test" + } + ], + "@id": "https://example.com/id" + } +] \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-in.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-in.json new file mode 100644 index 00000000..3622b5cc --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-in.json @@ -0,0 +1,4 @@ +{ + "uri": "https://example.com/id", + "description": "test" +} \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-out.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-out.json new file mode 100644 index 00000000..dc80296a --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-3-out.json @@ -0,0 +1,10 @@ +[ + { + "http://www.w3.org/2000/01/rdf-schema#comment": [ + { + "@value": "test" + } + ], + "@id": "https://example.com/id" + } +] \ No newline at end of file diff --git a/src/test/resources/com/apicatalog/jsonld/test/undefined-term-context.json b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-context.json new file mode 100644 index 00000000..f7ee0424 --- /dev/null +++ b/src/test/resources/com/apicatalog/jsonld/test/undefined-term-context.json @@ -0,0 +1,8 @@ +{ + "@context": { + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "@base": "https://api.inaturalist.org/v1/observations/", + "uri": "@id", + "description": "rdfs:comment" + } +} \ No newline at end of file From 88687477555ef0a44a5df81ea1e8a2cce4b1ff04 Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 15 Jul 2024 12:50:59 +0200 Subject: [PATCH 4/6] Fix #347 --- .../jsonld/expansion/ObjectExpansion1314.java | 15 +++++++++++---- .../jsonld/processor/ProcessingRuntime.java | 5 +++++ .../com/apicatalog/jsonld/test/manifest.json | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/apicatalog/jsonld/expansion/ObjectExpansion1314.java b/src/main/java/com/apicatalog/jsonld/expansion/ObjectExpansion1314.java index 0a441a22..c276e684 100644 --- a/src/main/java/com/apicatalog/jsonld/expansion/ObjectExpansion1314.java +++ b/src/main/java/com/apicatalog/jsonld/expansion/ObjectExpansion1314.java @@ -137,15 +137,23 @@ public void expand() throws JsonLdError { activeContext.runtime().tick(); // 13.2. - String expandedProperty = activeContext + final String expandedProperty = activeContext .uriExpansion() .documentRelative(false) .vocab(true) .expand(key); - // 13.3. + // if the term is undefined and if (expandedProperty == null || (!expandedProperty.contains(":") && !Keywords.contains(expandedProperty))) { - continue; + switch (activeContext.runtime().getUndefinedTermPolicy()) { + case Fail: + throw new JsonLdError(JsonLdErrorCode.UNDEFINED_TERM, + "An undefined term has been found [" + key + "]. Change policy to Ignore or Warn or define the term in a context"); + case Warn: + LOGGER.log(Level.WARNING, "An undefined term has been found [{0}]", key); + case Ignore: + continue; + } } JsonValue value = element.get(key); @@ -163,7 +171,6 @@ public void expand() throws JsonLdError { // 13.4.2 if (result.containsKey(expandedProperty) && Keywords.noneMatch(expandedProperty, Keywords.INCLUDED, Keywords.TYPE)) { - throw new JsonLdError(JsonLdErrorCode.COLLIDING_KEYWORDS, "Two properties which expand to the same keyword have been detected. A property '" + key + "'" + " expands to '" + expandedProperty + "'" diff --git a/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java b/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java index 451e7dda..0d92e7ee 100644 --- a/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java +++ b/src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java @@ -2,6 +2,7 @@ import com.apicatalog.jsonld.JsonLdError; import com.apicatalog.jsonld.JsonLdOptions; +import com.apicatalog.jsonld.JsonLdOptions.ProcessingPolicy; import com.apicatalog.jsonld.JsonLdVersion; import com.apicatalog.jsonld.context.cache.Cache; import com.apicatalog.jsonld.document.Document; @@ -79,4 +80,8 @@ public boolean isRdfStar() { public boolean isNumericId() { return options.isNumericId(); } + + public ProcessingPolicy getUndefinedTermPolicy() { + return options.getUndefinedTermsPolicy(); + } } diff --git a/src/test/resources/com/apicatalog/jsonld/test/manifest.json b/src/test/resources/com/apicatalog/jsonld/test/manifest.json index da5c4c1f..ca33c440 100644 --- a/src/test/resources/com/apicatalog/jsonld/test/manifest.json +++ b/src/test/resources/com/apicatalog/jsonld/test/manifest.json @@ -262,7 +262,7 @@ "expect": "undefined-term-2-out.json", "option": { "expandContext": "undefined-term-context.json", - "undefintedTermPolicy": "Ignore" + "undefinedTermPolicy": "Warn" } }, { @@ -276,7 +276,7 @@ "expect": "undefined-term-3-out.json", "option": { "expandContext": "undefined-term-context.json", - "undefintedTermPolicy": "Fail" + "undefinedTermPolicy": "Fail" } } ] From 39e3a4895356f7ee3d3a5102000f69d388a9e2e5 Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 15 Jul 2024 12:53:25 +0200 Subject: [PATCH 5/6] Update README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 41dcac12..2a337ebc 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,15 @@ JsonLd.toRdf("https://example/document.jsonld").loader(cachedLoader).get(); JsonLd.toRdf("https://example/another-document.jsonld").loader(cachedLoader).get(); ``` +#### Undefined Terms Processing Policy + +Set processing policy on undefined terms. `Ignore` by default. + +```javascript +// since 1.4.1 +JsonLd.expand(...).undefinedTermsPolicy(Fail|Warn|Ignore).get(); +``` + ## Contributing All PR's welcome! From 681700d01e0e0a45aecf2b3694ffb198ea044091 Mon Sep 17 00:00:00 2001 From: Filip Date: Mon, 15 Jul 2024 13:00:39 +0200 Subject: [PATCH 6/6] Fix javadoc --- pom_parent.xml | 1 + src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java | 7 ++----- src/main/java/com/apicatalog/jsonld/JsonLdOptions.java | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pom_parent.xml b/pom_parent.xml index e85cefd0..0de15146 100644 --- a/pom_parent.xml +++ b/pom_parent.xml @@ -129,6 +129,7 @@ 3.7.0 all,-missing + false diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java b/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java index 35b58896..043ebb5c 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java @@ -31,8 +31,7 @@ public enum JsonLdErrorCode { /** - * Two - * properties + * Two properties * which expand to the same keyword have been detected. This might occur if a * keyword and an alias thereof are used at the same time. */ @@ -270,9 +269,7 @@ public enum JsonLdErrorCode { * The value for @embed is not one recognized for the object embed * flag. * - * @see invalid @embed - * value + * @see invalid @embedvalue */ INVALID_KEYWORD_EMBED_VALUE, diff --git a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java index 78ad2bf1..1eecc27d 100644 --- a/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java +++ b/src/main/java/com/apicatalog/jsonld/JsonLdOptions.java @@ -551,7 +551,7 @@ public ProcessingPolicy getUndefinedTermsPolicy() { * Set processing policy on how proceed with an undefined term during expansion. * Ignore by default. * - * @param the processing policy, never null + * @param undefinedTerms the processing policy, never null * */ public void setUndefinedTermsPolicy(ProcessingPolicy undefinedTerms) {