Skip to content

Commit

Permalink
Merge branch 'windows_build' of github.com:creek-service/json-schema-…
Browse files Browse the repository at this point in the history
…validation-comparison into windows_build
  • Loading branch information
big-andy-coates committed Nov 19, 2023
2 parents d39a2aa + d38d9a2 commit 1f4cc6d
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

/*
Implementation does not seem to currently provide a way to programmatically turn on format assertions.
Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec.
*/

final Validator validator = validator(spec, additionalSchemas);
final URI schemaUri = validator.registerSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

/*
Implementation does not seem to currently provide a way to programmatically turn on format assertions.
Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec.
*/

final Object schemaObject = parse(schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ default boolean supports(final SchemaSpec spec) {
* @param spec the spec of the schema
* @param additionalSchemas accessor to meta-schemas and JSON-Schema-Test-Suite 'remote'
* schemas.
* @param enableFormatAssertions Turn on format assertions for schemas versions where format
* assertions should be off by default.
* @return a validator instance that be can be used to validate, serialise and deserialise JSON.
*/
JsonValidator prepare(String schema, SchemaSpec spec, AdditionalSchemas additionalSchemas);
JsonValidator prepare(
String schema,
SchemaSpec spec,
AdditionalSchemas additionalSchemas,
boolean enableFormatAssertions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {
return new JsonValidator() {
@Override
public void validate(final String json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

final JsonValidationService service = JsonValidationService.newInstance();
final JsonSchema parsedSchema = parseSchema(service, schema, spec, additionalSchemas);
final JsonSchema parsedSchema =
parseSchema(service, schema, spec, additionalSchemas, enableFormatAssertions);
return new JsonValidator() {
@Override
public void validate(final String json) {
Expand Down Expand Up @@ -127,17 +131,19 @@ private JsonSchema parseSchema(
final JsonValidationService service,
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas) {
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {
final JsonSchemaResolver resolver =
uri -> {
final String s = additionalSchemas.load(uri);
return parseSchema(service, s, spec, additionalSchemas);
return parseSchema(service, s, spec, additionalSchemas, enableFormatAssertions);
};

return service.createSchemaReaderFactoryBuilder()
.withDefaultSpecVersion(schemaVersion(spec))
.withSchemaResolver(resolver)
.withSchemaValidation(false)
.withStrictFormats(enableFormatAssertions)
.build()
.createSchemaReader(
new ByteArrayInputStream(schema.getBytes(StandardCharsets.UTF_8)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,22 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {
final JsonSchemaVersion version = schemaVersion(spec);

// Doesn't seem to be a way to reactively 'load' schema on demand:
// Only to provide them all, which means they ALL get parsed... slow!
/*
Implementation does not seem to currently provide a way to programmatically turn on format assertions.
Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec.
*/

/*
There doesn't seem to be a way to reactively 'load' schema on demand:
Only to provide them all upfront, which means they ALL get parsed... slow!
*/

final List<SchemaSource> schemas =
new ArrayList<>(additionalSchema(additionalSchemas, version));
schemas.add(MetaSchemaSource.Companion.forVersion(version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

/*
Implementation does not seem to currently provide a way to programmatically turn on format assertions.
Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec.
*/

final JsonSchema parsedSchema = parseSchema(schema, spec, additionalSchemas);

return new JsonValidator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

final Schema parsedSchema = parseSchema(schema, spec, additionalSchemas::load);
final Validator validator = new Validator(true);
final Validator validator = new Validator(enableFormatAssertions);

return new JsonValidator() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

/*
Waiting on https://github.com/erosb/json-sKema/commit/ee8aca8c1452688dba485fa7cc7f3ab4fc85d74c being released
before we can make use of enableFormatAssertions
*/

final JsonValue schemaJson = new JsonParser(schema).parse();

final SchemaLoader schemaLoader =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {
final Validator validator =
createValidator(schema, spec, Optional.of(additionalSchemas.remotesDir()));
createValidator(
schema,
spec,
Optional.of(additionalSchemas.remotesDir()),
enableFormatAssertions);

return new JsonValidator() {
@Override
Expand Down Expand Up @@ -132,7 +139,10 @@ private static JsonElement parse(final byte[] bytes) {

@SuppressWarnings({"CollectionContainsUrl", "OptionalUsedAsFieldOrParameterType"})
private Validator createValidator(
final String schema, final SchemaSpec spec, final Optional<Path> remotesDir) {
final String schema,
final SchemaSpec spec,
final Optional<Path> remotesDir,
final boolean enableFormatAssertions) {
try {
final Map<URI, URL> knownURLs =
remotesDir.isPresent()
Expand All @@ -142,7 +152,7 @@ private Validator createValidator(
: Map.of();

final Options opts = new Options();
opts.set(Option.FORMAT, true);
opts.set(Option.FORMAT, enableFormatAssertions);
opts.set(Option.CONTENT, true);
opts.set(Option.DEFAULT_SPECIFICATION, schemaVersion(spec));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public MetaData metadata() {

@Override
public JsonValidator prepare(
final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) {
final String schema,
final SchemaSpec spec,
final AdditionalSchemas additionalSchemas,
final boolean enableFormatAssertions) {

/*
Implementation does not seem to currently provide a way to programmatically turn on format assertions.
Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec.
*/

final Object decodedSchema = Json.decodeValue(schema);

final JsonSchema parsedSchema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,17 @@ private static class ImplementationState {
? impl.prepare(
TestSchemas.DRAFT_7_SCHEMA,
SchemaSpec.DRAFT_07,
new AdditionalSchemas(Map.of(), Path.of("")))
new AdditionalSchemas(Map.of(), Path.of("")),
false)
: null;

this.validator2020 =
impl.supports(SchemaSpec.DRAFT_2020_12)
? impl.prepare(
TestSchemas.DRAFT_2020_SCHEMA,
SchemaSpec.DRAFT_2020_12,
new AdditionalSchemas(Map.of(), Path.of("")))
new AdditionalSchemas(Map.of(), Path.of("")),
false)
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import static java.util.Objects.requireNonNull;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
Expand Down Expand Up @@ -134,10 +136,16 @@ private Executable<List<TestResult>> prepareSuite(
.collect(Collectors.toList());
}

@SuppressFBWarnings(
value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE",
justification = "Known not to be null")
private JsonValidator prepareValidator(
final SchemaSpec spec, final TestSuite suite, final Implementation implementation) {
try {
return implementation.prepare(suite.schema(), spec, additionalSchemas);
final boolean format =
Paths.get("format").equals(suite.filePath().getParent().getFileName());
return implementation.prepare(
suite.schema(), spec, additionalSchemas, suite.optional() && format);
} catch (final Throwable t) {
final RuntimeException e = new RuntimeException("Failed to build validator", t);
return new JsonValidator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void shouldNotThrowPreparingValidSchema(final String shortName, final Implementa
final TestData testData = testData(impl);

// When:
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);

// Then: did not throw.
}
Expand All @@ -102,7 +102,7 @@ void shouldThrowValidatingInvalidJson(final String shortName, final Implementati
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);
final String badJson =
new String(validator.serialize(BAD_DECIMAL, false), StandardCharsets.UTF_8);

Expand All @@ -120,7 +120,7 @@ void shouldNotThrowValidatingValidJson(final String shortName, final Implementat
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);
final String goodJson =
new String(
validator.serialize(ModelState.TEST_MODEL, false), StandardCharsets.UTF_8);
Expand All @@ -145,7 +145,7 @@ void shouldHandleRemoteSchemas(final String shortName, final Implementation impl
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.remoteSchema, testData.spec, additionalSchemas);
impl.prepare(testData.remoteSchema, testData.spec, additionalSchemas, false);

// Then:
validator.validate("100");
Expand All @@ -158,7 +158,7 @@ void shouldRoundTrip(final String shortName, final Implementation impl) {
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);

// When:
final byte[] bytes = validator.serialize(ModelState.TEST_MODEL, true);
Expand All @@ -177,7 +177,7 @@ void shouldValidateOnSerialize(final String shortName, final Implementation impl
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);

// Then:
assertThrows(RuntimeException.class, () -> validator.serialize(BAD_DECIMAL, true));
Expand All @@ -189,7 +189,7 @@ void shouldNotValidateOnSerialize(final String shortName, final Implementation i
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);

// When:
validator.serialize(BAD_DECIMAL, false);
Expand All @@ -206,7 +206,7 @@ void shouldValidateOnDeserialize(final String shortName, final Implementation im
// Given:
final TestData testData = testData(impl);
final Implementation.JsonValidator validator =
impl.prepare(testData.schema, testData.spec, additionalSchemas);
impl.prepare(testData.schema, testData.spec, additionalSchemas, false);
final byte[] serialized = validator.serialize(BAD_DECIMAL, false);

// Then:
Expand Down

0 comments on commit 1f4cc6d

Please sign in to comment.