Commit 27ab485b authored by duanledexianxianxian's avatar duanledexianxianxian 😁

Refactoring code.

parent dc061d6a
......@@ -3,9 +3,7 @@ package com.kim.apidoc.core;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kim.apidoc.core.common.helper.FileHelper;
import com.kim.apidoc.core.render.AsciiDocRender;
import com.kim.apidoc.core.render.MarkdownRender;
import com.kim.apidoc.core.render.PostmanRender;
import com.kim.apidoc.core.render.ProjectRender;
import lombok.Getter;
import lombok.Setter;
......@@ -54,8 +52,8 @@ public class Context {
*/
@Setter
private List<ProjectRender> renders = Lists.newArrayList(
new AsciiDocRender(),
new PostmanRender(),
//new AsciiDocRender(),
//new PostmanRender(),
new MarkdownRender());
/**
......
package com.kim.apidoc.core.common.description;
import com.kim.apidoc.core.schema.Row;
import com.kim.apidoc.core.common.helper.StringHelper;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.javadoc.Javadoc;
import com.google.common.collect.Lists;
import com.kim.apidoc.core.common.helper.CommentHelper;
import com.kim.apidoc.core.common.helper.StringHelper;
import com.kim.apidoc.core.schema.Row;
import lombok.Getter;
import lombok.Setter;
......@@ -168,9 +171,9 @@ public abstract class TypeDescription {
*
* @return the collection
*/
public Collection<Row> rows() {
String key = fullKey();
if (StringHelper.isBlank(key)) {
public Collection<Row> rows(String requestParameterType) {
String fullKey = fullKey();
if (StringHelper.isBlank(fullKey)) {
return Lists.newArrayList();
}
String def;
......@@ -186,7 +189,26 @@ public abstract class TypeDescription {
condition.append("required=").append(required);
}
return Lists.newArrayList(new Row(key, type,false, condition.toString(), def, remark));
return Lists.newArrayList(new Row(key, type, false, condition.toString(), def, remark, requestParameterType));
}
/**
* Rows collection.
*
* @return the collection
*/
public Collection<Row> rows() {
return rows(null);
}
public void accept(Comment comment) {
if (!comment.isJavadocComment()) {
setRemark(comment.getContent());
return;
}
Javadoc javadoc = comment.asJavadocComment().parse();
setRemark(CommentHelper.getDescription(javadoc.getDescription()));
}
}
......@@ -13,26 +13,26 @@ public class StringHelper {
}
public static boolean isBlank(Object text) {
if(text instanceof String){
if (text instanceof String) {
return isBlank(((String) text));
}
return isBlank(String.valueOf(text));
}
public static boolean nonBlank(Object text) {
if(text instanceof String){
if (text instanceof String) {
return nonBlank(((String) text));
}
return nonBlank(String.valueOf(text));
}
public static String join(String delimiter, String ... values){
public static String join(String delimiter, String... values) {
StringBuilder builder = new StringBuilder();
for (String value : values) {
if(isBlank(value)){
if (isBlank(value)) {
continue;
}
if(builder.length()>0){
if (builder.length() > 0) {
builder.append(delimiter);
}
builder.append(value);
......
......@@ -3,6 +3,7 @@ package com.kim.apidoc.core.resolver;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.kim.apidoc.core.ApiDoc;
import com.kim.apidoc.core.common.description.ObjectTypeDescription;
import com.kim.apidoc.core.common.description.TypeDescription;
......@@ -27,15 +28,16 @@ public class ObjectTypeResolver implements TypeResolver {
ObjectTypeDescription typeDescription = new ObjectTypeDescription();
typeDescription.setType(referenceType.getTypeDeclaration().getName());
((JavaParserClassDeclaration) referenceType.getTypeDeclaration()).getWrappedNode().getComment().ifPresent(typeDescription::accept);
//类型解析缓冲池,防止循环引用
if(!ReferenceContext.getInstance().push(referenceType.describe())){
if (!ReferenceContext.getInstance().push(referenceType.describe())) {
return typeDescription;
}
//解析父类属性,并合并至当前
for (ResolvedReferenceType directAncestor : referenceType.getDirectAncestors()) {
TypeDescription ancestorDescription = ApiDoc.getInstance().getTypeResolvers().resolve(directAncestor);
if(ancestorDescription.isAvailable() && ancestorDescription.isObject()){
if (ancestorDescription.isAvailable() && ancestorDescription.isObject()) {
typeDescription.merge(ancestorDescription.asObject());
}
}
......@@ -43,19 +45,19 @@ public class ObjectTypeResolver implements TypeResolver {
//TODO fix use access method
for (ResolvedFieldDeclaration declaredField : referenceType.getTypeDeclaration().getDeclaredFields()) {
if(declaredField.isStatic()){
if (declaredField.isStatic()) {
continue;
}
ResolvedType fieldType = declaredField.getType();
if(fieldType.isReferenceType()){
if (fieldType.isReferenceType()) {
//将父类的T,传递给 属性的T
fieldType = TypeParameterHelper.useClassTypeParameter(referenceType,fieldType.asReferenceType());
fieldType = TypeParameterHelper.useClassTypeParameter(referenceType, fieldType.asReferenceType());
}
if(declaredField.getType().isTypeVariable()){
if (declaredField.getType().isTypeVariable()) {
//类型为T,这种泛型
Optional<ResolvedType> optional = TypeParameterHelper.getTypeParameter(referenceType, declaredField.getType().asTypeParameter().getName());
if(optional.isPresent()){
if (optional.isPresent()) {
fieldType = optional.get();
}
}
......@@ -71,7 +73,7 @@ public class ObjectTypeResolver implements TypeResolver {
fieldDescription.getCondition().append(validation).append(" ");
}
//查找字段初始化值
FieldHelper.getInitializer(declaredField).ifPresent(expr-> fieldDescription.setDefaultValue(ExpressionHelper.getValue(expr)));
FieldHelper.getInitializer(declaredField).ifPresent(expr -> fieldDescription.setDefaultValue(ExpressionHelper.getValue(expr)));
typeDescription.add(fieldDescription);
}
......
......@@ -6,22 +6,8 @@ import com.github.javaparser.resolution.types.ResolvedType;
import com.google.common.collect.ImmutableList;
public class PrimitiveTypeResolver implements TypeResolver {
@Override
public boolean accept(ResolvedType type) {
return type.isPrimitive() || isBoxing(type);
}
@Override
public TypeDescription resolve(ResolvedType type) {
if(type.isPrimitive()){
return new PrimitiveTypeDescription(type.asPrimitive());
}else{
return new PrimitiveTypeDescription(type.asReferenceType());
}
}
private static boolean isBoxing(ResolvedType type){
if(!type.isReferenceType()){
private static boolean isBoxing(ResolvedType type) {
if (!type.isReferenceType()) {
return false;
}
String id = type.asReferenceType().getId();
......@@ -37,4 +23,18 @@ public class PrimitiveTypeResolver implements TypeResolver {
).contains(id);
}
@Override
public boolean accept(ResolvedType type) {
return type.isPrimitive() || isBoxing(type);
}
@Override
public TypeDescription resolve(ResolvedType type) {
if (type.isPrimitive()) {
return new PrimitiveTypeDescription(type.asPrimitive());
} else {
return new PrimitiveTypeDescription(type.asReferenceType());
}
}
}
......@@ -34,55 +34,57 @@ public class TypeResolvers {
/**
* 获取类型信息
*
* @param type
* @return
*/
public TypeDescription resolve(Type type){
try{
public TypeDescription resolve(Type type) {
try {
ResolvedType resolvedType = type.resolve();
return resolve(resolvedType);
} catch (UnsolvedSymbolException e){
} catch (UnsolvedSymbolException e) {
//解析失败时,尝试降级,使用名称解析
return nameResolve(type);
} catch (Exception e){
log.error(e.getMessage(),e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return new UnAvailableTypeDescription();
}
/**
* 解析类型信息
*
* @param type
* @return
*/
public TypeDescription resolve(ResolvedType type){
public TypeDescription resolve(ResolvedType type) {
for (TypeResolver typeResolver : resolvers) {
if(typeResolver.accept(type)){
if (typeResolver.accept(type)) {
return typeResolver.resolve(type);
}
}
if(objectTypeResolver.accept(type)){
if (objectTypeResolver.accept(type)) {
return objectTypeResolver.resolve(type);
}
return new UnAvailableTypeDescription();
}
public TypeDescription nameResolve(Type type){
public TypeDescription nameResolve(Type type) {
String id = TypeNameHelper.getName(type);
for (TypeNameResolver nameResolver : nameResolvers) {
if (nameResolver.accept(id)) {
return nameResolver.resolve(type);
}
}
log.warn("type({}) resolve failed",id);
log.warn("type({}) resolve failed", id);
return new UnAvailableTypeDescription();
}
public void addResolver(TypeResolver typeResolver){
public void addResolver(TypeResolver typeResolver) {
resolvers.add(typeResolver);
}
public void addNameResolver(TypeNameResolver nameResolver){
public void addNameResolver(TypeNameResolver nameResolver) {
nameResolvers.add(nameResolver);
}
......
package com.kim.apidoc.core.schema;
/**
* 请求参数枚举类型
*
* @author duanledexianxianxian
* @date 2020 /3/29 0:37
* @since 1.0.0
*/
public enum RequestParameterType {
/**
* 查询参数.
*/
QUERY,
/**
* 路径参数.
*/
PATH,
/**
* 请求参数体.
*/
BODY
}
......@@ -40,6 +40,12 @@ public class Row {
*/
String remark;
/**
* 请求参数类型
*/
String requestParameterType;
/**
* Instantiates a new Row.
*
......
......@@ -44,6 +44,7 @@ public class Section extends Node {
* The Query parameter.
*/
boolean queryParameter = true;
/**
* The Request rows.
*/
......
......@@ -56,11 +56,11 @@ ${section.getParameterString()}
<#-- 请求参数table列表-->
<#if section.requestRows?? && (section.requestRows?size>0)>
**Query**
| Field | Type | Required | Condition | Default | Description |
| :------- | :----- | :-------- |:-------- | :------ | :---------- |
**Request**
| Field | Type | Request Type | Required | Condition | Default | Description |
| :------- | :----- | :----- |:-------- |:-------- | :------ | :---------- |
<#list section.requestRows as rowKey,rowValue>
| ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} |
| ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.requestParameterType!''} |${rowValue.required?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} |
</#list>
</#if>
<#-- 响应-->
......
......@@ -22,6 +22,8 @@ import com.github.javaparser.ast.expr.Expression;
import java.util.List;
import java.util.Optional;
import static com.kim.apidoc.core.schema.RequestParameterType.*;
/**
* spring 解析
......@@ -43,7 +45,6 @@ public class SpringParser implements ParserStrategy {
public static final String EXT_URI = "uri";
@Override
public String name() {
return FRAMEWORK;
......@@ -157,6 +158,7 @@ public class SpringParser implements ParserStrategy {
if (ParameterHelper.isPathVariable(parameter)) {
section.getPathVariable().put(parameter.getNameAsString(), "");
Row row = new Row();
row.setRequestParameterType(PATH.name());
row.setKey(parameter.getNameAsString());
row.setType(parameter.getType().toString());
// 路径参数必填
......@@ -235,7 +237,7 @@ public class SpringParser implements ParserStrategy {
} else if (description.isObject()) {
section.setParameter(description.asObject().getValue());
}
section.addRequestRows(description.rows());
section.addRequestRows(description.rows(BODY.name()));
}
break;
}
......@@ -294,7 +296,7 @@ public class SpringParser implements ParserStrategy {
}
}
section.setParameter(objectTypeDescription.getValue());
section.addRequestRows(objectTypeDescription.rows());
section.addRequestRows(objectTypeDescription.rows(QUERY.name()));
}
/**
......
......@@ -8,10 +8,12 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
/**
* 用户对象
*
* @author fengyuchenglun
* @version 1.0.0
*/
@Setter
@Getter
......
......@@ -3,8 +3,8 @@ package com.kim.apidoc.example.spring.advanced;
import com.kim.apidoc.example.annotation.KimController;
import com.kim.apidoc.example.common.ResultData;
import com.kim.apidoc.example.common.User;
import com.kim.apidoc.example.common.UserQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -25,11 +25,11 @@ public class KimUserController {
* 用户详情信息
* 主动根据id获取用户的信息
*
* @param id 用户编号
* @return 用户数据
* @param userQuery the user query
* @return result data
*/
@GetMapping(value = "/{id}")
public ResultData<User> detail(@PathVariable String id) {
public ResultData<User> detail(UserQuery userQuery) {
User user = new User();
return ResultData.ok(user);
}
......
......@@ -17,7 +17,7 @@ public class SpringTest {
Context context = new Context();
context.setId("test");
context.setName("测试项目");
context.addSource(Paths.get("F:\\@project@\\@dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java"));
context.addSource(Paths.get("K:\\@project-dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java"));
// context.setCss("https://darshandsoni.com/asciidoctor-skins/css/monospace.css");
ApiDoc apigcc = new ApiDoc(context);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment