Skip to content
Merged
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 gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ junit-junit4 = { group = "junit", name = "junit", version = "4.13.2" }
junit-jupiter = { group = "org.junit.jupiter", name = "junit-jupiter", version = "5.14.3" }

groovy-json = { group = "org.codehaus.groovy", name = "groovy-json", version = "3.0.25" }
json-schema-validator = { group = "com.networknt", name = "json-schema-validator", version = "1.5.9" }
json-schema-validator = { group = "com.networknt", name = "json-schema-validator", version = "2.0.3" }
jetbrains-annotations = { group = "org.jetbrains", name = "annotations", version = "26.1.0" }
google-gson = { group = "com.google.code.gson", name = "gson", version = "2.13.2" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package org.gradle.github.dependencygraph

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.networknt.schema.*
import com.networknt.schema.Error
import com.networknt.schema.Schema
import com.networknt.schema.SchemaException
import com.networknt.schema.SchemaRegistry
import com.networknt.schema.SpecificationVersion
import groovy.json.JsonSlurper
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
Expand Down Expand Up @@ -251,15 +255,15 @@ abstract class BaseExtractorTest extends Specification {
protected Map jsonRepositorySnapshot() {
def jsonSlurper = new JsonSlurper()
println(dependencyGraphFile.text)
JsonSchema schema = createSchemaValidator()
Schema schema = createSchemaValidator()
ObjectMapper mapper = new ObjectMapper()
JsonNode node = mapper.readTree(dependencyGraphFile)
validateAgainstJsonSchema(schema, node)
return jsonSlurper.parse(dependencyGraphFile) as Map
}

private static void validateAgainstJsonSchema(JsonSchema schema, JsonNode json) {
final Set<ValidationMessage> validationMessages = schema.validate(json)
private static void validateAgainstJsonSchema(Schema schema, JsonNode json) {
final List<Error> validationMessages = schema.validate(json)
if (!validationMessages.isEmpty()) {
final String newline = System.lineSeparator()
final String violationMessage = createViolationMessage(newline, validationMessages)
Expand All @@ -270,24 +274,22 @@ abstract class BaseExtractorTest extends Specification {
}

@CompileDynamic
private static String createViolationMessage(String newline, Set<ValidationMessage> validationMessages) {
private static String createViolationMessage(String newline, List<Error> validationMessages) {
return validationMessages
.stream()
.map(ValidationMessage::getMessage)
.map(Error::getMessage)
.collect(Collectors.joining(newline + " - ", " - ", ""))
}

private static JsonSchema createSchemaValidator() {
final JsonSchemaFactory factory =
JsonSchemaFactory
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4))
.build()
private static Schema createSchemaValidator() {
final SchemaRegistry registry =
SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_4)
try {
return factory
return registry
.getSchema(
JsonRepositorySnapshotLoader.class.classLoader.getResourceAsStream(SCHEMA)
)
} catch (JsonSchemaException ex) {
} catch (SchemaException ex) {
throw new RuntimeException(
"Unable to load dependency constraints schema (resource: " + SCHEMA + ")",
ex
Expand Down
Loading