Skip to content

Commit bf5f620

Browse files
committed
Rename JsonMetaSchemaFactory to DialectRegistry
1 parent c7940b5 commit bf5f620

File tree

9 files changed

+127
-113
lines changed

9 files changed

+127
-113
lines changed

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

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

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

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

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.networknt.schema.Specification.Version;
22+
import com.networknt.schema.dialect.DefaultDialectRegistry;
2223
import com.networknt.schema.dialect.Dialect;
2324
import com.networknt.schema.dialect.DialectId;
25+
import com.networknt.schema.dialect.DialectRegistry;
2426
import com.networknt.schema.dialect.Dialects;
2527
import com.networknt.schema.resource.DefaultSchemaLoader;
2628
import com.networknt.schema.resource.SchemaLoader;
@@ -64,7 +66,7 @@ public static class Builder {
6466
private SchemaLoaders.Builder schemaLoadersBuilder = null;
6567
private SchemaMappers.Builder schemaMappersBuilder = null;
6668
private boolean enableSchemaCache = true;
67-
private JsonMetaSchemaFactory metaSchemaFactory = null;
69+
private DialectRegistry metaSchemaFactory = null;
6870

6971
/**
7072
* Sets the json node reader to read the data.
@@ -118,7 +120,7 @@ public Builder defaultMetaSchemaIri(final String defaultMetaSchemaIri) {
118120
return this;
119121
}
120122

121-
public Builder metaSchemaFactory(final JsonMetaSchemaFactory jsonMetaSchemaFactory) {
123+
public Builder metaSchemaFactory(final DialectRegistry jsonMetaSchemaFactory) {
122124
this.metaSchemaFactory = jsonMetaSchemaFactory;
123125
return this;
124126
}
@@ -196,7 +198,7 @@ public JsonSchemaFactory build() {
196198
private final ConcurrentMap<String, Dialect> metaSchemas;
197199
private final ConcurrentMap<SchemaLocation, JsonSchema> schemaCache = new ConcurrentHashMap<>();
198200
private final boolean enableSchemaCache;
199-
private final JsonMetaSchemaFactory metaSchemaFactory;
201+
private final DialectRegistry metaSchemaFactory;
200202

201203
private static final List<SchemaLoader> DEFAULT_SCHEMA_LOADERS = SchemaLoaders.builder().build();
202204
private static final List<SchemaMapper> DEFAULT_SCHEMA_MAPPERS = SchemaMappers.builder().build();
@@ -210,7 +212,7 @@ private JsonSchemaFactory(
210212
SchemaMappers.Builder schemaMappersBuilder,
211213
ConcurrentMap<String, Dialect> metaSchemas,
212214
boolean enableSchemaCache,
213-
JsonMetaSchemaFactory metaSchemaFactory) {
215+
DialectRegistry metaSchemaFactory) {
214216
this.metaSchemas = metaSchemas;
215217
if (defaultMetaSchemaIri == null || defaultMetaSchemaIri.trim().isEmpty()) {
216218
throw new IllegalArgumentException("defaultMetaSchemaIri must not be null or empty");
@@ -468,8 +470,8 @@ public Dialect getMetaSchema(String iri, SchemaValidatorsConfig config) {
468470
* @return the meta-schema
469471
*/
470472
protected Dialect loadMetaSchema(String iri, SchemaValidatorsConfig config) {
471-
return this.metaSchemaFactory != null ? this.metaSchemaFactory.getMetaSchema(iri, this, config)
472-
: DefaultJsonMetaSchemaFactory.getInstance().getMetaSchema(iri, this, config);
473+
return this.metaSchemaFactory != null ? this.metaSchemaFactory.getDialect(iri, this, config)
474+
: DefaultDialectRegistry.getInstance().getDialect(iri, this, config);
473475
}
474476

475477
JsonNode readTree(String content, InputFormat inputFormat) throws IOException {

src/main/java/com/networknt/schema/DefaultJsonMetaSchemaFactory.java renamed to src/main/java/com/networknt/schema/dialect/DefaultDialectRegistry.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,47 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.networknt.schema;
16+
package com.networknt.schema.dialect;
1717

1818
import java.util.Map;
1919
import java.util.Map.Entry;
2020

2121
import com.fasterxml.jackson.databind.JsonNode;
22+
import com.networknt.schema.Error;
23+
import com.networknt.schema.InvalidSchemaException;
24+
import com.networknt.schema.JsonSchema;
25+
import com.networknt.schema.JsonSchemaFactory;
26+
import com.networknt.schema.SchemaLocation;
27+
import com.networknt.schema.SchemaValidatorsConfig;
28+
import com.networknt.schema.Specification;
2229
import com.networknt.schema.Specification.Version;
23-
import com.networknt.schema.dialect.Dialect;
2430

2531
/**
26-
* Default {@link JsonMetaSchemaFactory}.
32+
* Default {@link DialectRegistry}.
2733
*/
28-
public class DefaultJsonMetaSchemaFactory implements JsonMetaSchemaFactory {
34+
public class DefaultDialectRegistry implements DialectRegistry {
2935
@Override
30-
public Dialect getMetaSchema(String iri, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config) {
36+
public Dialect getDialect(String dialectId, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config) {
3137
// Is it a well-known dialect?
32-
return Specification.Version.fromDialectId(iri)
33-
.map(JsonSchemaFactory::checkVersion)
34-
.orElseGet(() -> {
35-
// Custom meta schema
36-
return loadMetaSchema(iri, schemaFactory, config);
37-
});
38+
return Specification.Version.fromDialectId(dialectId).map(JsonSchemaFactory::checkVersion).orElseGet(() -> {
39+
// Custom dialect
40+
return loadDialect(dialectId, schemaFactory, config);
41+
});
3842
}
3943

40-
protected Dialect loadMetaSchema(String iri, JsonSchemaFactory schemaFactory,
41-
SchemaValidatorsConfig config) {
44+
protected Dialect loadDialect(String iri, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config) {
4245
try {
43-
Dialect result = loadMetaSchemaBuilder(iri, schemaFactory, config).build();
46+
Dialect result = loadDialectBuilder(iri, schemaFactory, config).build();
4447
return result;
4548
} catch (InvalidSchemaException e) {
4649
throw e;
4750
} catch (Exception e) {
48-
Error error = Error.builder()
49-
.message("Failed to load meta-schema ''{0}''").arguments(iri).build();
51+
Error error = Error.builder().message("Failed to load dialect ''{0}''").arguments(iri).build();
5052
throw new InvalidSchemaException(error, e);
5153
}
5254
}
5355

54-
protected Dialect.Builder loadMetaSchemaBuilder(String iri, JsonSchemaFactory schemaFactory,
56+
protected Dialect.Builder loadDialectBuilder(String iri, JsonSchemaFactory schemaFactory,
5557
SchemaValidatorsConfig config) {
5658
JsonSchema schema = schemaFactory.getSchema(SchemaLocation.of(iri), config);
5759
Dialect.Builder builder = Dialect.builder(iri, schema.getValidationContext().getMetaSchema());
@@ -72,10 +74,10 @@ protected Dialect.Builder loadMetaSchemaBuilder(String iri, JsonSchemaFactory sc
7274
}
7375

7476
private static class Holder {
75-
private static final DefaultJsonMetaSchemaFactory INSTANCE = new DefaultJsonMetaSchemaFactory();
77+
private static final DefaultDialectRegistry INSTANCE = new DefaultDialectRegistry();
7678
}
7779

78-
public static DefaultJsonMetaSchemaFactory getInstance() {
80+
public static DefaultDialectRegistry getInstance() {
7981
return Holder.INSTANCE;
8082
}
8183
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema.dialect;
17+
18+
import com.networknt.schema.JsonSchemaFactory;
19+
import com.networknt.schema.SchemaValidatorsConfig;
20+
21+
/**
22+
* Registry for {@link Dialect} that can be retrieved using the dialect id which
23+
* is the IRI that indicates the meta-schema that can be used to validate the
24+
* schema conforms to the dialect.
25+
*/
26+
@FunctionalInterface
27+
public interface DialectRegistry {
28+
/**
29+
* Gets the dialect given the dialect id which is the IRI that indicates the
30+
* meta-schema that can be used to validate the schema conforms to the dialect.
31+
*
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
37+
* @return the dialect
38+
*/
39+
Dialect getDialect(String dialectId, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config);
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema.dialect;
17+
18+
import com.networknt.schema.Error;
19+
import com.networknt.schema.InvalidSchemaException;
20+
import com.networknt.schema.JsonSchemaFactory;
21+
import com.networknt.schema.SchemaValidatorsConfig;
22+
23+
/**
24+
* A {@link DialectRegistry} that does not meta-schemas that aren't
25+
* explicitly configured in the {@link JsonSchemaFactory}.
26+
*/
27+
public class DisallowUnknownDialectFactory implements DialectRegistry {
28+
@Override
29+
public Dialect getDialect(String dialectId, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config) {
30+
throw new InvalidSchemaException(Error.builder()
31+
.message("Unknown dialect ''{0}''. Only dialects that are explicitly configured can be used.")
32+
.arguments(dialectId).build());
33+
}
34+
35+
private static class Holder {
36+
private static final DisallowUnknownDialectFactory INSTANCE = new DisallowUnknownDialectFactory();
37+
}
38+
39+
/**
40+
* Gets the instance of {@link DisallowUnknownDialectFactory}.
41+
*
42+
* @return the json meta schema factory
43+
*/
44+
public static DisallowUnknownDialectFactory getInstance() {
45+
return Holder.INSTANCE;
46+
}
47+
}

src/test/java/com/networknt/schema/DisallowUnknownJsonMetaSchemaFactoryTest.java renamed to src/test/java/com/networknt/schema/DisallowUnknownDialectFactoryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import com.networknt.schema.Specification.Version;
24+
import com.networknt.schema.dialect.DisallowUnknownDialectFactory;
2425

2526
/**
2627
* Tests for DisallowUnknownJsonMetaSchemaFactory.
2728
*/
28-
class DisallowUnknownJsonMetaSchemaFactoryTest {
29+
class DisallowUnknownDialectFactoryTest {
2930
private static final String DRAFT_202012_SCHEMA = "{\r\n"
3031
+ " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
3132
+ " \"type\": \"object\"\r\n"
@@ -45,15 +46,15 @@ void defaultHandling() {
4546
@Test
4647
void draft202012() {
4748
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(Version.DRAFT_2020_12,
48-
builder -> builder.metaSchemaFactory(DisallowUnknownJsonMetaSchemaFactory.getInstance()));
49+
builder -> builder.metaSchemaFactory(DisallowUnknownDialectFactory.getInstance()));
4950
assertDoesNotThrow(() -> factory.getSchema(DRAFT_202012_SCHEMA));
5051
assertThrows(InvalidSchemaException.class, () -> factory.getSchema(DRAFT_7_SCHEMA));
5152
}
5253

5354
@Test
5455
void draft7() {
5556
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(Version.DRAFT_7,
56-
builder -> builder.metaSchemaFactory(DisallowUnknownJsonMetaSchemaFactory.getInstance()));
57+
builder -> builder.metaSchemaFactory(DisallowUnknownDialectFactory.getInstance()));
5758
assertDoesNotThrow(() -> factory.getSchema(DRAFT_7_SCHEMA));
5859
assertThrows(InvalidSchemaException.class, () -> factory.getSchema(DRAFT_202012_SCHEMA));
5960
}

src/test/java/com/networknt/schema/oas/OpenApi30Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.junit.jupiter.api.Test;
2626

27-
import com.networknt.schema.DisallowUnknownJsonMetaSchemaFactory;
2827
import com.networknt.schema.InputFormat;
2928
import com.networknt.schema.JsonSchema;
3029
import com.networknt.schema.JsonSchemaFactory;
@@ -33,6 +32,7 @@
3332
import com.networknt.schema.SchemaLocation;
3433
import com.networknt.schema.SchemaValidatorsConfig;
3534
import com.networknt.schema.Specification.Version;
35+
import com.networknt.schema.dialect.DisallowUnknownDialectFactory;
3636
import com.networknt.schema.dialect.OpenApi30;
3737
import com.networknt.schema.Error;
3838

@@ -48,7 +48,7 @@ void validateMetaSchema() {
4848
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(Version.DRAFT_7,
4949
builder -> builder.metaSchema(OpenApi30.getInstance())
5050
.defaultMetaSchemaIri(OpenApi30.getInstance().getIri())
51-
.metaSchemaFactory(DisallowUnknownJsonMetaSchemaFactory.getInstance()));
51+
.metaSchemaFactory(DisallowUnknownDialectFactory.getInstance()));
5252
JsonSchema schema = factory.getSchema(SchemaLocation.of(
5353
"classpath:schema/oas/3.0/petstore.yaml#/paths/~1pet/post/requestBody/content/application~1json/schema"));
5454
String input = "{\r\n"

0 commit comments

Comments
 (0)