com.deepoove.swagger.diff.compare.ModelDiff Maven / Gradle / Ivy
package com.deepoove.swagger.diff.compare;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.deepoove.swagger.diff.model.ElProperty;
import io.swagger.models.Model;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
public class ModelDiff {
private List increased;
private List missing;
Map oldDedinitions;
Map newDedinitions;
private ModelDiff() {
increased = new ArrayList();
missing = new ArrayList();
}
public static ModelDiff buildWithDefinition(Map left,
Map right) {
ModelDiff diff = new ModelDiff();
diff.oldDedinitions = left;
diff.newDedinitions = right;
return diff;
}
public ModelDiff diff(Model leftModel, Model rightModel) {
return this.diff(leftModel, rightModel, null);
}
public ModelDiff diff(Model leftModel, Model rightModel, String parentEl) {
if (null == leftModel && null == rightModel) return this;
Map leftProperties = null == leftModel ? null : leftModel.getProperties();
Map rightProperties = null == rightModel ? null : rightModel.getProperties();
MapKeyDiff propertyDiff = MapKeyDiff.diff(leftProperties, rightProperties);
Map increasedProp = propertyDiff.getIncreased();
Map missingProp = propertyDiff.getMissing();
increased.addAll(convert2ElPropertys(increasedProp, parentEl, false));
missing.addAll(convert2ElPropertys(missingProp, parentEl, true));
List sharedKey = propertyDiff.getSharedKey();
for (String key : sharedKey) {
Property left = leftProperties.get(key);
Property right = rightProperties.get(key);
if (left instanceof RefProperty
&& right instanceof RefProperty) {
String leftRef = ((RefProperty) left).getSimpleRef();
String rightRef = ((RefProperty) right).getSimpleRef();
diff(oldDedinitions.get(leftRef),
newDedinitions.get(rightRef),
null == parentEl ? key : (parentEl + "." + key));
}
}
return this;
}
private Collection extends ElProperty> convert2ElPropertys(
Map propMap, String parentEl, boolean isLeft) {
List result = new ArrayList();
if (null == propMap) return result;
for (Entry entry : propMap.entrySet()) {
String propName = entry.getKey();
Property property = entry.getValue();
if (property instanceof RefProperty) {
String ref = ((RefProperty) property).getSimpleRef();
Model model = isLeft ? oldDedinitions.get(ref)
: newDedinitions.get(ref);
if (model != null) {
Map properties = model.getProperties();
result.addAll(
convert2ElPropertys(properties,
null == parentEl ? propName
: (parentEl + "." + propName),
isLeft));
}
} else {
ElProperty pWithPath = new ElProperty();
pWithPath.setProperty(property);
pWithPath.setEl(null == parentEl ? propName
: (parentEl + "." + propName));
result.add(pWithPath);
}
}
return result;
}
public List getIncreased() {
return increased;
}
public void setIncreased(List increased) {
this.increased = increased;
}
public List getMissing() {
return missing;
}
public void setMissing(List missing) {
this.missing = missing;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy