Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#522 Replace JSON Schema library #550

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 3 additions & 13 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ maven/mavencentral/com.aayushatharva.brotli4j/native-windows-x86_64/1.11.0, Apac
maven/mavencentral/com.aayushatharva.brotli4j/service/1.11.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.apicatalog/titanium-json-ld/1.3.2, Apache-2.0, approved, #8912
maven/mavencentral/com.carrotsearch.thirdparty/simple-xml-safe/2.7.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.damnhandy/handy-uri-templates/2.1.8, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.eatthepath/fast-uuid/0.2.0, MIT, approved, clearlydefined
maven/mavencentral/com.ethlo.time/itu/1.8.0, Apache-2.0, approved, #12927
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.15.4, Apache-2.0, approved, #7947
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.16.1, Apache-2.0, approved, #11606
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.15.4, MIT AND Apache-2.0, approved, #7932
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.16.1, Apache-2.0 AND MIT, approved, #11602
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.15.1, Apache-2.0, approved, #7934
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.15.4, Apache-2.0, approved, #7934
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.16.1, Apache-2.0, approved, #11605
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.15.1, Apache-2.0, approved, #8802
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.15.3, Apache-2.0, approved, #8802
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.15.4, Apache-2.0, approved, #8802
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jakarta-jsonp/2.15.2, Apache-2.0, approved, #9179
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jakarta-jsonp/2.15.4, Apache-2.0, approved, #9179
Expand All @@ -45,8 +45,8 @@ maven/mavencentral/com.google.guava/failureaccess/1.0.2, Apache-2.0, approved, C
maven/mavencentral/com.google.guava/guava/33.0.0-jre, Apache-2.0 AND CC0-1.0, approved, #12173
maven/mavencentral/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava, Apache-2.0, approved, CQ22657
maven/mavencentral/com.google.j2objc/j2objc-annotations/2.8, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.ibm.icu/icu4j/74.2, , approved, #11936
maven/mavencentral/com.jayway.jsonpath/json-path/2.9.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.networknt/json-schema-validator/1.4.0, Apache-2.0 AND Unicode-TOU, approved, #13812
maven/mavencentral/com.nimbusds/content-type/2.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/lang-tag/1.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
Expand Down Expand Up @@ -190,7 +190,6 @@ maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0,
maven/mavencentral/jakarta.ws.rs/jakarta.ws.rs-api/3.1.0, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.rest
maven/mavencentral/jakarta.xml.bind/jakarta.xml.bind-api/4.0.2, BSD-3-Clause, approved, ee4j.jaxb
maven/mavencentral/javax.jms/javax.jms-api/2.0.1, CDDL-1.1 OR GPL-2.0 WITH Classpath-exception-2.0, approved, #1516
maven/mavencentral/joda-time/joda-time/2.10.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/junit/junit/4.13.2, EPL-2.0, approved, CQ23636
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.12, Apache-2.0, approved, #7164
maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.4, Apache-2.0, approved, #7164
Expand All @@ -203,9 +202,6 @@ maven/mavencentral/net.java.dev.jna/jna/5.13.0, Apache-2.0 AND LGPL-2.1-or-later
maven/mavencentral/net.javacrumbs.json-unit/json-unit-assertj/3.2.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.javacrumbs.json-unit/json-unit-core/3.2.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.javacrumbs.json-unit/json-unit-json-path/3.2.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.jimblackler.jsonschemafriend/core/0.12.4, Apache-2.0, approved, #10911
maven/mavencentral/net.jimblackler.jsonschemafriend/extra/0.12.4, Apache-2.0, approved, #10910
maven/mavencentral/net.jimblackler/jsonschemafriend/0.12.4, Apache-2.0, approved, #10912
maven/mavencentral/net.jodah/typetools/0.6.3, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.minidev/accessors-smart/2.4.11, Apache-2.0, approved, #7515
maven/mavencentral/net.minidev/accessors-smart/2.5.1, Apache-2.0, approved, clearlydefined
Expand Down Expand Up @@ -364,10 +360,6 @@ maven/mavencentral/org.glassfish.jersey.media/jersey-media-json-jackson/3.1.5, E
maven/mavencentral/org.glassfish.jersey.media/jersey-media-multipart/3.1.3, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jersey
maven/mavencentral/org.glassfish.jersey.media/jersey-media-multipart/3.1.5, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jersey
maven/mavencentral/org.glassfish/jakarta.json/2.0.1, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jsonp
maven/mavencentral/org.graalvm.js/js/21.3.9, UPL-1.0 AND (UPL-1.0 AND GPL-2.0-only WITH Classpath-exception-2.0) AND BSD-3-Clause AND MPL-2.0, approved, #6714
maven/mavencentral/org.graalvm.regex/regex/21.3.9, UPL-1.0 AND Unicode-TOU, approved, #6719
maven/mavencentral/org.graalvm.sdk/graal-sdk/21.3.9, UPL-1.0, approved, #6717
maven/mavencentral/org.graalvm.truffle/truffle-api/21.3.9, UPL-1.0, approved, #6718
maven/mavencentral/org.hamcrest/hamcrest-core/2.2, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/org.hamcrest/hamcrest/2.2, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/org.hdrhistogram/HdrHistogram/2.1.12, BSD-2-Clause OR LicenseRef-Public-Domain, approved, CQ13192
Expand All @@ -388,8 +380,6 @@ maven/mavencentral/org.jetbrains/annotations/17.0.0, Apache-2.0, approved, clear
maven/mavencentral/org.jetbrains/annotations/24.0.1, Apache-2.0, approved, #7417
maven/mavencentral/org.jodd/jodd-lagarto/6.0.6, BSD-2-Clause, approved, clearlydefined
maven/mavencentral/org.jodd/jodd-util/6.1.0, BSD-2-Clause, approved, clearlydefined
maven/mavencentral/org.jruby.jcodings/jcodings/1.0.58, MIT, approved, CQ10635
maven/mavencentral/org.jruby.joni/joni/2.2.1, MIT, approved, #8771
maven/mavencentral/org.jsoup/jsoup/1.17.2, MIT AND Apache-2.0, approved, #11785
maven/mavencentral/org.junit.jupiter/junit-jupiter-api/5.9.2, EPL-2.0, approved, #3133
maven/mavencentral/org.junit.jupiter/junit-jupiter-api/5.9.3, EPL-2.0, approved, #3133
Expand Down
12 changes: 3 additions & 9 deletions irs-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,9 @@
<version>${irs-registry-client.version}</version>
</dependency>
<dependency>
<groupId>net.jimblackler</groupId>
<artifactId>jsonschemafriend</artifactId>
<version>${jsonschemafriend.version}</version>
<exclusions>
<exclusion>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</exclusion>
</exclusions>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${json-schema-validator.version}</version>
</dependency>
<!-- Update jsoup manually to avoid a vulnerability; can be removed after jsonschemafriend updates their dependency -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
********************************************************************************/
package org.eclipse.tractusx.irs.services.validation;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.networknt.schema.InputFormat;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaException;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import lombok.extern.slf4j.Slf4j;
import net.jimblackler.jsonschemafriend.GenerationException;
import net.jimblackler.jsonschemafriend.Schema;
import net.jimblackler.jsonschemafriend.SchemaStore;
import net.jimblackler.jsonschemafriend.Validator;
import org.eclipse.tractusx.irs.data.JsonParseException;
import org.eclipse.tractusx.irs.util.JsonUtil;
import org.springframework.stereotype.Service;

/**
Expand All @@ -41,17 +42,7 @@
@Service
@Slf4j
public class JsonValidatorService {
private final SchemaStore schemaStore = new SchemaStore();
private final JsonUtil parser;

/**
* Creates a new validation service
*
* @param parser the parser to use for the JSON
*/
public JsonValidatorService(final JsonUtil parser) {
this.parser = parser;
}
private final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);

/**
* Validate the payload against the schema.
Expand All @@ -63,15 +54,11 @@ public JsonValidatorService(final JsonUtil parser) {
public ValidationResult validate(final String jsonSchema, final String jsonPayload) throws InvalidSchemaException {
log.debug("Trying to validate JSON ({}) with schema ({})", jsonPayload, jsonSchema);

final Schema schema = loadSchema(jsonSchema);
final JsonSchema schemaFromString = loadSchema(jsonSchema);

final Validator validator = new Validator();
try {
final Object payload = parser.fromString(jsonPayload, Object.class);

final List<String> errors = new ArrayList<>();
validator.validate(schema, payload, validationError -> errors.add(validationError.getMessage()));
return createValidationResult(errors);
final Set<ValidationMessage> errors = schemaFromString.validate(jsonPayload, InputFormat.JSON);
return createValidationResult(errors.stream().map(ValidationMessage::getMessage).toList());

} catch (final IllegalStateException | JsonParseException e) {
log.warn("Unable to validate JSON payload ({})", jsonPayload, e);
Expand All @@ -92,10 +79,10 @@ private ValidationResult createValidationResult(final List<String> errors) {
}
}

private Schema loadSchema(final String jsonSchema) throws InvalidSchemaException {
private JsonSchema loadSchema(final String jsonSchema) throws InvalidSchemaException {
try {
return schemaStore.loadSchemaJson(jsonSchema);
} catch (final GenerationException e) {
return factory.getSchema(jsonSchema);
} catch (final JsonSchemaException e) {
throw new InvalidSchemaException("Cannot load JSON schema for validation", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,16 @@
import java.nio.file.Path;
import java.util.Objects;

import org.eclipse.tractusx.irs.util.JsonUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class JsonValidatorServiceTest {

private final JsonValidatorService testee = new JsonValidatorService(new JsonUtil());
private final JsonValidatorService testee = new JsonValidatorService();

@Test
@Disabled("Should not pass validation, but currently payload is valid with schema v.3.0.0")
void shouldFailWhenSchemaIsInThirdVerAndPayloadInSecondVer() throws Exception {
final String schema = readFile("/json-schema/slab-v3.0.0.json");
final String payload = readFile("/__files/integrationtesting/singleLevelBomAsBuilt-2.json");
final String payload = readFile("/__files/integrationtesting/singleLevelBomAsBuilt#2.0.0.json");

final ValidationResult result = testee.validate(schema, payload);

Expand Down
47 changes: 10 additions & 37 deletions irs-api/src/test/resources/__files/assemblyPartRelationship.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
{
"quantity": {
"quantityNumber": 0.11,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#kilogram",
"lexicalValue": "kilogram"
}
"measurementUnit": "unit:kilogram"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -17,10 +14,7 @@
{
"quantity": {
"quantityNumber": 0.1204,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#kilogram",
"lexicalValue": "kilogram"
}
"measurementUnit": "unit:kilogram"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -30,10 +24,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -43,10 +34,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -56,10 +44,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -79,7 +64,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": null
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -89,10 +74,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -102,10 +84,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -115,10 +94,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -128,10 +104,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"manufacturingInformation": {
"date": "2022-02-04T14:48:54",
"date": "2022-02-04T14:48:54Z",
"country": "HUR"
},
"catenaXId": "urn:uuid:334cce52-1f52-4bc9-9dd1-410bbe497bbc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"manufacturingInformation": {
"date": "2022-02-04T14:48:54",
"date": "2022-02-04T14:48:54Z",
"country": "HUR"
},
"catenaXId": "urn:uuid:7e4541ea-bb0f-464c-8cb3-021abccbfaf5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"manufacturingInformation": {
"date": "2022-02-04T14:48:54",
"date": "2022-02-04T14:48:54Z",
"country": "HUR"
},
"catenaXId": "urn:uuid:a314ad6b-77ea-417e-ae2d-193b3e249e99",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"catenaXId": "urn:uuid:7e4541ea-bb0f-464c-8cb3-021abccbfaf5",
"childItems": [
{
"catenaXId": "urn:uuid:a314ad6b-77ea-417e-ae2d-193b3e249e99",
"quantity": {
"quantityNumber": 0.4014,
"measurementUnit": "unit:kilogram"
},
"hasAlternatives": true,
"businessPartner": "BPNL00000000TEST",
"createdOn": "2022-02-03T14:48:54.709Z",
"lastModifiedOn": "2022-02-03T14:48:54.709Z"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{
"catenaXId": "urn:uuid:a314ad6b-77ea-417e-ae2d-193b3e249e99",
"quantity": {
"quantityNumber": 0.4014,
"measurementUnit": "unit:kilogram"
"value": 0.4014,
"unit": "unit:kilogram"
},
"hasAlternatives": true,
"businessPartner": "BPNL00000000TEST",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<bc-jdk18on.version>1.78</bc-jdk18on.version>
<wiremock-standalone.version>3.5.2</wiremock-standalone.version>
<jsoup.version>1.17.2</jsoup.version>
<jsonschemafriend.version>0.12.4</jsonschemafriend.version>
<json-schema-validator.version>1.4.0</json-schema-validator.version>
<commons-io.version>2.16.1</commons-io.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<okhttp.version>4.12.0</okhttp.version>
Expand Down
Loading