Commit 5ac3cb70 authored by Abhishek Kumar (SLB)'s avatar Abhishek Kumar (SLB)
Browse files

Fixing nested $ref validation

parent 8a3877d3
Pipeline #117058 failed with stages
in 26 minutes and 41 seconds
......@@ -3,15 +3,15 @@ package org.opengroup.osdu.schema.validation.version.handler;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.util.JSONUtil;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationChainOfHandlers;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationHandler;
import org.opengroup.osdu.schema.validation.version.model.SchemaBreakingChanges;
import org.opengroup.osdu.schema.validation.version.model.SchemaHandlerVO;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatch;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatchRefComp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -38,7 +38,10 @@ public class SchemaValidationManager {
log.info("Finding difference between two schemas");
schemaPatchList = jsonUtil.findJSONDiff(schemaDiff.getSourceSchema(), schemaDiff.getTargetSchema());
//Sort the patch such that $ref are processed first
if(schemaPatchList != null && schemaPatchList.size()>0) {
schemaPatchList = schemaPatchList.stream().sorted(new SchemaPatchRefComp()).collect(Collectors.toList());
}
log.info("Total differences found :"+schemaPatchList.size());
Set<String> processedArrayPath = new HashSet<>();
......
package org.opengroup.osdu.schema.validation.version.handler.common;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.schema.constants.SchemaConstants;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.util.SchemaUtil;
import org.opengroup.osdu.schema.validation.version.SchemaValidationType;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationHandler;
import org.opengroup.osdu.schema.validation.version.model.SchemaBreakingChanges;
import org.opengroup.osdu.schema.validation.version.model.SchemaHandlerVO;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
......@@ -18,6 +23,8 @@ import org.springframework.stereotype.Component;
public class RemoveOperationHandler implements SchemaValidationHandler{
private SchemaValidationHandler nextHandler;
@Autowired
private SchemaUtil schemaUtil;
@Override
public void setNextHandler(SchemaValidationHandler nextHandler) {
......@@ -26,24 +33,50 @@ public class RemoveOperationHandler implements SchemaValidationHandler{
@Override
public void compare(SchemaHandlerVO schemaDiff, SchemaPatch patch, List<SchemaBreakingChanges> schemaBreakingChanges, Set<String> processedArrayPath) throws ApplicationException {
String attributeName = StringUtils.substringAfterLast(patch.getPath(), "/");
if(SchemaConstants.OP_REMOVE.equals(patch.getOp())) {
if(schemaDiff.getChangedRefIds().containsKey(attributeName))
if(schemaDiff.getChangedRefIds().containsKey(attributeName)) {
return;
}else if(isPresentInSource(schemaDiff, patch)) {
return;
}
schemaBreakingChanges.add(new SchemaBreakingChanges(patch, "Removing elements from a schema at Minor level not permitted."));
}else if(null != nextHandler){
this.nextHandler.compare(schemaDiff, patch, schemaBreakingChanges, processedArrayPath);
}
}
@Override
public SchemaValidationType getValidationType() {
return SchemaValidationType.COMMON;
}
private boolean isPresentInSource(SchemaHandlerVO schemaDiff, SchemaPatch patch) {
Pattern pattern = Pattern.compile(SchemaConstants.SCHEMA_KIND_REGEX);
String path = patch.getPath();
String sourceField = StringUtils.substringAfterLast(path, "/");
if(!pattern.matcher(path).matches() || !isAtRoot(path))
return false;
Iterator<String> fieldNameItr = schemaDiff.getTargetDefinition().fieldNames();
while(fieldNameItr.hasNext()) {
String fieldName = fieldNameItr.next();
if(pattern.matcher(fieldName).matches()) {
if(schemaUtil.isValidSchemaVersionChange(sourceField, fieldName,schemaDiff.getValidationType())){
return true;
}
}
}
return false;
}
private boolean isAtRoot(String path) {
String subString [] = path.split("\\/");
return subString.length == 2;
}
}
\ No newline at end of file
package org.opengroup.osdu.schema.validation.version.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
......
package org.opengroup.osdu.schema.validation.version.model;
import java.util.Comparator;
import org.apache.commons.lang3.StringUtils;
public class SchemaPatchRefComp implements Comparator<SchemaPatch> {
@Override
public int compare(SchemaPatch patch1, SchemaPatch patch2) {
String attributeName1 = StringUtils.substringAfterLast(patch1.getPath(), "/");
String attributeName2 = StringUtils.substringAfterLast(patch2.getPath(), "/");
if("$ref".equals(attributeName1)) {
return 1;
}else if("$ref".equals(attributeName2)) {
return -1;
}
return 0;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment