Skip to content

Commit 0594786

Browse files
committed
Rename Specification.Version to SpecificationVersion and refactor
1 parent 0ccbbc7 commit 0594786

File tree

151 files changed

+516
-579
lines changed

Some content is hidden

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

151 files changed

+516
-579
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import com.fasterxml.jackson.databind.JsonNode;
3333
import com.fasterxml.jackson.databind.node.ObjectNode;
34-
import com.networknt.schema.Specification.Version;
3534
import com.networknt.schema.keyword.DiscriminatorValidator;
3635
import com.networknt.schema.keyword.KeywordValidator;
3736
import com.networknt.schema.keyword.TypeValidator;
@@ -51,7 +50,7 @@
5150
* modified.
5251
*/
5352
public class Schema implements Validator {
54-
private static final long DRAFT_2019_09_VALUE = Version.DRAFT_2019_09.getOrder();
53+
private static final long DRAFT_2019_09_VALUE = SpecificationVersion.DRAFT_2019_09.getOrder();
5554
private final String id;
5655

5756
/**
@@ -656,7 +655,7 @@ private List<KeywordValidator> read(JsonNode schemaNode) {
656655
}
657656

658657
// Ignore siblings for older drafts
659-
if (null != refValidator && getSchemaContext().getDialect().getSpecification().getOrder() < DRAFT_2019_09_VALUE) {
658+
if (null != refValidator && getSchemaContext().getDialect().getSpecificationVersion().getOrder() < DRAFT_2019_09_VALUE) {
660659
validators.clear();
661660
validators.add(refValidator);
662661
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package com.networknt.schema;
1818

19-
import java.util.Optional;
2019
import java.util.concurrent.ConcurrentHashMap;
2120
import java.util.concurrent.ConcurrentMap;
2221

2322
import com.fasterxml.jackson.databind.JsonNode;
24-
import com.networknt.schema.Specification.Version;
2523
import com.networknt.schema.dialect.Dialect;
2624
import com.networknt.schema.keyword.KeywordValidator;
2725

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.Specification.Version;
2120
import com.networknt.schema.dialect.BasicDialectRegistry;
2221
import com.networknt.schema.dialect.DefaultDialectRegistry;
2322
import com.networknt.schema.dialect.Dialect;
@@ -174,7 +173,7 @@ public SchemaLoader getSchemaLoader() {
174173
* Builder without keywords or formats.
175174
* <p>
176175
* Typically {@link #builder(SchemaRegistry)} or
177-
* {@link #withDefaultDialect(Version)} or {@link #withDialect(Dialect)} would be used instead.
176+
* {@link #withDefaultDialect(SpecificationVersion)} or {@link #withDialect(Dialect)} would be used instead.
178177
*
179178
* @return a builder instance without any keywords or formats - usually not what
180179
* one needs.
@@ -192,7 +191,7 @@ public static Builder builder() {
192191
* not specify the $schema keyword
193192
* @return the factory
194193
*/
195-
public static SchemaRegistry withDefaultDialect(Specification.Version specificationVersion) {
194+
public static SchemaRegistry withDefaultDialect(SpecificationVersion specificationVersion) {
196195
return withDefaultDialect(specificationVersion, null);
197196
}
198197

@@ -206,7 +205,7 @@ public static SchemaRegistry withDefaultDialect(Specification.Version specificat
206205
* @param customizer to customize the registry
207206
* @return the factory
208207
*/
209-
public static SchemaRegistry withDefaultDialect(Specification.Version specificationVersion,
208+
public static SchemaRegistry withDefaultDialect(SpecificationVersion specificationVersion,
210209
Consumer<SchemaRegistry.Builder> customizer) {
211210
Dialect dialect = Specification.getDialect(specificationVersion);
212211
return withDefaultDialectId(dialect.getId(), customizer);
@@ -628,7 +627,7 @@ private boolean isYaml(final SchemaLocation schemaUri) {
628627
*/
629628
static protected String normalizeDialectId(String id) {
630629
boolean found = false;
631-
for (Version flag : Specification.Version.values()) {
630+
for (SpecificationVersion flag : SpecificationVersion.values()) {
632631
if(flag.getDialectId().equals(id)) {
633632
found = true;
634633
break;

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

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,96 +15,26 @@
1515
*/
1616
package com.networknt.schema;
1717

18-
import java.util.Optional;
19-
2018
import com.networknt.schema.dialect.Dialect;
2119
import com.networknt.schema.dialect.DialectId;
2220
import com.networknt.schema.dialect.Dialects;
2321

2422
/**
25-
* The JSON Schema specification which defines the standard dialects.
23+
* The version of the JSON Schema specification that defines the standard
24+
* dialects.
2625
*/
2726
public class Specification {
2827

2928
/**
30-
* The JSON Schema specification version.
31-
*/
32-
public enum Version {
33-
/**
34-
* Draft 4.
35-
*/
36-
DRAFT_4(4, DialectId.DRAFT_4),
37-
/**
38-
* Draft 6.
39-
*/
40-
DRAFT_6(6, DialectId.DRAFT_6),
41-
/**
42-
* Draft 7.
43-
*/
44-
DRAFT_7(7, DialectId.DRAFT_7),
45-
/**
46-
* Draft 2019-09.
47-
*/
48-
DRAFT_2019_09(8, DialectId.DRAFT_2019_09),
49-
/**
50-
* Draft 2020-12.
51-
*/
52-
DRAFT_2020_12(9, DialectId.DRAFT_2020_12);
53-
54-
private final int order;
55-
private final String dialectId;
56-
57-
Version(int order, String dialectId) {
58-
this.order = order;
59-
this.dialectId = dialectId;
60-
}
61-
62-
/**
63-
* Gets the dialect id used for the $schema keyword. The dialect id is an IRI
64-
* that identifies the meta schema used to validate the dialect.
65-
*
66-
* @return the dialect id
67-
*/
68-
public String getDialectId() {
69-
return this.dialectId;
70-
}
71-
72-
/**
73-
* Gets the unique release order of the specification version used that
74-
* indicates when the specification was released. Lower numbers indicate the
75-
* specification was released earlier.
76-
*
77-
* @return the order when the specification was released
78-
*/
79-
public int getOrder() {
80-
return this.order;
81-
}
82-
83-
/**
84-
* Gets the specification version that matches the dialect id indicated by
85-
* $schema keyword. The dialect id is an IRI that identifies the meta schema
86-
* used to validate the dialect.
87-
*
88-
* @param dialectId the dialect id specified by $schema keyword
89-
* @return the specification version if it matches the dialect id
90-
*/
91-
public static Optional<Version> fromDialectId(String dialectId) {
92-
for (Version version : Version.values()) {
93-
if (version.dialectId.equals(dialectId)) {
94-
return Optional.of(version);
95-
}
96-
}
97-
return Optional.empty();
98-
}
99-
}
100-
101-
/**
102-
* Gets the dialect given the specification version.
29+
* Gets the standard dialect given the specification version.
30+
* <p>
31+
* This should only be used if the standard dialect is required, otherwise the
32+
* dialect should be retrieved from the dialect registry.
10333
*
10434
* @param version the schema specification version
10535
* @return the dialect or null if not found
10636
*/
107-
public static Dialect getDialect(Specification.Version version) {
37+
public static Dialect getDialect(SpecificationVersion version) {
10838
if (null == version) {
10939
return null;
11040
}
@@ -125,7 +55,10 @@ public static Dialect getDialect(Specification.Version version) {
12555
}
12656

12757
/**
128-
* Gets the dialect given the dialect id.
58+
* Gets the standard dialect given the dialect id.
59+
* <p>
60+
* This should only be used if the standard dialect is required, otherwise the
61+
* dialect should be retrieved from the dialect registry.
12962
*
13063
* @param dialectId the schema specification version
13164
* @return the dialect or null if not found
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2020 Network New Technologies Inc.
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;
17+
18+
import java.util.Optional;
19+
20+
import com.networknt.schema.dialect.DialectId;
21+
22+
/**
23+
* The version of the JSON Schema specification that defines the standard
24+
* dialects.
25+
*/
26+
public enum SpecificationVersion {
27+
/**
28+
* Draft 4.
29+
*/
30+
DRAFT_4(4, DialectId.DRAFT_4),
31+
/**
32+
* Draft 6.
33+
*/
34+
DRAFT_6(6, DialectId.DRAFT_6),
35+
/**
36+
* Draft 7.
37+
*/
38+
DRAFT_7(7, DialectId.DRAFT_7),
39+
/**
40+
* Draft 2019-09.
41+
*/
42+
DRAFT_2019_09(8, DialectId.DRAFT_2019_09),
43+
/**
44+
* Draft 2020-12.
45+
*/
46+
DRAFT_2020_12(9, DialectId.DRAFT_2020_12);
47+
48+
private final int order;
49+
private final String dialectId;
50+
51+
SpecificationVersion(int order, String dialectId) {
52+
this.order = order;
53+
this.dialectId = dialectId;
54+
}
55+
56+
/**
57+
* Gets the dialect id used for the $schema keyword. The dialect id is an IRI
58+
* that identifies the meta schema used to validate the dialect.
59+
*
60+
* @return the dialect id
61+
*/
62+
public String getDialectId() {
63+
return this.dialectId;
64+
}
65+
66+
/**
67+
* Gets the unique release order of the specification version used that
68+
* indicates when the specification was released. Lower numbers indicate the
69+
* specification was released earlier.
70+
*
71+
* @return the order when the specification was released
72+
*/
73+
public int getOrder() {
74+
return this.order;
75+
}
76+
77+
/**
78+
* Gets the specification version that matches the dialect id indicated by
79+
* $schema keyword. The dialect id is an IRI that identifies the meta schema
80+
* used to validate the dialect.
81+
*
82+
* @param dialectId the dialect id specified by $schema keyword
83+
* @return the specification version if it matches the dialect id
84+
*/
85+
public static Optional<SpecificationVersion> fromDialectId(String dialectId) {
86+
for (SpecificationVersion version : SpecificationVersion.values()) {
87+
if (version.dialectId.equals(dialectId)) {
88+
return Optional.of(version);
89+
}
90+
}
91+
return Optional.empty();
92+
}
93+
}

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.Specification.Version;
2120

2221
import java.nio.file.Path;
2322
import java.util.HashMap;
@@ -35,15 +34,15 @@
3534
*/
3635
public final class SpecificationVersionDetector {
3736

38-
private static final Map<String, Version> supportedVersions = new HashMap<>();
37+
private static final Map<String, SpecificationVersion> supportedVersions = new HashMap<>();
3938
private static final String SCHEMA_TAG = "$schema";
4039

4140
static {
42-
supportedVersions.put("draft2019-09", Version.DRAFT_2019_09);
43-
supportedVersions.put("draft2020-12", Version.DRAFT_2020_12);
44-
supportedVersions.put("draft4", Version.DRAFT_4);
45-
supportedVersions.put("draft6", Version.DRAFT_6);
46-
supportedVersions.put("draft7", Version.DRAFT_7);
41+
supportedVersions.put("draft2019-09", SpecificationVersion.DRAFT_2019_09);
42+
supportedVersions.put("draft2020-12", SpecificationVersion.DRAFT_2020_12);
43+
supportedVersions.put("draft4", SpecificationVersion.DRAFT_4);
44+
supportedVersions.put("draft6", SpecificationVersion.DRAFT_6);
45+
supportedVersions.put("draft7", SpecificationVersion.DRAFT_7);
4746
}
4847

4948
private SpecificationVersionDetector() {
@@ -57,7 +56,7 @@ private SpecificationVersionDetector() {
5756
* @param jsonNode JSON Node to read from
5857
* @return Spec version if present, otherwise throws an exception
5958
*/
60-
public static Version detect(JsonNode jsonNode) {
59+
public static SpecificationVersion detect(JsonNode jsonNode) {
6160
return detectOptionalVersion(jsonNode, true).orElseThrow(
6261
() -> new SchemaException("'" + SCHEMA_TAG + "' tag is not present")
6362
);
@@ -71,25 +70,25 @@ public static Version detect(JsonNode jsonNode) {
7170
* @param throwIfUnsupported whether to throw an exception if the version is not supported
7271
* @return Spec version if present, otherwise empty
7372
*/
74-
public static Optional<Version> detectOptionalVersion(JsonNode jsonNode, boolean throwIfUnsupported) {
73+
public static Optional<SpecificationVersion> detectOptionalVersion(JsonNode jsonNode, boolean throwIfUnsupported) {
7574
return Optional.ofNullable(jsonNode.get(SCHEMA_TAG)).map(schemaTag -> {
7675

7776
String schemaTagValue = schemaTag.asText();
7877
String schemaUri = SchemaRegistry.normalizeDialectId(schemaTagValue);
7978

8079
if (throwIfUnsupported) {
81-
return Version.fromDialectId(schemaUri)
80+
return SpecificationVersion.fromDialectId(schemaUri)
8281
.orElseThrow(() -> new SchemaException("'" + schemaTagValue + "' is unrecognizable schema"));
8382
} else {
84-
return Version.fromDialectId(schemaUri).orElse(null);
83+
return SpecificationVersion.fromDialectId(schemaUri).orElse(null);
8584
}
8685
});
8786
}
8887

8988

9089
// For 2019-09 and later published drafts, implementations that are able to
9190
// detect the draft of each schema via $schema SHOULD be configured to do so
92-
public static Version detectVersion(JsonNode jsonNode, Path specification, Version defaultVersion, boolean throwIfUnsupported) {
91+
public static SpecificationVersion detectVersion(JsonNode jsonNode, Path specification, SpecificationVersion defaultVersion, boolean throwIfUnsupported) {
9392
return Stream.of(
9493
detectOptionalVersion(jsonNode, throwIfUnsupported),
9594
detectVersionFromPath(specification)
@@ -103,7 +102,7 @@ public static Version detectVersion(JsonNode jsonNode, Path specification, Versi
103102
// For draft-07 and earlier, draft-next, and implementations unable to
104103
// detect via $schema, implementations MUST be configured to expect the
105104
// draft matching the test directory name
106-
public static Optional<Version> detectVersionFromPath(Path path) {
105+
public static Optional<SpecificationVersion> detectVersionFromPath(Path path) {
107106
return StreamSupport.stream(path.spliterator(), false)
108107
.map(Path::toString)
109108
.map(supportedVersions::get)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.networknt.schema.SchemaRegistry;
2828
import com.networknt.schema.SchemaLocation;
2929
import com.networknt.schema.Specification;
30-
import com.networknt.schema.Specification.Version;
30+
import com.networknt.schema.SpecificationVersion;
3131

3232
/**
3333
* Default {@link DialectRegistry}.
@@ -60,9 +60,9 @@ protected Dialect loadDialect(String iri, SchemaRegistry schemaFactory) {
6060
protected Dialect.Builder loadDialectBuilder(String iri, SchemaRegistry schemaFactory) {
6161
Schema schema = schemaFactory.getSchema(SchemaLocation.of(iri));
6262
Dialect.Builder builder = Dialect.builder(iri, schema.getSchemaContext().getDialect());
63-
Version specification = schema.getSchemaContext().getDialect().getSpecification();
63+
SpecificationVersion specification = schema.getSchemaContext().getDialect().getSpecificationVersion();
6464
if (specification != null) {
65-
if (specification.getOrder() >= Version.DRAFT_2019_09.getOrder()) {
65+
if (specification.getOrder() >= SpecificationVersion.DRAFT_2019_09.getOrder()) {
6666
// Process vocabularies
6767
JsonNode vocabulary = schema.getSchemaNode().get("$vocabulary");
6868
if (vocabulary != null) {

0 commit comments

Comments
 (0)