diff --git a/src/main/java/io/vertx/json/schema/SchemaRepository.java b/src/main/java/io/vertx/json/schema/SchemaRepository.java index 362156e8..f11574cc 100644 --- a/src/main/java/io/vertx/json/schema/SchemaRepository.java +++ b/src/main/java/io/vertx/json/schema/SchemaRepository.java @@ -18,7 +18,7 @@ /** * A repository is a holder of dereferenced schemas, it can be used to create validator instances for a specific schema. - * + *

* This is to be used when multiple schema objects compose the global schema to be used for validation. * * @author Paulo Lopes @@ -28,6 +28,7 @@ public interface SchemaRepository { /** * Create a repository with some initial configuration. + * * @param options the initial configuration * @return a repository */ @@ -39,8 +40,8 @@ static SchemaRepository create(JsonSchemaOptions options) { * Dereferences a schema to the repository. * * @param schema a new schema to list - * @throws SchemaException when a schema is already present for the same id * @return a repository + * @throws SchemaException when a schema is already present for the same id */ @Fluent SchemaRepository dereference(JsonSchema schema) throws SchemaException; @@ -48,18 +49,30 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * Dereferences a schema to the repository. * - * @param uri the source of the schema used for de-referencing, optionally relative to - * {@link JsonSchemaOptions#getBaseUri()}. + * @param uri the source of the schema used for de-referencing, optionally relative to + * {@link JsonSchemaOptions#getBaseUri()}. * @param schema a new schema to list - * @throws SchemaException when a schema is already present for the same id * @return a repository + * @throws SchemaException when a schema is already present for the same id */ @Fluent SchemaRepository dereference(String uri, JsonSchema schema) throws SchemaException; /** - * Preloads the repository with the meta schemas for the related draft version. + * Preloads the repository with the meta schemas for the related @link {@link Draft} version. The related draft version + * is determined from the {@link JsonSchemaOptions}, in case that no draft is set in the options an + * {@link IllegalStateException} is thrown. + * * @param fs The Vert.x file system to load the related schema meta files from classpath + * @return a repository + */ + @Fluent + SchemaRepository preloadMetaSchema(FileSystem fs); + + /** + * Preloads the repository with the meta schemas for the related draft version. + * + * @param fs The Vert.x file system to load the related schema meta files from classpath * @param draft The draft version of the meta files to load * @return a repository */ @@ -85,7 +98,7 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * A new validator instance overriding this repository options. * - * @param schema the start validation schema + * @param schema the start validation schema * @param options the options to be using on the validator instance * @return the validator */ @@ -94,7 +107,7 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * A new validator instance overriding this repository options. * - * @param ref the start validation reference in JSON pointer format + * @param ref the start validation reference in JSON pointer format * @param options the options to be using on the validator instance * @return the validator */ @@ -102,7 +115,7 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * Tries to resolve all internal and repository local references. External references are not resolved. - * + *

* The result is an object where all references have been resolved. Resolution of references is shallow. This * should normally not be a problem for this use case. * @@ -113,7 +126,7 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * Tries to resolve all internal and repository local references. External references are not resolved. - * + *

* The result is an object where all references have been resolved. Resolution of references is shallow. This * should normally not be a problem for this use case. * @@ -125,6 +138,7 @@ static SchemaRepository create(JsonSchemaOptions options) { /** * Look up a schema using a JSON pointer notation + * * @param pointer the JSON pointer * @return the schema */ diff --git a/src/main/java/io/vertx/json/schema/impl/SchemaRepositoryImpl.java b/src/main/java/io/vertx/json/schema/impl/SchemaRepositoryImpl.java index 41198240..c2f157aa 100644 --- a/src/main/java/io/vertx/json/schema/impl/SchemaRepositoryImpl.java +++ b/src/main/java/io/vertx/json/schema/impl/SchemaRepositoryImpl.java @@ -118,7 +118,16 @@ public SchemaRepository dereference(String uri, JsonSchema schema) throws Schema return this; } - @Override public SchemaRepository preloadMetaSchema(FileSystem fs, Draft draft) { + @Override + public SchemaRepository preloadMetaSchema(FileSystem fs) { + if (options.getDraft() == null) { + throw new IllegalStateException("No draft version is defined in the options of the repository"); + } + return preloadMetaSchema(fs, options.getDraft()); + } + + @Override + public SchemaRepository preloadMetaSchema(FileSystem fs, Draft draft) { List metaSchemaIds; switch (draft) { case DRAFT4: diff --git a/src/test/java/io/vertx/json/schema/impl/SchemaRepositoryImplTest.java b/src/test/java/io/vertx/json/schema/impl/SchemaRepositoryImplTest.java index 8d902bc9..14835b74 100644 --- a/src/test/java/io/vertx/json/schema/impl/SchemaRepositoryImplTest.java +++ b/src/test/java/io/vertx/json/schema/impl/SchemaRepositoryImplTest.java @@ -6,6 +6,9 @@ import io.vertx.json.schema.JsonSchemaOptions; import io.vertx.json.schema.SchemaRepository; import io.vertx.junit5.VertxExtension; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -22,7 +25,9 @@ import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_202012_META_FILES; import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_4_META_FILES; import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_7_META_FILES; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.endsWith; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -51,4 +56,23 @@ void testPreloadMetaSchema(Draft draft, List ids, Vertx vertx) { verify(fileSystemSpy).readFileBlocking(endsWith(classpath)); } } + + @Test + @DisplayName("preloadMetaSchema(fs) should throw an error if no draft is set in the options") + void testPreloadMetaSchemaException(Vertx vertx) { + JsonSchemaOptions opts = new JsonSchemaOptions().setBaseUri("https://example.org"); + SchemaRepository repo = SchemaRepository.create(opts); + + Assertions.assertThrows(IllegalStateException.class, () -> repo.preloadMetaSchema(vertx.fileSystem())); + } + + @Test + @DisplayName("preloadMetaSchema(fs) should get the draft from the options") + void testPreloadMetaSchemaDraftFromOptions(Vertx vertx) { + JsonSchemaOptions opts = new JsonSchemaOptions().setBaseUri("https://example.org").setDraft(DRAFT4); + SchemaRepository repoSpy = spy(SchemaRepository.create(opts)); + repoSpy.preloadMetaSchema(vertx.fileSystem()); + + verify(repoSpy).preloadMetaSchema(any(), eq(DRAFT4)); + } }