Skip to content
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'cn.gudqs7.idea.plugins'
version '2.6.0'
version '2.6.2'

repositories {
mavenCentral()
Expand Down
4 changes: 4 additions & 0 deletions parts/changeNotes.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf8">

<pre>
<strong>Release v2.6.2</strong>
1.支持java8所有的时间类型
2.修复近期的一些Issues缺陷
3.支持swagger3注解(ruoyi框架常用的几个);
<strong>Release v2.6.0</strong>
1.feat: 批量导出时, 新增带全部数据的单独 markdown 文件; #88
2.fix: 修复随机字符串使用了中文生僻字过多像乱码一样的问题: 不生成中文, 生成字母; #89
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public T read0(StructureAndCommentInfo structureAndCommentInfo, Map<String, Obje
} else {
T leafData = readLeaf(structureAndCommentInfo, data, parentData);
Map<String, StructureAndCommentInfo> children = structureAndCommentInfo.getChildren();
if (children != null && children.size() > 0) {
if (children != null && !children.isEmpty()) {
Map<String, Object> loopData = new HashMap<>(8);
loopData.put(MapKeyConstant.READER_PARENT_KEY, parentData);
beforeLoop(structureAndCommentInfo, loopData, data, parentData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum CommentTagEnum {
EXAMPLE("example"),
NOTES("notes"),
TAGS("tags"),
NAME("name"),
IMPORTANT("important", true),
DESCRIPTION("description"),
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public interface AnnotationHolder {
String QNAME_OF_PARAM = "io.swagger.annotations.ApiParam";
String QNAME_OF_RESPONSE = "io.swagger.annotations.ApiResponse";
String QNAME_OF_RESPONSES = "io.swagger.annotations.ApiResponses";

// 新增 Swagger v3 注解
String QNAME_OF_OPENAPI_API = "io.swagger.v3.oas.annotations.tags.Tag";
String QNAME_OF_OPENAPI_OPERATION = "io.swagger.v3.oas.annotations.Operation";
String QNAME_OF_OPENAPI_SCHEMA = "io.swagger.v3.oas.annotations.media.Schema";
String QNAME_OF_OPENAPI_PARAMETER = "io.swagger.v3.oas.annotations.Parameter";
String QNAME_OF_OPENAPI_RESPONSE = "io.swagger.v3.oas.annotations.responses.ApiResponse";
String QNAME_OF_OPENAPI_RESPONSES = "io.swagger.v3.oas.annotations.responses.ApiResponses";

String QNAME_OF_MAPPING = "org.springframework.web.bind.annotation.RequestMapping";
String QNAME_OF_GET_MAPPING = "org.springframework.web.bind.annotation.GetMapping";
String QNAME_OF_POST_MAPPING = "org.springframework.web.bind.annotation.PostMapping";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public CommentInfo getCommentInfoByAnnotation() {
commentInfo.setValue(getAnnotationValueByQname(QNAME_OF_MODEL, CommentTagEnum.DEFAULT.getTag()));
}
if (hasAnnotation(QNAME_OF_API)) {
commentInfo.setValue(getAnnotationValueByQname(QNAME_OF_API, CommentTagEnum.DESCRIPTION.getTag()));
commentInfo.setNotes(getAnnotationValueByQname(QNAME_OF_API, CommentTagEnum.DESCRIPTION.getTag()));
String description = getAnnotationValueByQname(QNAME_OF_API, CommentTagEnum.DESCRIPTION.getTag());
commentInfo.setValue(description);
commentInfo.setNotes(description);
List<String> tagsList = getAnnotationListValueByQname(QNAME_OF_API, CommentTagEnum.TAGS.getTag());
String tags = "";
if (CollectionUtils.isNotEmpty(tagsList)) {
Expand All @@ -111,12 +112,18 @@ public CommentInfo getCommentInfoByAnnotation() {
commentInfo.setTags(tags);
commentInfo.setHidden(getAnnotationValueByQname(QNAME_OF_API, CommentTagEnum.HIDDEN.getTag()));
}
if (hasAnnotation(QNAME_OF_OPENAPI_API)) {
String description = getAnnotationValueByQname(QNAME_OF_OPENAPI_API, CommentTagEnum.DESCRIPTION.getTag());
commentInfo.setValue(description);
commentInfo.setNotes(description);
commentInfo.setTags(getAnnotationValueByQname(QNAME_OF_OPENAPI_API, CommentTagEnum.NAME.getTag()));
}
return commentInfo;
}

@Override
protected boolean usingAnnotation() {
return hasAnyOneAnnotation(QNAME_OF_MODEL, QNAME_OF_API);
return hasAnyOneAnnotation(QNAME_OF_MODEL, QNAME_OF_API, QNAME_OF_OPENAPI_API);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.gudqs7.plugins.common.enums.MoreCommentTagEnum;
import cn.gudqs7.plugins.common.pojo.resolver.CommentInfo;
import cn.gudqs7.plugins.common.pojo.resolver.CommentInfoTag;
import cn.gudqs7.plugins.common.util.structure.PsiAnnotationUtil;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
Expand Down Expand Up @@ -79,12 +80,12 @@ public CommentInfoTag getCommentInfoByComment() {
@Override
public CommentInfo getCommentInfoByAnnotation() {
CommentInfo commentInfo = new CommentInfo();
boolean hasAnnotatation = hasAnnotation(QNAME_OF_PROPERTY);
if (hasAnnotatation) {
commentInfo.setHidden(getAnnotationValueByProperty(CommentTagEnum.HIDDEN.getTag()));
commentInfo.setRequired(getAnnotationValueByProperty(CommentTagEnum.REQUIRED.getTag()));
String value = getAnnotationValueByProperty(CommentTagEnum.DEFAULT.getTag());
String notes = getAnnotationValueByProperty(CommentTagEnum.NOTES.getTag());
PsiAnnotation psiAnnotation = getAnnotationByQname(QNAME_OF_PROPERTY);
if (psiAnnotation != null) {
commentInfo.setHidden(getAnnotationValueByProperty(psiAnnotation,CommentTagEnum.HIDDEN.getTag()));
commentInfo.setRequired(getAnnotationValueByProperty(psiAnnotation,CommentTagEnum.REQUIRED.getTag()));
String value = getAnnotationValueByProperty(psiAnnotation,CommentTagEnum.DEFAULT.getTag());
String notes = getAnnotationValueByProperty(psiAnnotation,CommentTagEnum.NOTES.getTag());
if (StringUtils.isNotBlank(value)) {
value = value.replaceAll("\\n", CommonConst.BREAK_LINE);
}
Expand All @@ -93,7 +94,15 @@ public CommentInfo getCommentInfoByAnnotation() {
}
commentInfo.setValue(value);
commentInfo.setNotes(notes);
commentInfo.setExample(getAnnotationValueByProperty(CommentTagEnum.EXAMPLE.getTag()));
commentInfo.setExample(getAnnotationValueByProperty(psiAnnotation,CommentTagEnum.EXAMPLE.getTag()));
}
psiAnnotation = getAnnotationByQname(QNAME_OF_OPENAPI_SCHEMA);
if (psiAnnotation != null) {
// 补充 CommentInfo 中已有的属性
commentInfo.setHidden(getAnnotationValueByProperty(psiAnnotation, "hidden"));
commentInfo.setRequired(getAnnotationValueByProperty(psiAnnotation, "required"));
commentInfo.setValue(getAnnotationValueByProperty(psiAnnotation, "description"));
commentInfo.setExample(getAnnotationValueByProperty(psiAnnotation, "example"));
}
dealOtherAnnotation(commentInfo);
return commentInfo;
Expand All @@ -118,9 +127,19 @@ private <T> T getAnnotationValueByProperty(String attr) {
return getAnnotationValueByQname(QNAME_OF_PROPERTY, attr);
}

/**
* 获取注解中的信息
*
* @param attr 注解字段
* @return 信息
*/
private <T> T getAnnotationValueByProperty( PsiAnnotation psiAnnotation,String attr) {
return PsiAnnotationUtil.getAnnotationValue(psiAnnotation, attr, null);
}

@Override
protected boolean usingAnnotation() {
return hasAnnotation(QNAME_OF_PROPERTY);
return hasAnyOneAnnotation(QNAME_OF_PROPERTY, QNAME_OF_OPENAPI_SCHEMA);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ public CommentInfoTag getCommentInfoByComment() {
@Override
public CommentInfo getCommentInfoByAnnotation() {
CommentInfo commentInfo = new CommentInfo();
boolean hasOperationAnnotation = hasAnnotation(QNAME_OF_OPERATION);
if (hasOperationAnnotation) {

// 方法级别的注解:优先使用 @ApiOperation,如果没有则使用 @Operation
if (hasAnnotation(QNAME_OF_OPERATION)) {
// 使用 Swagger v2 的 @ApiOperation 注解
commentInfo.setHidden(getAnnotationValueByApiOperation(CommentTagEnum.HIDDEN.getTag()));
String value = getAnnotationValueByApiOperation(CommentTagEnum.DEFAULT.getTag());
String notes = getAnnotationValueByApiOperation(CommentTagEnum.NOTES.getTag());
Expand All @@ -126,34 +128,40 @@ public CommentInfo getCommentInfoByAnnotation() {
}
commentInfo.setValue(value);
commentInfo.setNotes(notes);
} else if (hasAnnotation(QNAME_OF_OPENAPI_OPERATION)) {
// 使用 Swagger v3 的 @Operation 注解
String summary = getAnnotationValueByQname(QNAME_OF_OPENAPI_OPERATION, "summary");
String description = getAnnotationValueByQname(QNAME_OF_OPENAPI_OPERATION, "description");
commentInfo.setValue(summary); // 映射到 value
commentInfo.setNotes(description); // 映射到 notes
}
// 添加 knife4j 的 ApiOperationSupport 注解支持, 主要是 includeParameters 和 ignoreParameters
// 优先级是有 ignoreParameters 则跳过 includeParameters 字段
if (hasAnnotation(QNAME_OF_OPERATION_SUPPORT)) {
List<String> includeParameters = getAnnotationValueByQname(QNAME_OF_OPERATION_SUPPORT, "includeParameters");
addRequestTagInfo(commentInfo, includeParameters, MoreCommentTagEnum.ONLY_REQUEST.getTag());
List<String> ignoreParameters = getAnnotationValueByQname(QNAME_OF_OPERATION_SUPPORT, "ignoreParameters");
addRequestTagInfo(commentInfo, ignoreParameters, MoreCommentTagEnum.HIDDEN_REQUEST.getTag());
}

// 响应信息:优先使用 @ApiResponses,如果没有则使用 @ApiResponses
if (hasAnnotation(QNAME_OF_RESPONSES)) {
// 存在多个 code
// 使用 Swagger v2 的 @ApiResponses 注解
List<PsiAnnotation> psiAnnotationList = getAnnotationValueByQname(QNAME_OF_RESPONSES, "value");
if (psiAnnotationList != null && psiAnnotationList.size() > 0) {
if (psiAnnotationList != null) {
for (PsiAnnotation psiAnnotation : psiAnnotationList) {
Integer code = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "code", null);
String message = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "message", null);
ResponseCodeInfo codeInfo = new ResponseCodeInfo(String.valueOf(code), message);
commentInfo.getResponseCodeInfoList().add(codeInfo);
commentInfo.getResponseCodeInfoList().add(new ResponseCodeInfo(String.valueOf(code), message));
}
}
} else if (hasAnnotation(QNAME_OF_OPENAPI_RESPONSES)) {
// 使用 Swagger v3 的 @ApiResponses 注解
List<PsiAnnotation> psiAnnotationList = getAnnotationValueByQname(QNAME_OF_OPENAPI_RESPONSES, "value");
if (psiAnnotationList != null) {
for (PsiAnnotation psiAnnotation : psiAnnotationList) {
Integer code = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "responseCode", null);
String description = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "description", null);
commentInfo.getResponseCodeInfoList().add(new ResponseCodeInfo(String.valueOf(code), description));
}
}
} else if (hasAnnotation(QNAME_OF_RESPONSE)) {
// 存在单个 code
Integer code = getAnnotationValueByQname(QNAME_OF_RESPONSE, "code");
String message = getAnnotationValueByQname(QNAME_OF_RESPONSE, "message");
ResponseCodeInfo codeInfo = new ResponseCodeInfo(String.valueOf(code), message);
commentInfo.getResponseCodeInfoList().add(codeInfo);
}

// 请求映射处理
dealRequestMapping(commentInfo);

return commentInfo;
}

Expand All @@ -164,6 +172,7 @@ public CommentInfo getCommentInfoByAnnotation() {
* @return 信息
*/
private <T> T getAnnotationValueByApiOperation(String attr) {

return getAnnotationValueByQname(QNAME_OF_OPERATION, attr);
}

Expand Down Expand Up @@ -310,7 +319,7 @@ private void dealHttpMethod(CommentInfo commentInfo, String qnameOfXxxMapping, S

@Override
protected boolean usingAnnotation() {
return hasAnnotation(QNAME_OF_OPERATION);
return hasAnyOneAnnotation(QNAME_OF_OPERATION,QNAME_OF_OPENAPI_OPERATION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,67 +35,94 @@ public CommentInfoTag getCommentInfoByComment() {
CommentInfoTag commentInfoTag = new CommentInfoTag();
PsiElement parent = psiParameter.getParent().getParent();
String parameterName = psiParameter.getName();

if (parent instanceof PsiMethod) {
for (PsiElement child : parent.getChildren()) {
if (child instanceof PsiDocComment) {
Map<String, CommentTagEnum> commentTagMap = CommentTagEnum.allTagMap();
Map<String, MoreCommentTagEnum> moreCommentTagMap = MoreCommentTagEnum.allTagMap();
PsiDocComment psiComment = (PsiDocComment) child;
String text = psiComment.getText();

if (text.startsWith("/**") && text.endsWith("*/")) {
String[] lines = text.replaceAll("\r", "").split("\n");
for (String line : lines) {
if (line.contains("/**") || line.contains("*/")) {
continue;
}
line = removeJavaDocPrefix(line);
if (StringUtils.isBlank(line)) {
line = processCommentLine(line);
if (line == null) {
continue;
}
if (line.contains("@")) {
String atParam = "@param";
if (line.startsWith("@param")) {
line = line.substring(atParam.length()).trim();
String[] paramInfoArray = line.split(" ");
String fieldName = "";
if (paramInfoArray.length > 0) {
fieldName = paramInfoArray[0];
}
if (!fieldName.equals(parameterName)) {
continue;
}
String tag = line.substring(fieldName.length()).trim();
String[] tagArray = tag.split(" ");
for (String t : tagArray) {
String tagName = t;
String tagVal = null;
if (t.contains("=")) {
String[] tagKeyVal = t.split("=");
tagName = tagKeyVal[0];
if (tagKeyVal.length > 1) {
tagVal = tagKeyVal[1];
}
}
if (commentTagMap.containsKey(tagName)) {
setCommentInfoByTag(commentInfoTag, tagName, tagVal);
} else if (moreCommentTagMap.containsKey(tagName)) {
commentInfoTag.appendToTag(tagName, tagVal);
} else {
commentInfoTag.appendValue(t, CommonConst.SPACE);
}
}
}

if (line.startsWith("@param")) {
processParamTag(line, parameterName, commentTagMap, moreCommentTagMap, commentInfoTag);
} else {
commentInfoTag.appendValue(line, CommonConst.SPACE);
}
}
}
break;
}
}
}

dealOtherAnnotation(commentInfoTag);
return commentInfoTag;
}

private String processCommentLine(String line) {
// 去除注释前缀并修剪空白字符
line = removeJavaDocPrefix(line).trim();
if (StringUtils.isBlank(line)) {
return null; // 如果行为空或仅包含空白字符,返回 null
}
return line;
}

private void processParamTag(String line, String parameterName,
Map<String, CommentTagEnum> commentTagMap,
Map<String, MoreCommentTagEnum> moreCommentTagMap,
CommentInfoTag commentInfoTag) {
// 去掉 "@param" 前缀
line = line.substring("@param".length()).trim();
// 分割参数名和注释内容
String[] paramInfoArray = line.split(" ", 2);
if (paramInfoArray.length < 2) {
return; // 格式错误,跳过
}

String fieldName = paramInfoArray[0];
if (!fieldName.equals(parameterName)) {
return; // 不是当前参数,跳过
}

// 获取注释内容部分
String tagContent = paramInfoArray[1];
// 分割注释内容中的标签
String[] tagArray = tagContent.split(" ");
for (String tagEntry : tagArray) {
String tagName = tagEntry;
String tagVal = null;

// 处理键值对形式的标签
if (tagEntry.contains("=")) {
String[] tagKeyValue = tagEntry.split("=", 2);
tagName = tagKeyValue[0];
if (tagKeyValue.length > 1) {
tagVal = tagKeyValue[1];
}
}

// 根据标签类型处理
if (commentTagMap.containsKey(tagName)) {
setCommentInfoByTag(commentInfoTag, tagName, tagVal);
} else if (moreCommentTagMap.containsKey(tagName)) {
commentInfoTag.appendToTag(tagName, tagVal);
} else {
commentInfoTag.appendValue(tagEntry, CommonConst.SPACE);
}
}
}


@Override
public CommentInfo getCommentInfoByAnnotation() {
CommentInfo commentInfo = new CommentInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private StructureAndCommentInfo resolveByPsiType0(StructureAndCommentInfo parent
structureAndCommentInfo.setFieldName(fieldName);
structureAndCommentInfo.setCommentInfo(commentInfo);
structureAndCommentInfo.setFieldType(fieldTypeName);
structureAndCommentInfo.setOriginalFieldType(psiFieldTypeName);
structureAndCommentInfo.setOriginalFieldType(psiFieldType.getCanonicalText());
structureAndCommentInfo.setFieldTypeCode(fieldTypeCode);
structureAndCommentInfo.setOriginalFieldTypeCode(FieldType.BASE.getType());
structureAndCommentInfo.setPsiType(psiFieldType);
Expand Down
Loading