Skip to content

Commit

Permalink
feat: add preloadMetaSchema method without draft argument
Browse files Browse the repository at this point in the history
This is a convenience method which fetches the draft version from the repository if it is set in the options.

Signed-off-by: Pascal Krause <[email protected]>
  • Loading branch information
pk-work committed Nov 25, 2022
1 parent d5ca2b8 commit 7ac1f9f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/main/java/io/vertx/json/schema/SchemaRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* A repository is a holder of dereferenced schemas, it can be used to create validator instances for a specific schema.
*
* <p>
* This is to be used when multiple schema objects compose the global schema to be used for validation.
*
* @author Paulo Lopes
Expand All @@ -28,6 +28,7 @@ public interface SchemaRepository {

/**
* Create a repository with some initial configuration.
*
* @param options the initial configuration
* @return a repository
*/
Expand All @@ -39,27 +40,39 @@ 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;

/**
* 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
*/
Expand All @@ -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
*/
Expand All @@ -94,15 +107,15 @@ 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
*/
Validator validator(String ref, JsonSchemaOptions options);

/**
* Tries to resolve all internal and repository local references. External references are not resolved.
*
* <p>
* 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.
*
Expand All @@ -113,7 +126,7 @@ static SchemaRepository create(JsonSchemaOptions options) {

/**
* Tries to resolve all internal and repository local references. External references are not resolved.
*
* <p>
* 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.
*
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> metaSchemaIds;
switch (draft) {
case DRAFT4:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -51,4 +56,23 @@ void testPreloadMetaSchema(Draft draft, List<String> 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));
}
}

0 comments on commit 7ac1f9f

Please sign in to comment.