Skip to content

Commit

Permalink
Merge pull request #122 from sebastian-toepfer/improve_performance_an…
Browse files Browse the repository at this point in the history
…ykeytypes

provide a jsonprovider to const keyword
  • Loading branch information
sebastian-toepfer authored May 30, 2024
2 parents 718cc94 + 77e37c9 commit 17f657c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonValue, Keyword> keywordCreator;
private final BiFunction<JsonProvider, JsonValue, Keyword> keywordCreator;

public AnyKeywordType(final String name, final Function<JsonValue, Keyword> keywordCreator) {
public AnyKeywordType(
final JsonProvider jsonContext,
final String name,
final BiFunction<JsonProvider, JsonValue, Keyword> keywordCreator
) {
this.jsonContext = Objects.requireNonNull(jsonContext);
this.name = Objects.requireNonNull(name);
this.keywordCreator = Objects.requireNonNull(keywordCreator);
}
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

Expand Down Expand Up @@ -79,6 +81,8 @@ public boolean hasName(final String name) {

@Override
public <T extends Media<T>> 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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
);
}
Expand Down

0 comments on commit 17f657c

Please sign in to comment.