Skip to content

Commit

Permalink
fix: fix mock escape(query, body, curl, example)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqi146 committed Jan 1, 2024
1 parent 733f6c8 commit d234642
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>com.github.shalousun</groupId>
<artifactId>common-util</artifactId>
<version>2.2.1</version>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/ly/doc/helper/BaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ly.doc.constants.DocTags;
import com.ly.doc.utils.DocUtil;
import com.ly.doc.utils.JavaClassValidateUtil;
import com.power.common.util.StringEscapeUtil;
import com.power.common.util.StringUtil;
import com.ly.doc.utils.ParamUtil;

Expand All @@ -37,13 +38,15 @@ protected static String getFieldValueFromMock(String subTypeName, Map<String, St
String fieldValue = "";
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
fieldValue = tagsMap.get(DocTags.MOCK);
fieldValue = StringEscapeUtil.unescapeJava(fieldValue);
if (!DocUtil.javaPrimaryType(typeSimpleName)
&& !JavaClassValidateUtil.isCollection(subTypeName)
&& !JavaClassValidateUtil.isMap(subTypeName)
&& !JavaClassValidateUtil.isArray(subTypeName)) {
fieldValue = StringEscapeUtil.escapeJava(fieldValue, true);
fieldValue = DocUtil.handleJsonStr(fieldValue);
}
}
return ParamUtil.formatMockValue(fieldValue);
return fieldValue;
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.ly.doc.utils.JavaClassValidateUtil;
import com.power.common.model.EnumDictionary;
import com.power.common.util.CollectionUtil;
import com.power.common.util.StringEscapeUtil;
import com.power.common.util.StringUtil;
import com.ly.doc.builder.ProjectDocConfigBuilder;
import com.ly.doc.constants.DocTags;
Expand All @@ -66,6 +67,14 @@
*/
public class ParamsBuildHelper extends BaseHelper {

protected static String getFieldValueFromMock(Map<String, String> tagsMap) {
String fieldValue = "";
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
fieldValue = StringEscapeUtil.unescapeJava(tagsMap.get(DocTags.MOCK));
}
return fieldValue;
}

public static List<ApiParam> buildParams(String className, String pre, int level, String isRequired, boolean isResp
, Map<String, String> registryClasses, ProjectDocConfigBuilder projectBuilder, Set<String> groupClasses
, int pid, boolean jsonRequest, AtomicInteger atomicInteger) {
Expand Down Expand Up @@ -248,7 +257,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
}
}
comment.append(JavaFieldUtil.getJsrComment(javaAnnotations));
String fieldValue = getFieldValueFromMock(subTypeName, tagsMap, typeSimpleName);
// fixme post form curl example error
String fieldValue = getFieldValueFromMock(tagsMap);


// cover response value
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,6 @@ default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc ap
path = DocUtil.formatAndRemove(path, pathParamsMap);
body = UrlUtil.urlJoin(DocGlobalConstants.EMPTY, queryParamsMap)
.replace("?", DocGlobalConstants.EMPTY);
body = StringUtil.removeQuotes(body);
url = apiMethodDoc.getServerUrl() + "/" + path;
url = UrlUtil.simplifyUrl(url);

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/ly/doc/utils/ApiParamTreeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.ly.doc.model.ApiParam;
import com.ly.doc.model.ApiReqParam;
import com.power.common.util.CollectionUtil;
import com.power.common.util.StringUtil;

/**
* @author yu 2020/8/8.
Expand Down Expand Up @@ -102,7 +101,6 @@ public static ApiMethodReqParam buildMethodReqParam(List<ApiParam> paramList, fi
List<ApiParam> queryParams = new ArrayList<>();
List<ApiParam> bodyParams = new ArrayList<>();
for (ApiParam param : paramList) {
param.setValue(StringUtil.removeDoubleQuotes(param.getValue()));
if (param.isPathParam()) {
if (pathReqParamMap.containsKey(param.getField())) {
param.setConfigParam(true).setValue(pathReqParamMap.get(param.getField()).getValue());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ly/doc/utils/CurlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public static String toCurl(CurlRequest request) {

sb.append(" -i");
// append request url
sb.append(" ").append(request.getUrl());
if (StringUtil.isNotEmpty(request.getBody()) &&
!DocGlobalConstants.URL_CONTENT_TYPE.equals(request.getContentType())) {
sb.append(" ").append("'").append(request.getUrl()).append("'");
if (StringUtil.isNotEmpty(request.getBody())) {
sb.append(" --data");
sb.append(" '").append(request.getBody()).append("'");
String data = request.getBody().replaceAll("'", "'\\\\''");
sb.append(" '").append(data).append("'");
}
return sb.toString();
}
Expand Down

0 comments on commit d234642

Please sign in to comment.