From 055ab6e7aadbab5e091c6f688bd37bc1a2758f02 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Thu, 30 May 2024 11:23:05 +0200 Subject: [PATCH] provide a jsonprovider to const keyword --- .../jsonschema/core/keyword/type/AnyKeywordType.java | 11 +++++++---- .../core/vocab/validation/ConstKeyword.java | 8 +++++--- .../core/vocab/validation/ValidationVocabulary.java | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AnyKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AnyKeywordType.java index 1b7862c1..ab7c3622 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AnyKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AnyKeywordType.java @@ -27,15 +27,18 @@ import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.JsonValue; +import jakarta.json.spi.JsonProvider; import java.util.Objects; -import java.util.function.Function; +import java.util.function.BiFunction; public class AnyKeywordType implements KeywordType { + private final JsonProvider jsonContext; private final String name; - private final Function keywordCreator; + private final BiFunction keywordCreator; - public AnyKeywordType(final String name, final Function keywordCreator) { + public AnyKeywordType(final JsonProvider jsonContext, final String name, final BiFunction keywordCreator) { + this.jsonContext = Objects.requireNonNull(jsonContext); this.name = Objects.requireNonNull(name); this.keywordCreator = Objects.requireNonNull(keywordCreator); } @@ -47,6 +50,6 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema) { - return keywordCreator.apply(schema.asJsonObject().getOrDefault(name, JsonValue.NULL)); + return keywordCreator.apply(jsonContext, schema.asJsonObject().getOrDefault(name, JsonValue.NULL)); } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeyword.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeyword.java index ccb90adb..cd17a665 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeyword.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeyword.java @@ -27,9 +27,9 @@ import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.keyword.Assertion; -import jakarta.json.Json; import jakarta.json.JsonNumber; import jakarta.json.JsonValue; +import jakarta.json.spi.JsonProvider; import java.util.Objects; /** @@ -46,9 +46,11 @@ class ConstKeyword implements Assertion { static final String NAME = "const"; + private final JsonProvider jsonContext; private final JsonValue allowedValue; - public ConstKeyword(final JsonValue allowedValue) { + public ConstKeyword(final JsonProvider jsonContext, final JsonValue allowedValue) { + this.jsonContext = Objects.requireNonNull(jsonContext); this.allowedValue = Objects.requireNonNull(allowedValue); } @@ -79,6 +81,6 @@ public boolean hasName(final String name) { @Override public > T printOn(final T media) { - return new JsonObjectPrintable(Json.createObjectBuilder().add(NAME, allowedValue).build()).printOn(media); + return new JsonObjectPrintable(jsonContext.createObjectBuilder().add(NAME, allowedValue).build()).printOn(media); } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java index 8f7f6cc2..5d7a4c8f 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java @@ -46,7 +46,7 @@ public ValidationVocabulary(final JsonProvider jsonContext) { this.vocab = new DefaultVocabulary( URI.create("https://json-schema.org/draft/2020-12/vocab/validation"), new TypeKeywordType(), - new AnyKeywordType(ConstKeyword.NAME, ConstKeyword::new), + new AnyKeywordType(jsonContext, ConstKeyword.NAME, ConstKeyword::new), new ArrayKeywordType(EnumKeyword.NAME, EnumKeyword::new), new ObjectKeywordType(DependentRequiredKeyword.NAME, DependentRequiredKeyword::new), new IntegerKeywordType(jsonContext, MaxPropertiesKeyword.NAME, MaxPropertiesKeyword::new),