Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.5.9</version>
<version>2.0.1</version>
</dependency>

<!-- Unit Test -->
Expand Down
46 changes: 18 additions & 28 deletions src/main/java/org/cyclonedx/CycloneDxSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SchemaValidatorsConfig;
import com.networknt.schema.SpecVersionDetector;
import com.networknt.schema.resource.MapSchemaMapper;
import com.networknt.schema.SchemaRegistry;
import com.networknt.schema.SchemaRegistryConfig;
import com.networknt.schema.serialization.DefaultNodeReader;
import org.cyclonedx.generators.json.BomJsonGenerator;
import org.cyclonedx.generators.xml.BomXmlGenerator;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -82,36 +80,28 @@ public abstract class CycloneDxSchema
* @throws IOException when errors are encountered
* @since 6.0.0
*/
public JsonSchema getJsonSchema(Version schemaVersion, final ObjectMapper mapper)
public com.networknt.schema.Schema getJsonSchema(Version schemaVersion, final ObjectMapper mapper)
throws IOException
{
final InputStream spdxInstream = getJsonSchemaAsStream(schemaVersion);
final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPreloadJsonSchema(false);
final SchemaRegistryConfig config = SchemaRegistryConfig.builder().preloadSchema(false).build();

final Map<String, String> offlineMappings = new HashMap<>();
offlineMappings.put("http://cyclonedx.org/schema/spdx.schema.json",
getClass().getClassLoader().getResource("spdx.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/jsf-0.82.schema.json",
getClass().getClassLoader().getResource("jsf-0.82.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.2.schema.json",
getClass().getClassLoader().getResource("bom-1.2-strict.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.3.schema.json",
getClass().getClassLoader().getResource("bom-1.3-strict.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.4.schema.json",
getClass().getClassLoader().getResource("bom-1.4.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.5.schema.json",
getClass().getClassLoader().getResource("bom-1.5.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.6.schema.json",
getClass().getClassLoader().getResource("bom-1.6.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/spdx.schema.json", "classpath:spdx.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/jsf-0.82.schema.json", "classpath:jsf-0.82.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/bom-1.2.schema.json", "classpath:bom-1.2-strict.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/bom-1.3.schema.json", "classpath:bom-1.3-strict.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/bom-1.4.schema.json", "classpath:bom-1.4.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/bom-1.5.schema.json", "classpath:bom-1.5.schema.json");
offlineMappings.put("http://cyclonedx.org/schema/bom-1.6.schema.json", "classpath:bom-1.6.schema.json");

JsonNode schemaNode = mapper.readTree(spdxInstream);
final MapSchemaMapper offlineSchemaMapper = new MapSchemaMapper(offlineMappings);
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schemaNode)))
.jsonMapper(mapper)
.schemaMappers(s -> s.add(offlineSchemaMapper))
.build();
return factory.getSchema(schemaNode, config);
SchemaRegistry registry = SchemaRegistry.builder()
.nodeReader(DefaultNodeReader.builder().jsonMapper(mapper).build())
.schemaIdResolvers(b -> b.mappings(offlineMappings))
.schemaRegistryConfig(config)
.build();
return registry.getSchema(schemaNode);
}

private InputStream getJsonSchemaAsStream(final Version schemaVersion) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/cyclonedx/parsers/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.Error;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.cyclonedx.CycloneDxSchema;
Expand All @@ -36,7 +36,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

Set<ValidationMessage> errors = getJsonSchema(schemaVersion, mapper).validate(mapper.readTree(bomJson.toString()));
for (ValidationMessage message: errors) {
List<Error> errors = getJsonSchema(schemaVersion, mapper).validate(mapper.readTree(bomJson.toString()));
for (Error message: errors) {
exceptions.add(new ParseException(message.getMessage()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/cyclonedx/parse/JsonParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testValidateBomPrior12() throws IOException {

assertThat(exceptions.stream().map(ParseException::getMessage)).containsExactly(
"CycloneDX version 1.1 does not support the JSON format",
"$: unknown found, object expected"
"unknown found, object expected"
);
}
}