Skip to content

Commit 3fd3198

Browse files
authored
Merge pull request #802 from pedroboado/bump-json-schema-validator-2.x
Update usages of com.networknt:json-schema-validator to the new API. ( 1.5.9 -> 2.0.1 )
2 parents 7848b12 + b72fdc7 commit 3fd3198

4 files changed

Lines changed: 23 additions & 34 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
<dependency>
171171
<groupId>com.networknt</groupId>
172172
<artifactId>json-schema-validator</artifactId>
173-
<version>1.5.9</version>
173+
<version>2.0.1</version>
174174
</dependency>
175175

176176
<!-- Unit Test -->

src/main/java/org/cyclonedx/CycloneDxSchema.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020

2121
import com.fasterxml.jackson.databind.JsonNode;
2222
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.networknt.schema.JsonSchema;
24-
import com.networknt.schema.JsonSchemaFactory;
25-
import com.networknt.schema.SchemaValidatorsConfig;
26-
import com.networknt.schema.SpecVersionDetector;
27-
import com.networknt.schema.resource.MapSchemaMapper;
23+
import com.networknt.schema.SchemaRegistry;
24+
import com.networknt.schema.SchemaRegistryConfig;
25+
import com.networknt.schema.serialization.DefaultNodeReader;
2826
import org.cyclonedx.generators.json.BomJsonGenerator;
2927
import org.cyclonedx.generators.xml.BomXmlGenerator;
3028
import org.xml.sax.SAXException;
@@ -82,36 +80,28 @@ public abstract class CycloneDxSchema
8280
* @throws IOException when errors are encountered
8381
* @since 6.0.0
8482
*/
85-
public JsonSchema getJsonSchema(Version schemaVersion, final ObjectMapper mapper)
83+
public com.networknt.schema.Schema getJsonSchema(Version schemaVersion, final ObjectMapper mapper)
8684
throws IOException
8785
{
8886
final InputStream spdxInstream = getJsonSchemaAsStream(schemaVersion);
89-
final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
90-
config.setPreloadJsonSchema(false);
87+
final SchemaRegistryConfig config = SchemaRegistryConfig.builder().preloadSchema(false).build();
9188

9289
final Map<String, String> offlineMappings = new HashMap<>();
93-
offlineMappings.put("http://cyclonedx.org/schema/spdx.schema.json",
94-
getClass().getClassLoader().getResource("spdx.schema.json").toExternalForm());
95-
offlineMappings.put("http://cyclonedx.org/schema/jsf-0.82.schema.json",
96-
getClass().getClassLoader().getResource("jsf-0.82.schema.json").toExternalForm());
97-
offlineMappings.put("http://cyclonedx.org/schema/bom-1.2.schema.json",
98-
getClass().getClassLoader().getResource("bom-1.2-strict.schema.json").toExternalForm());
99-
offlineMappings.put("http://cyclonedx.org/schema/bom-1.3.schema.json",
100-
getClass().getClassLoader().getResource("bom-1.3-strict.schema.json").toExternalForm());
101-
offlineMappings.put("http://cyclonedx.org/schema/bom-1.4.schema.json",
102-
getClass().getClassLoader().getResource("bom-1.4.schema.json").toExternalForm());
103-
offlineMappings.put("http://cyclonedx.org/schema/bom-1.5.schema.json",
104-
getClass().getClassLoader().getResource("bom-1.5.schema.json").toExternalForm());
105-
offlineMappings.put("http://cyclonedx.org/schema/bom-1.6.schema.json",
106-
getClass().getClassLoader().getResource("bom-1.6.schema.json").toExternalForm());
90+
offlineMappings.put("http://cyclonedx.org/schema/spdx.schema.json", "classpath:spdx.schema.json");
91+
offlineMappings.put("http://cyclonedx.org/schema/jsf-0.82.schema.json", "classpath:jsf-0.82.schema.json");
92+
offlineMappings.put("http://cyclonedx.org/schema/bom-1.2.schema.json", "classpath:bom-1.2-strict.schema.json");
93+
offlineMappings.put("http://cyclonedx.org/schema/bom-1.3.schema.json", "classpath:bom-1.3-strict.schema.json");
94+
offlineMappings.put("http://cyclonedx.org/schema/bom-1.4.schema.json", "classpath:bom-1.4.schema.json");
95+
offlineMappings.put("http://cyclonedx.org/schema/bom-1.5.schema.json", "classpath:bom-1.5.schema.json");
96+
offlineMappings.put("http://cyclonedx.org/schema/bom-1.6.schema.json", "classpath:bom-1.6.schema.json");
10797

10898
JsonNode schemaNode = mapper.readTree(spdxInstream);
109-
final MapSchemaMapper offlineSchemaMapper = new MapSchemaMapper(offlineMappings);
110-
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schemaNode)))
111-
.jsonMapper(mapper)
112-
.schemaMappers(s -> s.add(offlineSchemaMapper))
113-
.build();
114-
return factory.getSchema(schemaNode, config);
99+
SchemaRegistry registry = SchemaRegistry.builder()
100+
.nodeReader(DefaultNodeReader.builder().jsonMapper(mapper).build())
101+
.schemaIdResolvers(b -> b.mappings(offlineMappings))
102+
.schemaRegistryConfig(config)
103+
.build();
104+
return registry.getSchema(schemaNode);
115105
}
116106

117107
private InputStream getJsonSchemaAsStream(final Version schemaVersion) {

src/main/java/org/cyclonedx/parsers/JsonParser.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import com.fasterxml.jackson.databind.JsonNode;
2222
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.networknt.schema.ValidationMessage;
23+
import com.networknt.schema.Error;
2424
import org.apache.commons.io.FileUtils;
2525
import org.apache.commons.io.IOUtils;
2626
import org.cyclonedx.CycloneDxSchema;
@@ -36,7 +36,6 @@
3636
import java.nio.charset.StandardCharsets;
3737
import java.util.ArrayList;
3838
import java.util.List;
39-
import java.util.Set;
4039

4140
/**
4241
* JsonParser is responsible for validating and parsing CycloneDX bill-of-material
@@ -182,8 +181,8 @@ public List<ParseException> validate(final JsonNode bomJson, final Version schem
182181
);
183182
}
184183

185-
Set<ValidationMessage> errors = getJsonSchema(schemaVersion, mapper).validate(mapper.readTree(bomJson.toString()));
186-
for (ValidationMessage message: errors) {
184+
List<Error> errors = getJsonSchema(schemaVersion, mapper).validate(mapper.readTree(bomJson.toString()));
185+
for (Error message: errors) {
187186
exceptions.add(new ParseException(message.getMessage()));
188187
}
189188

src/test/java/org/cyclonedx/parse/JsonParseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testValidateBomPrior12() throws IOException {
6363

6464
assertThat(exceptions.stream().map(ParseException::getMessage)).containsExactly(
6565
"CycloneDX version 1.1 does not support the JSON format",
66-
"$: unknown found, object expected"
66+
"unknown found, object expected"
6767
);
6868
}
6969
}

0 commit comments

Comments
 (0)