Skip to content

Commit

Permalink
Merge 9422bb7 into bda37e6
Browse files Browse the repository at this point in the history
  • Loading branch information
Hervian committed Mar 13, 2019
2 parents bda37e6 + 9422bb7 commit dbd0a2d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/deepoove/swagger/diff/compare/ListDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.BiFunction;

import com.google.common.collect.Lists;

/**
* compare two Lists
*
*
* @author Sayi
* @version
*/
Expand All @@ -27,13 +30,14 @@ public static <K> ListDiff<K> diff(List<K> left, List<K> right) {
}

/**
*
*
* @param left
* @param right
* @param biFunc
* if right List contains left element
* @return
*/
@SuppressWarnings("unchecked")
public static <K> ListDiff<K> diff(List<K> left, List<K> right,
BiFunction<List<K>, K, K> biFunc) {
ListDiff<K> instance = new ListDiff<>();
Expand All @@ -52,7 +56,14 @@ public static <K> ListDiff<K> diff(List<K> left, List<K> right,
for (K ele : left) {
K rightEle = biFunc.apply(right, ele);
if (null != rightEle) {
instance.increased.remove(ele);
ListIterator<K> listIterator = instance.increased.listIterator();
while(listIterator.hasNext()){
K k = listIterator.next();
if (biFunc.apply(Lists.newArrayList(k), ele)!=null) {
listIterator.remove();
break;
}
}
instance.shared.put(ele, rightEle);
} else {
instance.missing.add(ele);
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/com/deepoove/swagger/diff/compare/ParameterDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import org.apache.commons.lang3.StringUtils;

import com.deepoove.swagger.diff.model.ChangedParameter;
import com.deepoove.swagger.diff.model.ElProperty;
import com.google.common.collect.Lists;

import io.swagger.models.Model;
import io.swagger.models.RefModel;
import io.swagger.models.parameters.AbstractSerializableParameter;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;

/**
* compare two parameter
*
*
* @author Sayi
* @version
*/
Expand Down Expand Up @@ -55,23 +60,20 @@ public ParameterDiff diff(List<Parameter> left, List<Parameter> right) {
this.increased.addAll(paramDiff.getIncreased());
this.missing.addAll(paramDiff.getMissing());
Map<Parameter, Parameter> shared = paramDiff.getShared();

shared.forEach((leftPara, rightPara) -> {
ChangedParameter changedParameter = new ChangedParameter();
changedParameter.setLeftParameter(leftPara);
changedParameter.setRightParameter(rightPara);

if (leftPara instanceof BodyParameter && rightPara instanceof BodyParameter) {
BodyParameter leftBodyPara = (BodyParameter) leftPara;
Model leftSchema = leftBodyPara.getSchema();
BodyParameter rightBodyPara = (BodyParameter) rightPara;
Model rightSchema = rightBodyPara.getSchema();
Model rightSchema = rightBodyPara.getSchema();
if (leftSchema instanceof RefModel && rightSchema instanceof RefModel) {
String leftRef = ((RefModel) leftSchema).getSimpleRef();
String rightRef = ((RefModel) rightSchema).getSimpleRef();
Model leftModel = oldDedinitions.get(leftRef);
Model rightModel = newDedinitions.get(rightRef);

ModelDiff diff = ModelDiff.buildWithDefinition(oldDedinitions, newDedinitions)
.diff(leftModel, rightModel, leftPara.getName());
changedParameter.setIncreased(diff.getIncreased());
Expand All @@ -80,6 +82,16 @@ public ParameterDiff diff(List<Parameter> left, List<Parameter> right) {
}
}

//Let's handle the case where the new API has fx changed the type of PathParameter from being of type String to type integer
if (leftPara instanceof AbstractSerializableParameter && rightPara instanceof AbstractSerializableParameter) {
if (!leftPara.equals(rightPara)) {
ElProperty elProperty = new ElProperty();
elProperty.setEl(rightPara.getName());
elProperty.setProperty(mapToProperty(rightPara));
changedParameter.setChanged(Lists.newArrayList(elProperty));
}
}

// is requried
boolean rightRequired = rightPara.getRequired();
boolean leftRequired = leftPara.getRequired();
Expand All @@ -101,6 +113,17 @@ public ParameterDiff diff(List<Parameter> left, List<Parameter> right) {
return this;
}

private Property mapToProperty(Parameter rightPara) {
Property prop = new StringProperty();
prop.setAccess(rightPara.getAccess());
prop.setAllowEmptyValue(rightPara.getAllowEmptyValue());
prop.setDescription(rightPara.getDescription());
prop.setName(rightPara.getName());
prop.setReadOnly(rightPara.isReadOnly());
prop.setRequired(rightPara.getRequired());
return prop;
}

public List<Parameter> getIncreased() {
return increased;
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/resources/petstore_v2_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,10 @@
{
"name": "username",
"in": "path",
"description": "The name that needs to be fetched. Use user1 for testing. ",
"description": "The name that needs to be fetched. Use user1 for testing. edited",
"required": true,
"type": "string"
"type": "integer",
"format": "int32"
}
],
"responses": {
Expand Down

0 comments on commit dbd0a2d

Please sign in to comment.