Skip to content

Commit bfa801d

Browse files
committed
Refactor SchemaRegistry
1 parent bbff591 commit bfa801d

File tree

140 files changed

+431
-659
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+431
-659
lines changed

src/main/java/com/networknt/schema/Schema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Used for creating a schema with validators for validating inputs. This is
43-
* created using {@link SchemaRegistry#getInstance(Version, Consumer)}
43+
* created using {@link SchemaRegistry#withDefaultSpecificationVersion(Version, Consumer)}
4444
* and should be cached for performance.
4545
* <p>
4646
* This is the core of json constraint implementation. It parses json constraint

src/main/java/com/networknt/schema/SchemaRegistry.java

Lines changed: 84 additions & 171 deletions
Large diffs are not rendered by default.

src/main/java/com/networknt/schema/SpecificationVersionDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Optional<Version> detectOptionalVersion(JsonNode jsonNode, boolean
7575
return Optional.ofNullable(jsonNode.get(SCHEMA_TAG)).map(schemaTag -> {
7676

7777
String schemaTagValue = schemaTag.asText();
78-
String schemaUri = SchemaRegistry.normalizeMetaSchemaUri(schemaTagValue);
78+
String schemaUri = SchemaRegistry.normalizeDialectId(schemaTagValue);
7979

8080
if (throwIfUnsupported) {
8181
return Version.fromDialectId(schemaUri)

src/main/java/com/networknt/schema/dialect/DefaultDialectRegistry.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Map;
1919
import java.util.Map.Entry;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.ConcurrentMap;
2022

2123
import com.fasterxml.jackson.databind.JsonNode;
2224
import com.networknt.schema.Error;
@@ -32,12 +34,14 @@
3234
* Default {@link DialectRegistry}.
3335
*/
3436
public class DefaultDialectRegistry implements DialectRegistry {
37+
private final ConcurrentMap<String, Dialect> dialects = new ConcurrentHashMap<>();
38+
3539
@Override
3640
public Dialect getDialect(String dialectId, SchemaRegistry schemaFactory, SchemaValidatorsConfig config) {
3741
// Is it a well-known dialect?
3842
return Specification.Version.fromDialectId(dialectId).map(SchemaRegistry::checkVersion).orElseGet(() -> {
3943
// Custom dialect
40-
return loadDialect(dialectId, schemaFactory, config);
44+
return dialects.computeIfAbsent(dialectId, id -> loadDialect(id, schemaFactory, config));
4145
});
4246
}
4347

src/main/java/com/networknt/schema/dialect/DialectRegistry.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ public interface DialectRegistry {
2929
* Gets the dialect given the dialect id which is the IRI that indicates the
3030
* meta-schema that can be used to validate the schema conforms to the dialect.
3131
*
32-
* @param dialectId the dialect id of the dialect which IRI that indicates
33-
* the meta-schema that can be used to validate the schema
34-
* conforms to the dialect
35-
* @param schemaFactory the schema factory
36-
* @param config the config
32+
* @param dialectId the dialect id of the dialect which IRI that indicates
33+
* the meta-schema that can be used to validate the schema
34+
* conforms to the dialect
35+
* @param schemaRegistry the schema registry to fetch and load unknown dialect's
36+
* meta-schema
37+
* @param config the config
3738
* @return the dialect
3839
*/
39-
Dialect getDialect(String dialectId, SchemaRegistry schemaFactory, SchemaValidatorsConfig config);
40+
Dialect getDialect(String dialectId, SchemaRegistry schemaRegistry, SchemaValidatorsConfig config);
4041
}

src/main/java/com/networknt/schema/dialect/DisallowUnknownDialectFactory.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/test/java/com/networknt/schema/AbstractJsonSchemaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private JsonNode getJsonNodeFromPath(String dataPath) {
6060

6161
private Schema getJsonSchema(JsonNode schemaNode) {
6262
return SchemaRegistry
63-
.getInstance(SpecificationVersionDetector.detectOptionalVersion(schemaNode, false).orElse(DEFAULT_VERSION_FLAG))
63+
.withDefaultSpecificationVersion(SpecificationVersionDetector.detectOptionalVersion(schemaNode, false).orElse(DEFAULT_VERSION_FLAG))
6464
.getSchema(schemaNode);
6565
}
6666

src/test/java/com/networknt/schema/AbstractJsonSchemaTestSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
202202
}
203203
};
204204
Version specVersion = detectVersion(testCase.getSchema(), testCase.getSpecification(), defaultVersion, false);
205-
SchemaRegistry base = SchemaRegistry.getInstance(specVersion);
205+
SchemaRegistry base = SchemaRegistry.withDefaultSpecificationVersion(specVersion);
206206
return SchemaRegistry
207207
.builder(base)
208208
.schemaMappers(schemaMappers -> schemaMappers

src/test/java/com/networknt/schema/AdditionalPropertiesValidatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void messageFalse() {
4545
+ " },\r\n"
4646
+ " \"additionalProperties\": false\r\n"
4747
+ "}";
48-
SchemaRegistry factory = SchemaRegistry.getInstance(Version.DRAFT_2020_12);
48+
SchemaRegistry factory = SchemaRegistry.withDefaultSpecificationVersion(Version.DRAFT_2020_12);
4949
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().build();
5050
Schema schema = factory.getSchema(schemaData, config);
5151
String inputData = "{\r\n"
@@ -80,7 +80,7 @@ void messageSchema() {
8080
+ " },\r\n"
8181
+ " \"additionalProperties\": { \"type\": \"number\" }\r\n"
8282
+ "}";
83-
SchemaRegistry factory = SchemaRegistry.getInstance(Version.DRAFT_2020_12);
83+
SchemaRegistry factory = SchemaRegistry.withDefaultSpecificationVersion(Version.DRAFT_2020_12);
8484
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().build();
8585
Schema schema = factory.getSchema(schemaData, config);
8686
String inputData = "{\r\n"

src/test/java/com/networknt/schema/AllOfValidatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void invalidTypeShouldThrowJsonSchemaException() {
3333
+ " \"$ref\": \"#/defs/User\"\r\n"
3434
+ " }\r\n"
3535
+ "}";
36-
SchemaRegistry factory = SchemaRegistry.getInstance(Version.DRAFT_2020_12);
36+
SchemaRegistry factory = SchemaRegistry.withDefaultSpecificationVersion(Version.DRAFT_2020_12);
3737
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
3838
assertEquals("type", ex.getError().getMessageKey());
3939
}
@@ -59,7 +59,7 @@ void walkValidationWithNullNodeShouldNotValidate() {
5959

6060
String jsonContents = "{}";
6161

62-
SchemaRegistry factory = SchemaRegistry.getInstance(Specification.Version.DRAFT_7);
62+
SchemaRegistry factory = SchemaRegistry.withDefaultSpecificationVersion(Specification.Version.DRAFT_7);
6363
Schema schema = factory.getSchema(schemaContents);
6464
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
6565
assertEquals(true, result.getErrors().isEmpty());

0 commit comments

Comments
 (0)