Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apidoc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanledexianxianxian
apidoc
Commits
27ab485b
Commit
27ab485b
authored
Mar 29, 2020
by
duanledexianxianxian
😁
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
♻
Refactoring code.
parent
dc061d6a
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
122 additions
and
64 deletions
+122
-64
apidoc-core/src/main/java/com/kim/apidoc/core/Context.java
apidoc-core/src/main/java/com/kim/apidoc/core/Context.java
+2
-4
apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java
...m/kim/apidoc/core/common/description/TypeDescription.java
+28
-6
apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java
.../java/com/kim/apidoc/core/common/helper/StringHelper.java
+5
-5
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java
...java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java
+10
-8
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java
...a/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java
+16
-16
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java
...main/java/com/kim/apidoc/core/resolver/TypeResolvers.java
+14
-12
apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java
...java/com/kim/apidoc/core/schema/RequestParameterType.java
+23
-0
apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java
...oc-core/src/main/java/com/kim/apidoc/core/schema/Row.java
+6
-0
apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java
...ore/src/main/java/com/kim/apidoc/core/schema/Section.java
+1
-0
apidoc-core/src/main/resources/templates/markdown.ftl
apidoc-core/src/main/resources/templates/markdown.ftl
+4
-4
apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java
.../src/main/java/com/kim/apidoc/springmvc/SpringParser.java
+5
-3
apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java
...mvc/src/test/java/com/kim/apidoc/example/common/User.java
+3
-1
apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java
...kim/apidoc/example/spring/advanced/KimUserController.java
+4
-4
apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java
...vc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java
+1
-1
No files found.
apidoc-core/src/main/java/com/kim/apidoc/core/Context.java
View file @
27ab485b
...
...
@@ -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
());
/**
...
...
apidoc-core/src/main/java/com/kim/apidoc/core/common/description/TypeDescription.java
View file @
27ab485b
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
k
ey
=
fullKey
();
if
(
StringHelper
.
isBlank
(
k
ey
))
{
public
Collection
<
Row
>
rows
(
String
requestParameterType
)
{
String
fullK
ey
=
fullKey
();
if
(
StringHelper
.
isBlank
(
fullK
ey
))
{
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
()));
}
}
apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/StringHelper.java
View file @
27ab485b
...
...
@@ -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
);
...
...
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/ObjectTypeResolver.java
View file @
27ab485b
...
...
@@ -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
);
}
...
...
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/PrimitiveTypeResolver.java
View file @
27ab485b
...
...
@@ -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
());
}
}
}
apidoc-core/src/main/java/com/kim/apidoc/core/resolver/TypeResolvers.java
View file @
27ab485b
...
...
@@ -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
);
}
...
...
apidoc-core/src/main/java/com/kim/apidoc/core/schema/RequestParameterType.java
0 → 100644
View file @
27ab485b
package
com.kim.apidoc.core.schema
;
/**
* 请求参数枚举类型
*
* @author duanledexianxianxian
* @date 2020 /3/29 0:37
* @since 1.0.0
*/
public
enum
RequestParameterType
{
/**
* 查询参数.
*/
QUERY
,
/**
* 路径参数.
*/
PATH
,
/**
* 请求参数体.
*/
BODY
}
apidoc-core/src/main/java/com/kim/apidoc/core/schema/Row.java
View file @
27ab485b
...
...
@@ -40,6 +40,12 @@ public class Row {
*/
String
remark
;
/**
* 请求参数类型
*/
String
requestParameterType
;
/**
* Instantiates a new Row.
*
...
...
apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java
View file @
27ab485b
...
...
@@ -44,6 +44,7 @@ public class Section extends Node {
* The Query parameter.
*/
boolean
queryParameter
=
true
;
/**
* The Request rows.
*/
...
...
apidoc-core/src/main/resources/templates/markdown.ftl
View file @
27ab485b
...
...
@@ -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.requ
estParameterType!''} |${rowValue.requ
ired?string('true','false')} | ${rowValue.condition!''} | ${rowValue.def!''} | ${rowValue.remark!''} |
</#list>
</#if>
<#-- 响应-->
...
...
apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java
View file @
27ab485b
...
...
@@ -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
()
));
}
/**
...
...
apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/User.java
View file @
27ab485b
...
...
@@ -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
...
...
apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java
View file @
27ab485b
...
...
@@ -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
);
}
...
...
apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java
View file @
27ab485b
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment