Skip to content

Commit

Permalink
IRS-739_add_value_for_submodel_url_for_IRS_version_4.5.2
Browse files Browse the repository at this point in the history
feat(impl):[eclipse-tractusx#522] change json schema lib
  • Loading branch information
poojapatel23 committed Jul 5, 2024
1 parent 40d7d25 commit 6ab3780
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed submodel request path by introducing configuration property `irs-edc-client.submodel.submodel-suffix` which will
be appended to the href URL.
- Fixed validation of json-schemas - IRS is creating tombstone instead collecting Submodel payload, when it not passes validation of schema #522

## [4.5.2] - 2024-02-22
## Changed
Expand Down
12 changes: 3 additions & 9 deletions irs-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,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 @@ -61,17 +52,13 @@ public JsonValidatorService(final JsonUtil parser) {
* @return the validation result, containing the validation errors if applicable
*/
public ValidationResult validate(final String jsonSchema, final String jsonPayload) throws InvalidSchemaException {
log.debug("Trying to validate JSON ({}) with schema ({})", jsonPayload, jsonSchema);
log.trace("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,12 +33,11 @@
import java.nio.file.Path;
import java.util.Objects;

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

class JsonValidatorServiceTest {

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

@Test
void shouldValidateAssemblyPartRelationship() throws Exception {
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<bc-jdk18on.version>1.76</bc-jdk18on.version>
<wiremock-standalone.version>3.3.1</wiremock-standalone.version>
<jsoup.version>1.16.1</jsoup.version>
<jsonschemafriend.version>0.12.0</jsonschemafriend.version>
<json-schema-validator.version>1.4.0</json-schema-validator.version>
<commons-io.version>2.14.0</commons-io.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<okhttp.version>4.10.0</okhttp.version>
Expand Down

0 comments on commit 6ab3780

Please sign in to comment.