Skip to content

Commit 1c4061d

Browse files
committed
全部测试通过
1 parent c10ff77 commit 1c4061d

File tree

7 files changed

+81
-73
lines changed

7 files changed

+81
-73
lines changed

apidoc-core/src/main/java/com/ztianzeng/apidoc/ModelConverters.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
import com.ztianzeng.apidoc.converter.ModelConverterContextImpl;
77
import com.ztianzeng.apidoc.converter.ResolvedSchema;
88
import com.ztianzeng.apidoc.models.media.Schema;
9-
import com.ztianzeng.apidoc.utils.Json;
109
import lombok.extern.slf4j.Slf4j;
1110

12-
import java.lang.reflect.Type;
1311
import java.util.*;
1412
import java.util.concurrent.CopyOnWriteArrayList;
1513

@@ -32,7 +30,7 @@ public static ModelConverters getInstance() {
3230
public ModelConverters() {
3331
SourceBuilder sourceBuilder = new SourceBuilder();
3432
converters = new CopyOnWriteArrayList<>();
35-
converters.add(new ModelResolver(Json.mapper(), sourceBuilder));
33+
converters.add(new ModelResolver(sourceBuilder));
3634
}
3735

3836

apidoc-core/src/main/java/com/ztianzeng/apidoc/ModelResolver.java

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ztianzeng.apidoc;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.thoughtworks.qdox.JavaProjectBuilder;
54
import com.thoughtworks.qdox.model.JavaClass;
65
import com.thoughtworks.qdox.model.JavaField;
@@ -31,12 +30,10 @@
3130
*/
3231
@Slf4j
3332
public class ModelResolver implements ModelConverter {
34-
private SourceBuilder sourceBuilder;
3533
private JavaProjectBuilder builder;
3634

3735

38-
public ModelResolver(ObjectMapper mapper, SourceBuilder sourceBuilder) {
39-
this.sourceBuilder = sourceBuilder;
36+
public ModelResolver(SourceBuilder sourceBuilder) {
4037
builder = sourceBuilder.getBuilder();
4138
}
4239

@@ -48,6 +45,7 @@ public Schema resolve(AnnotatedType annotatedType,
4845
return null;
4946
}
5047

48+
5149
// 分析目标类信息
5250
JavaClass targetClass = annotatedType.getJavaClass();
5351

@@ -67,7 +65,9 @@ public Schema resolve(AnnotatedType annotatedType,
6765
}
6866
}
6967

70-
68+
if (targetClass instanceof DefaultJavaParameterizedType) {
69+
setRefType(targetClass, schema, annotatedType, targetClass, context);
70+
}
7171
// 转换成OpenApi定义的字段信息
7272
PrimitiveType parentType = PrimitiveType.fromType(targetClass.getBinaryName());
7373
schema.setType(Optional.ofNullable(parentType).orElse(PrimitiveType.OBJECT).getCommonName());
@@ -93,11 +93,11 @@ public Schema resolve(AnnotatedType annotatedType,
9393

9494
if (targetClass.isA(Map.class.getName())) {
9595
// 泛型信息
96-
List<JavaType> actualTypeArguments = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
97-
if (actualTypeArguments.isEmpty()) {
96+
List<JavaType> tar = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
97+
if (tar.isEmpty()) {
9898
return null;
9999
}
100-
JavaType javaType = actualTypeArguments.get(1);
100+
JavaType javaType = tar.get(1);
101101

102102
Schema addPropertiesSchema = context.resolve(
103103
new AnnotatedType()
@@ -127,11 +127,11 @@ public Schema resolve(AnnotatedType annotatedType,
127127
} else if (targetClass.isA(Collection.class.getName())
128128
|| targetClass.isA(List.class.getName())) {
129129
// 泛型信息
130-
List<JavaType> actualTypeArguments = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
131-
if (actualTypeArguments.isEmpty()) {
130+
List<JavaType> tar = ((DefaultJavaParameterizedType) targetClass).getActualTypeArguments();
131+
if (tar.isEmpty()) {
132132
return null;
133133
}
134-
JavaType javaType = actualTypeArguments.get(0);
134+
JavaType javaType = tar.get(0);
135135
// 处理集合
136136
Schema items = context.resolve(new AnnotatedType()
137137
.javaClass(builder.getClassByName(javaType.getFullyQualifiedName()))
@@ -147,64 +147,17 @@ public Schema resolve(AnnotatedType annotatedType,
147147
}
148148
schema = new ArraySchema().items(items);
149149
}
150-
// // 如果是集合类型,将类型向上抛出继续处理
151-
// if (targetType.isContainerType()) {
152-
// // 处理Map那种两种都有的
153-
// if (keyType != null && valueType != null) {
154-
// Schema addPropertiesSchema = context.resolve(
155-
// new AnnotatedType()
156-
// .type(valueType)
157-
// .schemaProperty(annotatedType.isSchemaProperty())
158-
// .skipSchemaName(true)
159-
// .resolveAsRef(annotatedType.isResolveAsRef())
160-
// .jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
161-
// .propertyName(annotatedType.getPropertyName())
162-
// .parent(annotatedType.getParent())
163-
// );
164-
//
165-
// String pName = null;
166-
//
167-
// if (addPropertiesSchema != null) {
168-
// if (StringUtils.isNotBlank(addPropertiesSchema.getName())) {
169-
// pName = addPropertiesSchema.getName();
170-
// }
171-
// if ("object".equals(addPropertiesSchema.getType()) && pName != null) {
172-
// // create a reference for the items
173-
// if (context.getDefinedModels().containsKey(pName)) {
174-
// addPropertiesSchema = new Schema().$ref(constructRef(pName));
175-
// }
176-
// } else if (addPropertiesSchema.get$ref() != null) {
177-
// addPropertiesSchema = new Schema().$ref(StringUtils.isNotEmpty(addPropertiesSchema.get$ref()) ? addPropertiesSchema.get$ref() : addPropertiesSchema.getName());
178-
// }
179-
// }
180-
// schema = new MapSchema().additionalProperties(addPropertiesSchema);
181-
// } else if (valueType != null) {
182-
// // 处理Array
183-
// Schema items = context.resolve(new AnnotatedType()
184-
// .type(valueType)
185-
// .schemaProperty(annotatedType.isSchemaProperty())
186-
// .skipSchemaName(true)
187-
// .resolveAsRef(annotatedType.isResolveAsRef())
188-
// .propertyName(annotatedType.getPropertyName())
189-
// .jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
190-
// .parent(annotatedType.getParent()));
191-
//
192-
// if (items == null) {
193-
// return null;
194-
// }
195-
// schema = new ArraySchema().items(items);
196-
//
197-
// }
198-
// }
199150

200151

201152
for (JavaField field : fields) {
202153
if (DocUtils.isPrimitive(field.getName())) {
203154
continue;
204155
}
156+
JavaClass type = field.getType();
157+
setRefType(type, schema, annotatedType, targetClass, context);
205158

206159
AnnotatedType aType = new AnnotatedType()
207-
.javaClass(field.getType())
160+
.javaClass(type)
208161
.parent(schema)
209162
.resolveAsRef(annotatedType.isResolveAsRef())
210163
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
@@ -250,5 +203,31 @@ public Schema resolve(AnnotatedType annotatedType,
250203
return schema;
251204
}
252205

206+
/**
207+
* 设置泛型
208+
*
209+
* @param type
210+
* @param schema
211+
* @param annotatedType
212+
* @param targetClass
213+
* @param context
214+
*/
215+
public void setRefType(JavaClass type, Schema schema, AnnotatedType annotatedType, JavaClass targetClass, ModelConverterContext context) {
216+
List<JavaType> ref = ((DefaultJavaParameterizedType) type).getActualTypeArguments();
217+
if (!ref.isEmpty()) {
218+
for (JavaType actualTypeArgument : ref) {
219+
type = builder.getClassByName(actualTypeArgument.getBinaryName());
220+
AnnotatedType aType = new AnnotatedType()
221+
.javaClass(type)
222+
.parent(schema)
223+
.resolveAsRef(annotatedType.isResolveAsRef())
224+
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
225+
.skipSchemaName(true)
226+
.schemaProperty(true)
227+
.propertyName(targetClass.getName());
228+
schema = context.resolve(aType);
229+
}
230+
}
231+
}
253232

254233
}

apidoc-core/src/main/java/com/ztianzeng/apidoc/models/media/Schema.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.math.BigDecimal;
2323
import java.util.*;
24+
import java.util.stream.Collectors;
2425

2526
/**
2627
* Schema
@@ -432,6 +433,9 @@ public Schema minProperties(Integer minProperties) {
432433
**/
433434

434435
public List<String> getRequired() {
436+
if (required != null) {
437+
required = required.stream().distinct().collect(Collectors.toList());
438+
}
435439
return required;
436440
}
437441

@@ -460,7 +464,7 @@ public Schema required(List<String> required) {
460464

461465
public Schema addRequiredItem(String requiredItem) {
462466
if (this.required == null) {
463-
this.required = new ArrayList<String>();
467+
this.required = new ArrayList<>();
464468
}
465469
this.required.add(requiredItem);
466470
Collections.sort(required);

apidoc-core/src/test/java/com/ztianzeng/apidoc/test/ContainerTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.ztianzeng.apidoc.converter.ModelConverterContextImpl;
88
import com.ztianzeng.apidoc.models.media.ArraySchema;
99
import com.ztianzeng.apidoc.models.media.Schema;
10+
import com.ztianzeng.apidoc.test.res.CreateParam;
1011
import com.ztianzeng.apidoc.test.res.InnerType;
12+
import com.ztianzeng.apidoc.test.res.Result;
1113
import org.junit.Test;
1214

1315
import java.util.Calendar;
@@ -21,7 +23,7 @@ public class ContainerTest extends TestBase {
2123

2224
@Test
2325
public void testArray() throws Exception {
24-
final ModelResolver modelResolver = new ModelResolver(mapper(), sourceBuilder);
26+
final ModelResolver modelResolver = new ModelResolver(sourceBuilder);
2527

2628
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
2729
JavaClass classByName = TestBase.builder.getClassByName(ArrayBean.class.getName());
@@ -42,7 +44,7 @@ public void testArray() throws Exception {
4244

4345
@Test
4446
public void testMap() throws Exception {
45-
final ModelResolver modelResolver = new ModelResolver(mapper(), sourceBuilder);
47+
final ModelResolver modelResolver = new ModelResolver(sourceBuilder);
4648
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
4749
JavaClass classByName = TestBase.builder.getClassByName(MapBean.class.getName());
4850

@@ -62,7 +64,7 @@ public void testMap() throws Exception {
6264

6365
@Test
6466
public void testComplexMap() throws Exception {
65-
ModelResolver resolver = new ModelResolver(mapper(), sourceBuilder);
67+
ModelResolver resolver = new ModelResolver(sourceBuilder);
6668

6769
final ModelConverterContextImpl context = new ModelConverterContextImpl(resolver);
6870
JavaClass classByName = TestBase.builder.getClassByName(WrapperType.class.getName());
@@ -84,6 +86,28 @@ public void testComplexMap() throws Exception {
8486
assertEquals(((Schema) wrapperType.getProperties().get("innerType")).getType(), "object");
8587
}
8688

89+
90+
@Test
91+
public void testResult() {
92+
ModelResolver resolver = new ModelResolver(sourceBuilder);
93+
94+
final ModelConverterContextImpl context = new ModelConverterContextImpl(resolver);
95+
JavaClass classByName = TestBase.builder.getClassByName(ResultBean.class.getName());
96+
context.resolve(new AnnotatedType(classByName));
97+
98+
final Map<String, Schema> models = context.getDefinedModels();
99+
final Schema createParam = models.get("CreateParam");
100+
assertNotNull(createParam);
101+
final Map<String, Schema> innerProps = createParam.getProperties();
102+
assertEquals(innerProps.size(), 2);
103+
final Schema foo = innerProps.get("username");
104+
assertEquals(foo.getType(), "string");
105+
final Schema name = innerProps.get("mobile");
106+
assertEquals(name.getType(), "string");
107+
108+
109+
}
110+
87111
static class ArrayBean {
88112
public int[] a;
89113
}
@@ -95,4 +119,9 @@ static class MapBean {
95119
static class WrapperType {
96120
public Map<String, InnerType> innerType;
97121
}
122+
123+
static class ResultBean {
124+
public Result<CreateParam> result;
125+
}
126+
98127
}

apidoc-core/src/test/java/com/ztianzeng/apidoc/test/ReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void test2497() {
181181
assertNotNull(operation);
182182
ArraySchema schema = (ArraySchema) operation.getResponses().get("200").getContent().values().iterator().next().getSchema();
183183
assertNotNull(schema);
184-
assertEquals(schema.getItems().get$ref(), "#/components/schemas/com.ztianzeng.apidoc.test.res.ResponseContentWithArrayResource.User");
184+
assertEquals(schema.getItems().get$ref(), "#/components/schemas/User");
185185
}
186186

187187
}

apidoc-core/src/test/java/com/ztianzeng/apidoc/test/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static ObjectMapper mapper() {
2525
}
2626

2727
protected ModelResolver modelResolver() {
28-
return new ModelResolver(new ObjectMapper(), new SourceBuilder());
28+
return new ModelResolver(new SourceBuilder());
2929
}
3030

3131
protected void prettyPrint(Object o) {

apidoc-core/src/test/java/com/ztianzeng/apidoc/test/res/TestController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@RequestMapping("/test")
1717
@RestController
1818
public class TestController {
19-
//
19+
2020

2121
/**
2222
* 新增一个实例
@@ -65,7 +65,5 @@ public Result<CreateParam> get2(@RequestParam(value = "userId", required = false
6565
return new Result<>();
6666
}
6767

68-
private void a() {
6968

70-
}
7169
}

0 commit comments

Comments
 (0)