Skip to content

Commit

Permalink
Merge pull request #759 from baboy/master
Browse files Browse the repository at this point in the history
fixed #758 fixed #749: add extension in parameters to support openapi…
  • Loading branch information
shalousun authored Mar 12, 2024
2 parents a7b10a8 + abcf770 commit 19c4589
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
propertiesData.put("description", apiParam.getDesc() + "(object)");
}
}
if (apiParam.getExtensions() != null && !apiParam.getExtensions().isEmpty()){
apiParam.getExtensions().entrySet().forEach( e-> propertiesData.put("x-"+e.getKey(), e.getValue()));
}

return propertiesData;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/ly/doc/builder/openapi/OpenApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.ly.doc.helper.JavaProjectBuilderHelper;
import com.ly.doc.model.openapi.OpenApiTag;
import com.ly.doc.utils.JsonUtil;
import com.power.common.util.StringUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -239,6 +240,10 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
Map<String, Object> parameters;
parameters = new HashMap<>(20);
//add mock value for parameters
if (StringUtils.isNotEmpty(apiParam.getValue())) {
parameters.put("example", StringUtil.removeDoubleQuotes(apiParam.getValue()));
}
if (!hasItems) {
parameters.put("name", apiParam.getField());
parameters.put("description", apiParam.getDesc());
Expand All @@ -262,6 +267,9 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
}
parameters.putAll(buildParametersSchema(apiParam));
}
if (apiParam.getExtensions() != null && !apiParam.getExtensions().isEmpty()){
apiParam.getExtensions().entrySet().forEach( e-> parameters.put("x-"+e.getKey(), e.getValue()));
}

return parameters;
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/ly/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public static List<ApiParam> buildParams(String className, String pre, int level
if (tagsMap.containsKey(DocTags.SINCE)) {
since = tagsMap.get(DocTags.SINCE);
}
//handle extension
Map<String, String> extensions = DocUtil.getCommentsByTag(field.getTagsByName(DocTags.EXTENSION), DocTags.EXTENSION);
Map<String, Object> extensionParams = new HashMap<>();
if (extensions != null && !extensions.isEmpty()){
extensions.forEach( (k, v) -> extensionParams.put(k, DocUtil.detectTagValue(v)));
}

boolean strRequired = false;
CustomField.Key key = CustomField.Key.create(docField.getDeclaringClassName(), fieldName);
Expand Down Expand Up @@ -296,7 +302,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setMaxLength(maxLength)
.setDesc(comment.toString())
.setRequired(strRequired)
.setVersion(since);
.setVersion(since)
.setExtensions(extensionParams);
if (fieldGicName.contains("[]") || fieldGicName.endsWith(">")) {
param.setType(DocGlobalConstants.PARAM_TYPE_FILE);
param.setDesc(comment.append("(array of file)").toString());
Expand All @@ -314,6 +321,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
param.setId(atomicOrDefault(atomicInteger, paramList.size() + param.getPid() + 1));
String processedType = processFieldTypeName(isShowJavaType, subTypeName);
param.setType(processedType);
param.setExtensions(extensionParams);
// handle param
commonHandleParam(paramList, param, isRequired, comment.toString(), since, strRequired);

Expand Down Expand Up @@ -349,6 +357,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
int fieldPid;
ApiParam param = ApiParam.of().setField(pre + fieldName).setClassName(className).setPid(pid).setMaxLength(maxLength);
param.setId(atomicOrDefault(atomicInteger, paramList.size() + param.getPid() + 1));
param.setExtensions(extensionParams);
String processedType;
if (fieldGicName.length() == 1) {
String gicName = DocGlobalConstants.JAVA_OBJECT_FULLY;
Expand Down Expand Up @@ -459,7 +468,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setMaxLength(maxLength)
.setType(DocGlobalConstants.PARAM_TYPE_OBJECT)
.setDesc(DocGlobalConstants.ANY_OBJECT_MSG)
.setVersion(DocGlobalConstants.DEFAULT_VERSION);
.setVersion(DocGlobalConstants.DEFAULT_VERSION)
.setExtensions(extensionParams);
paramList.add(param1);
continue;
}
Expand Down Expand Up @@ -525,7 +535,8 @@ public static List<ApiParam> buildParams(String className, String pre, int level
.setMaxLength(maxLength)
.setType(DocGlobalConstants.PARAM_TYPE_OBJECT)
.setDesc(comment.append(" $ref... self").toString())
.setVersion(DocGlobalConstants.DEFAULT_VERSION);
.setVersion(DocGlobalConstants.DEFAULT_VERSION)
.setExtensions(extensionParams);
paramList.add(param1);
} else {
commonHandleParam(paramList, param, isRequired, comment + appendComment, since, strRequired);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/ly/doc/model/ApiParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.ly.doc.model;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ly.doc.model.torna.EnumInfo;
Expand Down Expand Up @@ -125,6 +126,7 @@ public class ApiParam {
* Self Reference loop
*/
private boolean selfReferenceLoop;
private Map<String, Object> extensions;

public static ApiParam of() {
return new ApiParam();
Expand Down Expand Up @@ -311,6 +313,15 @@ public ApiParam setFullyTypeName(String fullyTypeName) {
return this;
}

public Map<String, Object> getExtensions() {
return extensions;
}

public ApiParam setExtensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

@Override
public String toString() {
return "ApiParam{" +
Expand All @@ -333,6 +344,7 @@ public String toString() {
", maxLength='" + maxLength + '\'' +
", configParam=" + configParam +
", selfReferenceLoop=" + selfReferenceLoop +
", extensions=" + extensions +
'}';
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/ly/doc/utils/DocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ public static Map<String, String> getRecordCommentsByTag(JavaClass javaClass, fi
"Please @see " + className;
return getCommentsByTag(paramTags, tagName, className, tagValNullMsg, tagValErrorMsg);
}

public static Map<String, String> getCommentsByTag(List<DocletTag> paramTags, final String tagName){
return getCommentsByTag(paramTags, tagName, null, null,null);
}
private static Map<String, String> getCommentsByTag(List<DocletTag> paramTags, final String tagName, String className,
String tagValNullMsg, String tagValErrorMsg) {
Map<String, String> paramTagMap = new HashMap<>(16);
Expand Down

0 comments on commit 19c4589

Please sign in to comment.