Skip to content

Commit 7ac1f9f

Browse files
committed
feat: add preloadMetaSchema method without draft argument
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]>
1 parent d5ca2b8 commit 7ac1f9f

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/main/java/io/vertx/json/schema/SchemaRepository.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

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

2929
/**
3030
* Create a repository with some initial configuration.
31+
*
3132
* @param options the initial configuration
3233
* @return a repository
3334
*/
@@ -39,27 +40,39 @@ static SchemaRepository create(JsonSchemaOptions options) {
3940
* Dereferences a schema to the repository.
4041
*
4142
* @param schema a new schema to list
42-
* @throws SchemaException when a schema is already present for the same id
4343
* @return a repository
44+
* @throws SchemaException when a schema is already present for the same id
4445
*/
4546
@Fluent
4647
SchemaRepository dereference(JsonSchema schema) throws SchemaException;
4748

4849
/**
4950
* Dereferences a schema to the repository.
5051
*
51-
* @param uri the source of the schema used for de-referencing, optionally relative to
52-
* {@link JsonSchemaOptions#getBaseUri()}.
52+
* @param uri the source of the schema used for de-referencing, optionally relative to
53+
* {@link JsonSchemaOptions#getBaseUri()}.
5354
* @param schema a new schema to list
54-
* @throws SchemaException when a schema is already present for the same id
5555
* @return a repository
56+
* @throws SchemaException when a schema is already present for the same id
5657
*/
5758
@Fluent
5859
SchemaRepository dereference(String uri, JsonSchema schema) throws SchemaException;
5960

6061
/**
61-
* Preloads the repository with the meta schemas for the related draft version.
62+
* Preloads the repository with the meta schemas for the related @link {@link Draft} version. The related draft version
63+
* is determined from the {@link JsonSchemaOptions}, in case that no draft is set in the options an
64+
* {@link IllegalStateException} is thrown.
65+
*
6266
* @param fs The Vert.x file system to load the related schema meta files from classpath
67+
* @return a repository
68+
*/
69+
@Fluent
70+
SchemaRepository preloadMetaSchema(FileSystem fs);
71+
72+
/**
73+
* Preloads the repository with the meta schemas for the related draft version.
74+
*
75+
* @param fs The Vert.x file system to load the related schema meta files from classpath
6376
* @param draft The draft version of the meta files to load
6477
* @return a repository
6578
*/
@@ -85,7 +98,7 @@ static SchemaRepository create(JsonSchemaOptions options) {
8598
/**
8699
* A new validator instance overriding this repository options.
87100
*
88-
* @param schema the start validation schema
101+
* @param schema the start validation schema
89102
* @param options the options to be using on the validator instance
90103
* @return the validator
91104
*/
@@ -94,15 +107,15 @@ static SchemaRepository create(JsonSchemaOptions options) {
94107
/**
95108
* A new validator instance overriding this repository options.
96109
*
97-
* @param ref the start validation reference in JSON pointer format
110+
* @param ref the start validation reference in JSON pointer format
98111
* @param options the options to be using on the validator instance
99112
* @return the validator
100113
*/
101114
Validator validator(String ref, JsonSchemaOptions options);
102115

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

114127
/**
115128
* Tries to resolve all internal and repository local references. External references are not resolved.
116-
*
129+
* <p>
117130
* The result is an object where all references have been resolved. Resolution of references is shallow. This
118131
* should normally not be a problem for this use case.
119132
*
@@ -125,6 +138,7 @@ static SchemaRepository create(JsonSchemaOptions options) {
125138

126139
/**
127140
* Look up a schema using a JSON pointer notation
141+
*
128142
* @param pointer the JSON pointer
129143
* @return the schema
130144
*/

src/main/java/io/vertx/json/schema/impl/SchemaRepositoryImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,16 @@ public SchemaRepository dereference(String uri, JsonSchema schema) throws Schema
118118
return this;
119119
}
120120

121-
@Override public SchemaRepository preloadMetaSchema(FileSystem fs, Draft draft) {
121+
@Override
122+
public SchemaRepository preloadMetaSchema(FileSystem fs) {
123+
if (options.getDraft() == null) {
124+
throw new IllegalStateException("No draft version is defined in the options of the repository");
125+
}
126+
return preloadMetaSchema(fs, options.getDraft());
127+
}
128+
129+
@Override
130+
public SchemaRepository preloadMetaSchema(FileSystem fs, Draft draft) {
122131
List<String> metaSchemaIds;
123132
switch (draft) {
124133
case DRAFT4:

src/test/java/io/vertx/json/schema/impl/SchemaRepositoryImplTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import io.vertx.json.schema.JsonSchemaOptions;
77
import io.vertx.json.schema.SchemaRepository;
88
import io.vertx.junit5.VertxExtension;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
912
import org.junit.jupiter.api.extension.ExtendWith;
1013
import org.junit.jupiter.params.ParameterizedTest;
1114
import org.junit.jupiter.params.provider.Arguments;
@@ -22,7 +25,9 @@
2225
import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_202012_META_FILES;
2326
import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_4_META_FILES;
2427
import static io.vertx.json.schema.impl.SchemaRepositoryImpl.DRAFT_7_META_FILES;
28+
import static org.mockito.ArgumentMatchers.any;
2529
import static org.mockito.ArgumentMatchers.endsWith;
30+
import static org.mockito.ArgumentMatchers.eq;
2631
import static org.mockito.Mockito.spy;
2732
import static org.mockito.Mockito.verify;
2833

@@ -51,4 +56,23 @@ void testPreloadMetaSchema(Draft draft, List<String> ids, Vertx vertx) {
5156
verify(fileSystemSpy).readFileBlocking(endsWith(classpath));
5257
}
5358
}
59+
60+
@Test
61+
@DisplayName("preloadMetaSchema(fs) should throw an error if no draft is set in the options")
62+
void testPreloadMetaSchemaException(Vertx vertx) {
63+
JsonSchemaOptions opts = new JsonSchemaOptions().setBaseUri("https://example.org");
64+
SchemaRepository repo = SchemaRepository.create(opts);
65+
66+
Assertions.assertThrows(IllegalStateException.class, () -> repo.preloadMetaSchema(vertx.fileSystem()));
67+
}
68+
69+
@Test
70+
@DisplayName("preloadMetaSchema(fs) should get the draft from the options")
71+
void testPreloadMetaSchemaDraftFromOptions(Vertx vertx) {
72+
JsonSchemaOptions opts = new JsonSchemaOptions().setBaseUri("https://example.org").setDraft(DRAFT4);
73+
SchemaRepository repoSpy = spy(SchemaRepository.create(opts));
74+
repoSpy.preloadMetaSchema(vertx.fileSystem());
75+
76+
verify(repoSpy).preloadMetaSchema(any(), eq(DRAFT4));
77+
}
5478
}

0 commit comments

Comments
 (0)