Skip to content

Make ParameterDiff detect changes to fx the Type of a PathParameter #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 31 additions & 3 deletions src/main/java/com/deepoove/swagger/diff/compare/ParameterDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
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 @@ -54,22 +61,32 @@ 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();

ModelDiff diff = ModelDiff.buildWithDefinition(oldDedinitions, newDedinitions)
.diff(leftSchema, rightSchema, leftPara.getName());
changedParameter.setIncreased(diff.getIncreased());
changedParameter.setMissing(diff.getMissing());
changedParameter.setChanged(diff.getChanged());

}

//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
Expand All @@ -93,6 +110,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 @@ -712,9 +712,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