Commit d3a2a3e0 authored by Abhishek Kumar's avatar Abhishek Kumar
Browse files

Patch Schema

parent d2707bc6
Pipeline #54189 failed with stages
in 1 minute and 58 seconds
......@@ -24,7 +24,6 @@ import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
......@@ -34,7 +33,6 @@ public class SchemaMinorVersionValidator implements SchemaVersionValidator{
private final JaxRsDpsLog log;
private final JSONUtil jsonUtil;
private static String REGEX="^([^\\s]+):([^\\s]+):([^\\s]+):\\d+.\\d+.\\d+$";
@Override
public void validate(String oldSchema, String newSchema) throws SchemaVersionException, ApplicationException{
......@@ -90,7 +88,7 @@ public class SchemaMinorVersionValidator implements SchemaVersionValidator{
}
//Removing anything is not permitted
if(path.contains("/allOf") || path.contains("/oneOf") || path.contains("/anyOf")) {
else if(path.contains("/allOf") || path.contains("/oneOf") || path.contains("/anyOf")) {
String [] attributes = path.split("/");
Optional<String> xxxOfAttrOpt = Arrays.stream(attributes)
......@@ -168,20 +166,6 @@ public class SchemaMinorVersionValidator implements SchemaVersionValidator{
changedRefIds.put(oldRefId, newRefId);
/*
* try {
*
* JsonNode oldVal = oldDef.get(oldRefId); JsonNode newVal =
* newDef.get(newRefId);
*
* //compare the actual schemas referenced under definition block
* //compare(oldVal, newVal, oldDef, newDef, changedRefIds);
*
* //Remove the element if validation has passed //removeElement(oldDef,
* oldRefId); //removeElement(newDef, newRefId);
*
* } catch (ApplicationException e) { e.printStackTrace(); }
*/
}
public static boolean isValidVersionChange(String oldRefValue, String newRefValue) {
......@@ -212,11 +196,8 @@ public class SchemaMinorVersionValidator implements SchemaVersionValidator{
}
private void validateCompositionAttributesForMinorVersion(JsonNode oldSchema, JsonNode newSchema, JsonNode sourceDefinitions, JsonNode targetDefinitions, String path, Map<String, String> changedRefIds) throws SchemaVersionException, ApplicationException, JsonMappingException, JsonProcessingException {
//JsonArray oldJsonArray = jsonUtil.getJsonArrayFromGivenPath(oldSchema, path);
//JsonArray newJsonArray = jsonUtil.getJsonArrayFromGivenPath(newSchema, path);
JsonNode oldJsonArray = oldSchema.at(path);
JsonNode newJsonArray = newSchema.at(path);
try {
if(oldJsonArray.size() > newJsonArray.size()) {
......
......@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import javax.json.Json;
import javax.json.JsonObject;
......@@ -94,6 +95,41 @@ public class SchemaPatchVersionValidator implements SchemaVersionValidator{
|| attributeName.startsWith(SchemaConstants.TRANSFORMATION_TAG))
continue;
//Changing state of "additionalProperties" is only permitted when it was not present and added as true
//or it is present as true but removed now
if("additionalProperties".equals(attributeName)) {
if("add".equals(operation) && "true".equals(diffElement.get("value").asText())) {
continue;
}else if("remove".equals(operation)) {
JsonPointer pointer = JsonPointer.valueOf(path);
JsonNode originalValue = cleanSource.at(pointer);
if("true".equals(originalValue.asText()))
continue;
}
log.error("Changing state of \"additionalProperties\" is not permitted. Path :"+path);
throw new SchemaVersionException(SchemaConstants.BREAKING_CHANGES_MINOR);
}
else if("$ref".equals(attributeName)) {
if("replace".equals(operation)) {
checkRefChangesAreAllowed(cleanSource, sourceDefinitions, cleanTarget, targetDefinitions, diffElement, changedRefIds);
continue;
}
log.error("Changing state of \"additionalProperties\" is not permitted. Path :"+path);
throw new SchemaVersionException(SchemaConstants.BREAKING_CHANGES_MINOR);
}
//Remove operation is permitted for some cases
else if(operation.equals("remove")) {
if(changedRefIds.containsKey(attributeName))
continue;
log.error("Removing elements from a schema at Minor level not permitted. Path :"+path);
throw new SchemaVersionException(SchemaConstants.BREAKING_CHANGES_MINOR);
}
log.error("Failed patch level validation for the attribute: "+attributeName);
throw new SchemaVersionException(SchemaConstants.BREAKING_CHANGES_PATCH);
......@@ -102,4 +138,42 @@ public class SchemaPatchVersionValidator implements SchemaVersionValidator{
}
public void checkRefChangesAreAllowed(JsonNode cleanSource, JsonNode oldDef, JsonNode cleanTarget, JsonNode newDef, JsonNode patchValue, Map<String, String> changedRefIds) throws SchemaVersionException {
String path = patchValue.get("path").asText();
JsonPointer pointer = JsonPointer.valueOf(path);
JsonNode oldRefValue = cleanSource.at(pointer);
JsonNode newRefValue = cleanTarget.at(pointer);
String oldRefId = StringUtils.substringAfterLast(oldRefValue.asText(), "/");
String newRefId = StringUtils.substringAfterLast(newRefValue.asText(), "/");
if(isValidVersionChange(oldRefId, newRefId) == false)
throw new SchemaVersionException("");
changedRefIds.put(oldRefId, newRefId);
}
public static boolean isValidVersionChange(String oldRefValue, String newRefValue) {
Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
boolean isValidOldSchemaId = pattern.matcher(oldRefValue).find();
boolean isValidNewSchemaId = pattern.matcher(newRefValue).find();
if((false == isValidOldSchemaId || false == isValidNewSchemaId) && oldRefValue.equals(newRefValue) == false)
return false;
String [] oldKind = oldRefValue.split("\\.");
String [] newKind = newRefValue.split("\\.");
if(oldKind[0].equals(newKind[0]) == false)
return false;
else if(Long.valueOf(oldKind[1]) != Long.valueOf(newKind[1])
|| Long.valueOf(oldKind[2]) > Long.valueOf(newKind[2]))
return false;
return true;
}
}
......@@ -5,6 +5,8 @@ import org.opengroup.osdu.schema.exceptions.SchemaVersionException;
public interface SchemaVersionValidator {
public static String REGEX="^([^\\s]+):([^\\s]+):([^\\s]+):\\d+.\\d+.\\d+$";
public SchemaVersionValidatorType getType();
public void validate(String oldSchema, String newSchema) throws SchemaVersionException, ApplicationException;
......
{
"oneOf": [
{
"$ref": "#/definitions/osdu:wks:AbstractCommonResources:2.1.1"
},
{
"title": "title-1",
"type": "object",
"properties": {
"State": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": [
"ZipCode"
]
},
{
"title": "title-2",
"type": "object",
"properties": {
"PostCode": {
"type": "string"
},
"County": {
"type": "string"
}
},
"required": [
"PostCode"
]
}
],
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "My schema",
"definitions": {
"osdu:wks:AbstractCommonResources:2.1.1": {
"x-osdu-inheriting-from-kind": [],
"x-osdu-license": "Copyright 2021, The Open Group \\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"$schema": "http://json-schema.org/draft-07/schema#",
"x-osdu-schema-source": "osdu:wks:AbstractCommonResources:1.0.0",
"description": "Common resources to be injected at root 'data' level for every entity, which is persistable in Storage. The insertion is performed by the OsduSchemaComposer script.",
"title": "OSDU Common Resources",
"type": "object",
"properties": {
"ResourceHomeRegionID": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "The name of the home [cloud environment] region for this OSDU resource object.",
"title": "Resource Home Region ID",
"type": "string"
},
"ResourceHostRegionIDs": {
"description": "The name of the host [cloud environment] region(s) for this OSDU resource object.",
"title": "Resource Host Region ID",
"type": "array",
"items": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"type": "string"
}
},
"ResourceLifecycleStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceLifecycleStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceLifecycleStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Resource Lifecycle status.",
"title": "Resource Lifecycle Status",
"type": "string"
},
"ResourceSecurityClassification": {
"x-osdu-relationship": [
{
"EntityType": "ResourceSecurityClassification",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceSecurityClassification:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Classifies the security level of the resource.",
"title": "Resource Security Classification",
"type": "string"
},
"ResourceCurationStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceCurationStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceCurationStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Curation status.",
"title": "Resource Curation Status",
"type": "string"
},
"ExistenceKind": {
"x-osdu-relationship": [
{
"EntityType": "ExistenceKind",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ExistenceKind:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Where does this data resource sit in the cradle-to-grave span of its existence?",
"title": "Existence Kind",
"type": "string"
},
"Source": {
"description": "The entity that produced the record, or from which it is received; could be an organization, agency, system, internal team, or individual. For informational purposes only, the list of sources is not governed.",
"title": "Data Source",
"type": "string"
}
},
"$id": "https://schema.osdu.opengroup.org/json/abstract/AbstractCommonResources.2.1.1.json"
}
},
"properties": {
"AddressLine2": {
"type": "string"
},
"AddressLine1": {
"type": "string"
},
"City": {
"type": "string"
}
},
"required": [
"AddressLine1",
"AddressLine2"
]
}
\ No newline at end of file
{
"oneOf": [
{
"$ref": "#/definitions/osdu:wks:AbstractCommonResources:1.1.1"
},
{
"title": "title-1",
"type": "object",
"properties": {
"State": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": [
"ZipCode"
]
},
{
"title": "title-2",
"type": "object",
"properties": {
"PostCode": {
"type": "string"
},
"County": {
"type": "string"
}
},
"required": [
"PostCode"
]
}
],
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "My schema",
"definitions": {
"osdu:wks:AbstractCommonResources:1.1.1": {
"x-osdu-inheriting-from-kind": [],
"x-osdu-license": "Copyright 2021, The Open Group \\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"$schema": "http://json-schema.org/draft-07/schema#",
"x-osdu-schema-source": "osdu:wks:AbstractCommonResources:1.0.0",
"description": "Common resources to be injected at root 'data' level for every entity, which is persistable in Storage. The insertion is performed by the OsduSchemaComposer script.",
"title": "OSDU Common Resources",
"type": "object",
"properties": {
"ResourceHomeRegionID": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "The name of the home [cloud environment] region for this OSDU resource object.",
"title": "Resource Home Region ID",
"type": "string"
},
"ResourceHostRegionIDs": {
"description": "The name of the host [cloud environment] region(s) for this OSDU resource object.",
"title": "Resource Host Region ID",
"type": "array",
"items": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"type": "string"
}
},
"ResourceLifecycleStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceLifecycleStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceLifecycleStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Resource Lifecycle status.",
"title": "Resource Lifecycle Status",
"type": "string"
},
"ResourceSecurityClassification": {
"x-osdu-relationship": [
{
"EntityType": "ResourceSecurityClassification",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceSecurityClassification:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Classifies the security level of the resource.",
"title": "Resource Security Classification",
"type": "string"
},
"ResourceCurationStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceCurationStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceCurationStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Curation status.",
"title": "Resource Curation Status",
"type": "string"
},
"ExistenceKind": {
"x-osdu-relationship": [
{
"EntityType": "ExistenceKind",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ExistenceKind:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Where does this data resource sit in the cradle-to-grave span of its existence?",
"title": "Existence Kind",
"type": "string"
},
"Source": {
"description": "The entity that produced the record, or from which it is received; could be an organization, agency, system, internal team, or individual. For informational purposes only, the list of sources is not governed.",
"title": "Data Source",
"type": "string"
}
},
"$id": "https://schema.osdu.opengroup.org/json/abstract/AbstractCommonResources.2.1.1.json"
}
},
"properties": {
"AddressLine2": {
"type": "string"
},
"AddressLine1": {
"type": "string"
},
"City": {
"type": "string"
}
},
"required": [
"AddressLine1",
"AddressLine2"
]
}
\ No newline at end of file
{
"oneOf": [
{
"$ref": "#/definitions/osdu:wks:AbstractCommonResources:2.0.1"
},
{
"title": "title-1",
"type": "object",
"properties": {
"State": {
"type": "string"
},
"ZipCode": {
"type": "string"
}
},
"required": [
"ZipCode"
]
},
{
"title": "title-2",
"type": "object",
"properties": {
"PostCode": {
"type": "string"
},
"County": {
"type": "string"
}
},
"required": [
"PostCode"
]
}
],
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "My schema",
"definitions": {
"osdu:wks:AbstractCommonResources:2.0.1": {
"x-osdu-inheriting-from-kind": [],
"x-osdu-license": "Copyright 2021, The Open Group \\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"$schema": "http://json-schema.org/draft-07/schema#",
"x-osdu-schema-source": "osdu:wks:AbstractCommonResources:1.0.0",
"description": "Common resources to be injected at root 'data' level for every entity, which is persistable in Storage. The insertion is performed by the OsduSchemaComposer script.",
"title": "OSDU Common Resources",
"type": "object",
"properties": {
"ResourceHomeRegionID": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "The name of the home [cloud environment] region for this OSDU resource object.",
"title": "Resource Home Region ID",
"type": "string"
},
"ResourceHostRegionIDs": {
"description": "The name of the host [cloud environment] region(s) for this OSDU resource object.",
"title": "Resource Host Region ID",
"type": "array",
"items": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"type": "string"
}
},
"ResourceLifecycleStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceLifecycleStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceLifecycleStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Resource Lifecycle status.",
"title": "Resource Lifecycle Status",
"type": "string"