Skip to content
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

refactor: ♻️ rename param type constants and format code #738

Merged
merged 1 commit into from
Feb 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean is
if (isRep) {
String responseRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams());
if (apiMethodDoc.getIsResponseArray() == 1) {
schema.put("type", ARRAY);
schema.put("type", PARAM_TYPE_ARRAY);
innerScheme.put("$ref", responseRef);
schema.put("items", innerScheme);
} else if (CollectionUtil.isNotEmpty(apiMethodDoc.getResponseParams())) {
Expand All @@ -228,7 +228,7 @@ public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean is
//remove special characters in url
if (CollectionUtil.isNotEmpty(apiMethodDoc.getRequestParams())) {
if (apiMethodDoc.getIsRequestArray() == 1) {
schema.put("type", ARRAY);
schema.put("type", PARAM_TYPE_ARRAY);
innerScheme.put("$ref", requestRef);
schema.put("items", innerScheme);
} else {
Expand Down Expand Up @@ -301,12 +301,12 @@ public Map<String, Object> buildParametersSchema(ApiParam apiParam) {
if ("enum".equals(apiParam.getType())) {
schema.put("enum", apiParam.getEnumValues());
}
} else if (ARRAY.equals(openApiType)) {
} else if (PARAM_TYPE_ARRAY.equals(openApiType)) {
if (CollectionUtil.isNotEmpty(apiParam.getEnumValues())) {
schema.put("type", "string");
schema.put("items", apiParam.getEnumValues());
} else {
schema.put("type", ARRAY);
schema.put("type", PARAM_TYPE_ARRAY);
Map<String, String> map = new HashMap<>(4);
map.put("type", "string");
map.put("format", "string");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ly/doc/builder/openapi/OpenApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
if (apiParam.isHasItems()) {
parameters = getStringParams(apiParam, false);
Map<String, Object> arrayMap = new HashMap<>();
arrayMap.put("type", DocGlobalConstants.ARRAY);
arrayMap.put("type", DocGlobalConstants.PARAM_TYPE_ARRAY);
arrayMap.put("items", getStringParams(apiParam, apiParam.isHasItems()));
parameters.put("schema", arrayMap);
parametersList.add(parameters);
Expand Down Expand Up @@ -242,8 +242,8 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
parameters.put("in", "query");
parameters.put("schema", buildParametersSchema(apiParam));
} else {
if (DocGlobalConstants.OBJECT.equals(apiParam.getType()) ||
(DocGlobalConstants.ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
if (DocGlobalConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) ||
(DocGlobalConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
parameters.put("type", "object");
parameters.put("description", "(complex POJO please use @RequestBody)");
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/ly/doc/builder/openapi/SwaggerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Map<String, Object> buildPathUrlsRequest(ApiConfig apiConfig, ApiMethodDo
request.put("tags", Arrays.asList(apiDoc.getName(),apiDoc.getDesc(), apiMethodDoc.getGroup())
.stream().filter(StringUtil::isNotEmpty).toArray(n->new String[n]));
List<Map<String, Object>> parameters = buildParameters(apiMethodDoc);
//requestBody
// requestBody
if (CollectionUtil.isNotEmpty(apiMethodDoc.getRequestParams())) {
Map<String, Object> parameter = new HashMap<>();
parameter.put("in", "body");
Expand Down Expand Up @@ -207,9 +207,9 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
parametersList.add(parameters);
}
for (ApiParam apiParam : apiMethodDoc.getQueryParams()) {
if (apiParam.getType().equals(DocGlobalConstants.ARRAY) || apiParam.isHasItems()) {
if (apiParam.getType().equals(DocGlobalConstants.PARAM_TYPE_ARRAY) || apiParam.isHasItems()) {
parameters = getStringParams(apiParam, false);
parameters.put("type", DocGlobalConstants.ARRAY);
parameters.put("type", DocGlobalConstants.PARAM_TYPE_ARRAY);
parameters.put("items", getStringParams(apiParam, true));
parametersList.add(parameters);
} else {
Expand All @@ -218,7 +218,7 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
parametersList.add(parameters);
}
}
//with headers
// with headers
if (!CollectionUtil.isEmpty(apiMethodDoc.getRequestHeaders())) {
for (ApiReqParam header : apiMethodDoc.getRequestHeaders()) {
parameters = new HashMap<>(20);
Expand Down Expand Up @@ -251,7 +251,7 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
parameters.put("required", apiParam.isRequired());
parameters.put("type", apiParam.getType());
} else {
if (DocGlobalConstants.OBJECT.equals(apiParam.getType()) || (DocGlobalConstants.ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
if (DocGlobalConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) || (DocGlobalConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
parameters.put("type", "object(complex POJO please use @RequestBody)");
} else {
String desc = apiParam.getDesc();
Expand Down Expand Up @@ -279,12 +279,12 @@ public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs) {
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
//request components
// request components
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams());
List<ApiParam> requestParams = method.getRequestParams();
Map<String, Object> prop = buildProperties(requestParams, component, false);
component.put(requestSchema, prop);
//response components
// response components
List<ApiParam> responseParams = method.getResponseParams();
String schemaName = OpenApiSchemaUtil.getClassNameFromParams(method.getResponseParams());
component.put(schemaName, buildProperties(responseParams, component, true));
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/ly/doc/constants/DocGlobalConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,30 @@ public interface DocGlobalConstants {
String CURL_POST_PUT_JSON = "curl -X %s -H 'Content-Type: application/json;charset=UTF-8' %s -i %s --data '%s'";

String EMPTY = "";

String ENUM = "enum";

String ARRAY = "array";
/**
* param type enum
*/
String PARAM_TYPE_ENUM = "enum";
/**
* param type array
*/
String PARAM_TYPE_ARRAY = "array";
/**
* param type file
*/
String PARAM_TYPE_FILE = "file";

String OBJECT = "object";
/**
* param type text
*/
String PARAM_TYPE_TEXT = "text";
/**
* param type map
*/
String PARAM_TYPE_MAP = "map";
/**
* param type object
*/
String PARAM_TYPE_OBJECT = "object";

String JSON_PROPERTY_READ_WRITE = "JsonProperty.Access.READ_WRITE";

Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/ly/doc/helper/FormDataBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
FormData formData = new FormData();
formData.setKey(pre);
formData.setType("text");
formData.setType(DocGlobalConstants.PARAM_TYPE_TEXT);
formData.setValue(StringUtil.removeQuotes(RandomUtil.randomValueByType(className)));
formDataList.add(formData);
return formDataList;
Expand Down Expand Up @@ -123,7 +123,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
if (JavaClassValidateUtil.isFile(fieldGicName)) {
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("file");
formData.setType(DocGlobalConstants.PARAM_TYPE_FILE);
if (fieldGicName.contains("[]") || fieldGicName.endsWith(">")) {
comment = comment + "(array of file)";
formData.setType(DocGlobalConstants.PARAM_TYPE_FILE);
Expand All @@ -136,15 +136,16 @@ public static List<FormData> getFormData(String className, Map<String, String> r
if (StringUtil.isEmpty(fieldValue)) {
fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
}
CustomField customRequestField = builder.getCustomReqFieldMap().get(fieldName);
CustomField.Key key = CustomField.Key.create(docField.getDeclaringClassName(), fieldName);
CustomField customRequestField = builder.getCustomReqFieldMap().get(key);
// cover request value
if (Objects.nonNull(customRequestField) && Objects.nonNull(customRequestField.getValue())
&& JavaClassUtil.isTargetChildClass(simpleName, customRequestField.getOwnerClassName())) {
fieldValue = String.valueOf(customRequestField.getValue());
}
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setType(DocGlobalConstants.PARAM_TYPE_TEXT);
formData.setValue(fieldValue);
formData.setDescription(comment);
formDataList.add(formData);
Expand All @@ -155,7 +156,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
}
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setType(DocGlobalConstants.PARAM_TYPE_TEXT);
formData.setValue(StringUtil.removeQuotes(String.valueOf(value)));
formData.setDescription(comment);
formDataList.add(formData);
Expand All @@ -174,7 +175,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
}
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setType(DocGlobalConstants.PARAM_TYPE_TEXT);
formData.setValue(fieldValue);
formData.setDescription(comment);
formDataList.add(formData);
Expand All @@ -193,14 +194,16 @@ public static List<FormData> getFormData(String className, Map<String, String> r
}
}
}
} else if (subTypeName.length() == 1 || DocGlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
}
// else if (subTypeName.length() == 1 || DocGlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
// For Generics,do nothing, spring mvc not support
// if (n < globGicName.length) {
// String gicName = globGicName[n];
// formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + fieldName + "."));
// }
// n++;
} else {
// }
else {
formDataList.addAll(getFormData(javaClass.getGenericFullyQualifiedName(), registryClasses, counter, builder, pre + fieldName + "."));
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setClassName(className)
.setId(atomicOrDefault(atomicInteger, pid + 1))
.setField(pre + "any object")
.setType("object")
.setType(DocGlobalConstants.PARAM_TYPE_OBJECT)
.setPid(pid)
.setDesc(DocGlobalConstants.ANY_OBJECT_MSG)
.setVersion(DocGlobalConstants.DEFAULT_VERSION)
Expand Down Expand Up @@ -177,7 +177,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
}

String subTypeName = docField.getTypeFullyQualifiedName();
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
boolean needToUnderline = (responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp);
if (needToUnderline) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
String typeSimpleName = field.getType().getSimpleName();
Expand Down Expand Up @@ -287,7 +288,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
fieldName = fieldName.trim();
// Analyzing File Type Field
if (JavaClassValidateUtil.isFile(fieldGicName)) {
ApiParam param = ApiParam.of().setField(pre + fieldName).setType("file")
ApiParam param = ApiParam.of().setField(pre + fieldName)
.setType(DocGlobalConstants.PARAM_TYPE_FILE)
.setClassName(className)
.setPid(pid)
.setId(atomicOrDefault(atomicInteger, paramList.size() + pid + 1))
Expand Down Expand Up @@ -377,7 +379,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
// rpc
param.setType(JavaFieldUtil.convertToSimpleTypeName(docField.getTypeGenericFullyQualifiedName()));
} else {
param.setType("array");
param.setType(DocGlobalConstants.PARAM_TYPE_ARRAY);
}
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
param.setValue(fieldValue);
Expand Down Expand Up @@ -442,7 +444,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level

} else if (JavaClassValidateUtil.isMap(subTypeName)) {
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
param.setType("map");
param.setType(DocGlobalConstants.PARAM_TYPE_MAP);
param.setValue(fieldValue);
}
commonHandleParam(paramList, param, isRequired, comment + appendComment, since, strRequired);
Expand All @@ -455,7 +457,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setId(atomicOrDefault(atomicInteger, fieldPid + 1)).setPid(fieldPid)
.setClassName(className)
.setMaxLength(maxLength)
.setType("object")
.setType(DocGlobalConstants.PARAM_TYPE_OBJECT)
.setDesc(DocGlobalConstants.ANY_OBJECT_MSG)
.setVersion(DocGlobalConstants.DEFAULT_VERSION);
paramList.add(param1);
Expand Down Expand Up @@ -488,13 +490,13 @@ public static List<ApiParam> buildParams(String className, String pre, int level
String simple = DocClassUtil.getSimpleName(gicName);
// set type array
if (JavaClassValidateUtil.isArray(gicName)) {
param.setType(DocGlobalConstants.ARRAY);
param.setType(DocGlobalConstants.PARAM_TYPE_ARRAY);
}
if (JavaClassValidateUtil.isPrimitive(simple)) {
//do nothing
} else if (gicName.contains("<")) {
if (JavaClassValidateUtil.isCollection(simple)) {
param.setType(DocGlobalConstants.ARRAY);
param.setType(DocGlobalConstants.PARAM_TYPE_ARRAY);
String gName = DocClassUtil.getSimpleGicName(gicName)[0];
if (!JavaClassValidateUtil.isPrimitive(gName)) {
paramList.addAll(buildParams(gName, preBuilder.toString(), nextLevel, isRequired
Expand All @@ -521,7 +523,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setId(atomicOrDefault(atomicInteger, paramList.size() + pid + 1))
.setClassName(subTypeName)
.setMaxLength(maxLength)
.setType("object")
.setType(DocGlobalConstants.PARAM_TYPE_OBJECT)
.setDesc(comment.append(" $ref... self").toString())
.setVersion(DocGlobalConstants.DEFAULT_VERSION);
paramList.add(param1);
Expand Down Expand Up @@ -620,7 +622,7 @@ private static String handleEnumComment(JavaClass javaClass, ProjectDocConfigBui
if (Objects.isNull(dataDictionary)) {
comment = comment + "<br/>[Enum values:<br/>" + JavaClassUtil.getEnumParams(javaClass) + "]";
} else {
Class enumClass = dataDictionary.getEnumClass();
Class<?> enumClass = dataDictionary.getEnumClass();
if (enumClass.isInterface()) {
ClassLoader classLoader = projectBuilder.getApiConfig().getClassLoader();
try {
Expand Down
Loading
Loading