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..a7d4bed3 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,22 @@ 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 +54,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..c3b8ccfe 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,8 @@ 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 d8914192..dd4670ce 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), diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeywordTest.java index ea31cde7..15edcad9 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeywordTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ConstKeywordTest.java @@ -35,6 +35,7 @@ import jakarta.json.Json; import jakarta.json.JsonObject; import jakarta.json.JsonValue; +import jakarta.json.spi.JsonProvider; import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; @@ -139,7 +140,7 @@ void should_be_printable() { } private static Keyword createKeywordFrom(final JsonObject json) { - return new AnyKeywordType("const", ConstKeyword::new).createKeyword( + return new AnyKeywordType(JsonProvider.provider(), "const", ConstKeyword::new).createKeyword( new DefaultJsonSchemaFactory().create(json) ); }