support @mock tag
This commit is contained in:
parent
59007f8ac6
commit
5c00b676fa
|
@ -492,22 +492,18 @@ public class SourceBuilder {
|
|||
String fieldGicName = field.getType().getGenericCanonicalName();
|
||||
List<JavaAnnotation> javaAnnotations = field.getAnnotations();
|
||||
|
||||
List<DocletTag> paramTags = field.getTags();
|
||||
Map<String, String> tagsMap = DocUtil.getFieldTagsValue(field);
|
||||
String since = DocGlobalConstants.DEFAULT_VERSION;//since tag value
|
||||
if (!isResp) {
|
||||
pre:
|
||||
for (DocletTag docletTag : paramTags) {
|
||||
if (DocClassUtil.isIgnoreTag(docletTag.getName())) {
|
||||
continue out;
|
||||
} else if (DocTags.SINCE.equals(docletTag.getName())) {
|
||||
since = docletTag.getValue();
|
||||
}
|
||||
if (tagsMap.containsKey(DocTags.IGNORE)) {
|
||||
continue out;
|
||||
} else if (tagsMap.containsKey(DocTags.SINCE)) {
|
||||
since = tagsMap.get(DocTags.SINCE);
|
||||
}
|
||||
} else {
|
||||
for (DocletTag docletTag : paramTags) {
|
||||
if (DocTags.SINCE.equals(docletTag.getName())) {
|
||||
since = docletTag.getValue();
|
||||
}
|
||||
if (tagsMap.containsKey(DocTags.SINCE)) {
|
||||
since = tagsMap.get(DocTags.SINCE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -538,11 +534,9 @@ public class SourceBuilder {
|
|||
}
|
||||
if (annotationCounter < 1) {
|
||||
doc:
|
||||
for (DocletTag docletTag : paramTags) {
|
||||
if (DocClassUtil.isRequiredTag(docletTag.getName())) {
|
||||
strRequired = true;
|
||||
break doc;
|
||||
}
|
||||
if (tagsMap.containsKey(DocTags.REQUIRED)) {
|
||||
strRequired = true;
|
||||
break doc;
|
||||
}
|
||||
}
|
||||
//cover comment
|
||||
|
@ -825,13 +819,10 @@ public class SourceBuilder {
|
|||
DocClassUtil.isIgnoreFieldTypes(subTypeName)) {
|
||||
continue;
|
||||
}
|
||||
List<DocletTag> paramTags = field.getTags();
|
||||
Map<String, String> tagsMap = DocUtil.getFieldTagsValue(field);
|
||||
if (!isResp) {
|
||||
pre:
|
||||
for (DocletTag docletTag : paramTags) {
|
||||
if (DocClassUtil.isIgnoreTag(docletTag.getName())) {
|
||||
continue out;
|
||||
}
|
||||
if (tagsMap.containsKey(DocTags.IGNORE)) {
|
||||
continue out;
|
||||
}
|
||||
}
|
||||
List<JavaAnnotation> annotations = field.getAnnotations();
|
||||
|
@ -858,20 +849,29 @@ public class SourceBuilder {
|
|||
String fieldGicName = field.getType().getGenericCanonicalName();
|
||||
data0.append("\"").append(fieldName).append("\":");
|
||||
if (DocClassUtil.isPrimitive(subTypeName)) {
|
||||
String fieldValue = "";
|
||||
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
|
||||
fieldValue = tagsMap.get(DocTags.MOCK);
|
||||
if ("String".equals(typeSimpleName)) {
|
||||
fieldValue = DocUtil.handleJsonStr(fieldValue);
|
||||
}
|
||||
} else {
|
||||
fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
|
||||
}
|
||||
CustomRespField customResponseField = responseFieldMap.get(fieldName);
|
||||
if (null != customResponseField) {
|
||||
Object val = customResponseField.getValue();
|
||||
if (null != val) {
|
||||
if ("String".equals(typeSimpleName)) {
|
||||
data0.append("\"").append(val).append("\",");
|
||||
data0.append(DocUtil.handleJsonStr(String.valueOf(val))).append(",");
|
||||
} else {
|
||||
data0.append(val).append(",");
|
||||
}
|
||||
} else {
|
||||
data0.append(DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName())).append(",");
|
||||
data0.append(fieldValue).append(",");
|
||||
}
|
||||
} else {
|
||||
data0.append(DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName())).append(",");
|
||||
data0.append(fieldValue).append(",");
|
||||
}
|
||||
} else {
|
||||
if (DocClassUtil.isCollection(subTypeName) || DocClassUtil.isArray(subTypeName)) {
|
||||
|
@ -992,6 +992,7 @@ public class SourceBuilder {
|
|||
}
|
||||
boolean containsBrace = apiMethodDoc.getUrl().replace(DEFAULT_SERVER_URL, "").contains("{");
|
||||
Map<String, String> paramsMap = new LinkedHashMap<>();
|
||||
Map<String, String> paramsComments = DocUtil.getParamsComments(method, DocTags.PARAM, null);
|
||||
List<String> springMvcRequestAnnotations = SpringMvcRequestAnnotations.listSpringMvcRequestAnnotations();
|
||||
for (JavaParameter parameter : parameterList) {
|
||||
JavaType javaType = parameter.getType();
|
||||
|
@ -1000,85 +1001,98 @@ public class SourceBuilder {
|
|||
String typeName = javaType.getFullyQualifiedName();
|
||||
JavaClass javaClass = builder.getClassByName(typeName);
|
||||
String paraName = parameter.getName();
|
||||
if (!DocClassUtil.isMvcIgnoreParams(typeName)) {
|
||||
//file upload
|
||||
if (gicTypeName.contains(DocGlobalConstants.MULTIPART_FILE_FULLY)) {
|
||||
apiMethodDoc.setContentType(MULTIPART_TYPE);
|
||||
return DocClassUtil.isArray(typeName) ? "Use FormData upload files." : "Use FormData upload file.";
|
||||
if (DocClassUtil.isMvcIgnoreParams(typeName)) {
|
||||
continue;
|
||||
}
|
||||
String mockValue = "";
|
||||
if (DocClassUtil.isPrimitive(simpleTypeName)) {
|
||||
mockValue = paramsComments.get(paraName);
|
||||
if(mockValue.contains("|")){
|
||||
mockValue = mockValue.substring(mockValue.lastIndexOf("|") + 1, mockValue.length());
|
||||
} else {
|
||||
mockValue ="";
|
||||
}
|
||||
List<JavaAnnotation> annotations = parameter.getAnnotations();
|
||||
int requestBodyCounter = 0;
|
||||
String defaultVal = null;
|
||||
boolean notHasRequestParams = true;
|
||||
for (JavaAnnotation annotation : annotations) {
|
||||
String fullName = annotation.getType().getSimpleName();
|
||||
if (!springMvcRequestAnnotations.contains(fullName)) {
|
||||
continue;
|
||||
}
|
||||
String annotationName = annotation.getType().getSimpleName();
|
||||
if (REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) {
|
||||
requestBodyCounter++;
|
||||
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
|
||||
if (DocClassUtil.isPrimitive(simpleTypeName)) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\"")
|
||||
.append(paraName)
|
||||
.append("\":")
|
||||
.append(DocUtil.jsonValueByType(simpleTypeName))
|
||||
.append("}");
|
||||
return builder.toString();
|
||||
} else {
|
||||
return buildJson(typeName, gicTypeName, this.fieldMap, false, 0, new HashMap<>());
|
||||
}
|
||||
}
|
||||
|
||||
if (DocAnnotationConstants.SHORT_REQ_PARAM.equals(annotationName)) {
|
||||
notHasRequestParams = false;
|
||||
}
|
||||
AnnotationValue annotationDefaultVal = annotation.getProperty(DocAnnotationConstants.DEFAULT_VALUE_PROP);
|
||||
if (null != annotationDefaultVal) {
|
||||
defaultVal = StringUtil.removeQuotes(annotationDefaultVal.toString());
|
||||
}
|
||||
AnnotationValue annotationValue = annotation.getProperty(DocAnnotationConstants.VALUE_PROP);
|
||||
if (null != annotationValue) {
|
||||
paraName = StringUtil.removeQuotes(annotationValue.toString());
|
||||
}
|
||||
AnnotationValue annotationOfName = annotation.getProperty(DocAnnotationConstants.NAME_PROP);
|
||||
if (null != annotationOfName) {
|
||||
paraName = StringUtil.removeQuotes(annotationOfName.toString());
|
||||
}
|
||||
if (REQUEST_HERDER.equals(annotationName)) {
|
||||
paraName = null;
|
||||
}
|
||||
}
|
||||
if (DocClassUtil.isPrimitive(typeName) && parameterList.size() == 1
|
||||
&& isPostMethod && notHasRequestParams && !containsBrace) {
|
||||
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\"")
|
||||
.append(paraName)
|
||||
.append("\":")
|
||||
.append(DocUtil.jsonValueByType(simpleTypeName))
|
||||
.append("}");
|
||||
return builder.toString();
|
||||
}
|
||||
if (requestBodyCounter < 1 && paraName != null) {
|
||||
if (javaClass.isEnum()) {
|
||||
Object value = this.handleEnumValue(javaClass, Boolean.TRUE);
|
||||
paramsMap.put(paraName, StringUtil.removeQuotes(String.valueOf(value)));
|
||||
} else if (annotations.size() < 1 && !DocClassUtil.isPrimitive(typeName)) {
|
||||
return "Smart-doc can't support create form-data example,It is recommended to use @RequestBody to receive parameters.";
|
||||
} else if (StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitive(typeName)) {
|
||||
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
|
||||
true));
|
||||
} else if ((StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitiveArray(typeName))) {
|
||||
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
|
||||
true));
|
||||
} else {
|
||||
paramsMap.put(paraName, defaultVal);
|
||||
}
|
||||
if (StringUtil.isEmpty(mockValue)) {
|
||||
mockValue = DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName, true);
|
||||
}
|
||||
}
|
||||
|
||||
//file upload
|
||||
if (gicTypeName.contains(DocGlobalConstants.MULTIPART_FILE_FULLY)) {
|
||||
apiMethodDoc.setContentType(MULTIPART_TYPE);
|
||||
return DocClassUtil.isArray(typeName) ? "Use FormData upload files." : "Use FormData upload file.";
|
||||
}
|
||||
List<JavaAnnotation> annotations = parameter.getAnnotations();
|
||||
int requestBodyCounter = 0;
|
||||
String defaultVal = null;
|
||||
boolean notHasRequestParams = true;
|
||||
for (JavaAnnotation annotation : annotations) {
|
||||
String fullName = annotation.getType().getSimpleName();
|
||||
if (!springMvcRequestAnnotations.contains(fullName)) {
|
||||
continue;
|
||||
}
|
||||
String annotationName = annotation.getType().getSimpleName();
|
||||
if (REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) {
|
||||
requestBodyCounter++;
|
||||
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
|
||||
if (DocClassUtil.isPrimitive(simpleTypeName)) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\"")
|
||||
.append(paraName)
|
||||
.append("\":")
|
||||
.append(DocUtil.handleJsonStr(mockValue))
|
||||
.append("}");
|
||||
return builder.toString();
|
||||
} else {
|
||||
return buildJson(typeName, gicTypeName, this.fieldMap, false, 0, new HashMap<>());
|
||||
}
|
||||
}
|
||||
|
||||
if (DocAnnotationConstants.SHORT_REQ_PARAM.equals(annotationName)) {
|
||||
notHasRequestParams = false;
|
||||
}
|
||||
AnnotationValue annotationDefaultVal = annotation.getProperty(DocAnnotationConstants.DEFAULT_VALUE_PROP);
|
||||
if (null != annotationDefaultVal) {
|
||||
defaultVal = StringUtil.removeQuotes(annotationDefaultVal.toString());
|
||||
}
|
||||
AnnotationValue annotationValue = annotation.getProperty(DocAnnotationConstants.VALUE_PROP);
|
||||
if (null != annotationValue) {
|
||||
paraName = StringUtil.removeQuotes(annotationValue.toString());
|
||||
}
|
||||
AnnotationValue annotationOfName = annotation.getProperty(DocAnnotationConstants.NAME_PROP);
|
||||
if (null != annotationOfName) {
|
||||
paraName = StringUtil.removeQuotes(annotationOfName.toString());
|
||||
}
|
||||
if (REQUEST_HERDER.equals(annotationName)) {
|
||||
paraName = null;
|
||||
}
|
||||
}
|
||||
if (DocClassUtil.isPrimitive(typeName) && parameterList.size() == 1
|
||||
&& isPostMethod && notHasRequestParams && !containsBrace) {
|
||||
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("{\"")
|
||||
.append(paraName)
|
||||
.append("\":")
|
||||
.append(DocUtil.handleJsonStr(mockValue))
|
||||
.append("}");
|
||||
return builder.toString();
|
||||
}
|
||||
if (requestBodyCounter < 1 && paraName != null) {
|
||||
if (javaClass.isEnum()) {
|
||||
Object value = this.handleEnumValue(javaClass, Boolean.TRUE);
|
||||
paramsMap.put(paraName, StringUtil.removeQuotes(String.valueOf(value)));
|
||||
} else if (annotations.size() < 1 && !DocClassUtil.isPrimitive(typeName)) {
|
||||
return "Smart-doc can't support create form-data example,It is recommended to use @RequestBody to receive parameters.";
|
||||
} else if (StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitive(typeName)) {
|
||||
paramsMap.put(paraName, mockValue);
|
||||
} else if ((StringUtil.isEmpty(defaultVal) && DocClassUtil.isPrimitiveArray(typeName))) {
|
||||
paramsMap.put(paraName, mockValue);
|
||||
} else {
|
||||
paramsMap.put(paraName, defaultVal);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
String url;
|
||||
if (containsBrace && !(apiMethodDoc.getUrl().equals(DEFAULT_SERVER_URL))) {
|
||||
|
@ -1161,6 +1175,10 @@ public class SourceBuilder {
|
|||
String comment = paramTagMap.get(paramName);
|
||||
if (StringUtil.isEmpty(comment)) {
|
||||
comment = NO_COMMENTS_FOUND;
|
||||
} else {
|
||||
if(comment.contains("|")){
|
||||
comment = comment.substring(0,comment.indexOf("|"));
|
||||
}
|
||||
}
|
||||
List<JavaAnnotation> annotations = parameter.getAnnotations();
|
||||
if (annotations.size() == 0) {
|
||||
|
|
|
@ -34,4 +34,9 @@ public class DocTags {
|
|||
* custom ignore tag
|
||||
*/
|
||||
public static final String IGNORE = "ignore";
|
||||
|
||||
/**
|
||||
* custom @mock tag
|
||||
*/
|
||||
public static final String MOCK = "mock";
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ public class ApiDataDictionary {
|
|||
/**
|
||||
* enumClass
|
||||
*/
|
||||
private Class enumClass;
|
||||
private Class<? extends Enum> enumClass;
|
||||
|
||||
/**
|
||||
* enum class name
|
||||
|
|
|
@ -9,7 +9,7 @@ public class ApiErrorCodeDictionary {
|
|||
/**
|
||||
* enumClass
|
||||
*/
|
||||
private Class enumClass;
|
||||
private Class<? extends Enum> enumClass;
|
||||
|
||||
/**
|
||||
* enum class name
|
||||
|
|
|
@ -9,10 +9,12 @@ import com.power.doc.constants.DocAnnotationConstants;
|
|||
import com.power.doc.constants.DocGlobalConstants;
|
||||
import com.thoughtworks.qdox.model.DocletTag;
|
||||
import com.thoughtworks.qdox.model.JavaAnnotation;
|
||||
import com.thoughtworks.qdox.model.JavaField;
|
||||
import com.thoughtworks.qdox.model.JavaMethod;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
|
@ -144,9 +146,7 @@ public class DocUtil {
|
|||
return jsonValueByType(typeName);
|
||||
} else {
|
||||
if ("string".equals(type.toLowerCase())) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("\"").append(value).append("\"");
|
||||
return builder.toString();
|
||||
return handleJsonStr(value.toString());
|
||||
} else {
|
||||
return value.toString();
|
||||
}
|
||||
|
@ -291,7 +291,7 @@ public class DocUtil {
|
|||
Map<String, String> paramTagMap = new HashMap<>();
|
||||
for (DocletTag docletTag : paramTags) {
|
||||
String value = docletTag.getValue();
|
||||
if (StringUtil.isEmpty(value)) {
|
||||
if (StringUtil.isEmpty(value)&&StringUtil.isNotEmpty(className)) {
|
||||
throw new RuntimeException("ERROR: #" + javaMethod.getName()
|
||||
+ "() - bad @" + tagName + " javadoc from " + className + ", must be add comment if you use it.");
|
||||
}
|
||||
|
@ -324,6 +324,16 @@ public class DocUtil {
|
|||
return getFirstKeyAndValue(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field tags
|
||||
* @param field JavaField
|
||||
* @return map
|
||||
*/
|
||||
public static Map<String, String> getFieldTagsValue(final JavaField field) {
|
||||
List<DocletTag> paramTags = field.getTags();
|
||||
return paramTags.stream().collect(Collectors.toMap(DocletTag::getName, DocletTag::getValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first element of a map.
|
||||
*
|
||||
|
@ -371,4 +381,10 @@ public class DocUtil {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String handleJsonStr(String content){
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("\"").append(content).append("\"");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue