Skip to content

Commit f25b776

Browse files
map management
1 parent 531a78a commit f25b776

File tree

15 files changed

+459
-48
lines changed

15 files changed

+459
-48
lines changed

jsondoc-core/src/main/java/org/jsondoc/core/annotation/ApiMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* An array of strings representing media types produced by the method, like application/json, application/xml, ...
4242
* @return
4343
*/
44-
public String[] produces();
44+
public String[] produces() default {};
4545

4646
/**
4747
* An array of strings representing media types consumed by the method, like application/json, application/xml, ...

jsondoc-core/src/main/java/org/jsondoc/core/pojo/ApiBodyObjectDoc.java

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.lang.reflect.ParameterizedType;
66
import java.lang.reflect.Type;
77
import java.util.Collection;
8+
import java.util.Map;
89
import java.util.UUID;
910

1011
import org.jsondoc.core.annotation.ApiBodyObject;
@@ -14,54 +15,73 @@ public class ApiBodyObjectDoc {
1415
public String jsondocId = UUID.randomUUID().toString();
1516
private String object;
1617
private String multiple;
18+
private String mapKeyObject;
19+
private String mapValueObject;
20+
private String map;
1721

1822
public static ApiBodyObjectDoc buildFromAnnotation(Method method) {
1923
boolean multiple = false;
2024
Integer index = getApiBodyObjectIndex(method);
21-
if(index != -1) {
25+
if (index != -1) {
2226
Class<?> parameter = method.getParameterTypes()[index];
2327
multiple = JSONDocUtils.isMultiple(parameter);
24-
return new ApiBodyObjectDoc(getBodyObject(method, index), String.valueOf(multiple));
28+
return new ApiBodyObjectDoc(getBodyObject(method, index)[0], getBodyObject(method, index)[1], getBodyObject(method, index)[2], String.valueOf(multiple), getBodyObject(method, index)[3]);
2529
}
2630
return null;
2731
}
28-
32+
2933
private static Integer getApiBodyObjectIndex(Method method) {
3034
Annotation[][] parametersAnnotations = method.getParameterAnnotations();
31-
for(int i=0; i<parametersAnnotations.length; i++) {
32-
for(int j=0; j<parametersAnnotations[i].length; j++) {
33-
if(parametersAnnotations[i][j] instanceof ApiBodyObject) {
35+
for (int i = 0; i < parametersAnnotations.length; i++) {
36+
for (int j = 0; j < parametersAnnotations[i].length; j++) {
37+
if (parametersAnnotations[i][j] instanceof ApiBodyObject) {
3438
return i;
3539
}
3640
}
3741
}
3842
return -1;
3943
}
40-
41-
private static String getBodyObject(Method method, Integer index) {
44+
45+
private static String[] getBodyObject(Method method, Integer index) {
4246
Class<?> parameter = method.getParameterTypes()[index];
4347
Type generic = method.getGenericParameterTypes()[index];
44-
if(Collection.class.isAssignableFrom(parameter)) {
45-
if(generic instanceof ParameterizedType) {
48+
if (Map.class.isAssignableFrom(parameter)) {
49+
Class<?> mapKeyClazz = null;
50+
Class<?> mapValueClazz = null;
51+
52+
if (generic instanceof ParameterizedType) {
53+
ParameterizedType parameterizedType = (ParameterizedType) generic;
54+
Type mapKeyType = parameterizedType.getActualTypeArguments()[0];
55+
Type mapValueType = parameterizedType.getActualTypeArguments()[1];
56+
mapKeyClazz = (Class<?>) mapKeyType;
57+
mapValueClazz = (Class<?>) mapValueType;
58+
}
59+
return new String[]{JSONDocUtils.getObjectNameFromAnnotatedClass(parameter), (mapKeyClazz != null) ? mapKeyClazz.getSimpleName().toLowerCase() : null, (mapValueClazz != null) ? mapValueClazz.getSimpleName().toLowerCase() : null, "map"};
60+
61+
} else if (Collection.class.isAssignableFrom(parameter)) {
62+
if (generic instanceof ParameterizedType) {
4663
ParameterizedType parameterizedType = (ParameterizedType) generic;
4764
Type type = parameterizedType.getActualTypeArguments()[0];
4865
Class<?> clazz = (Class<?>) type;
49-
return JSONDocUtils.getObjectNameFromAnnotatedClass(clazz);
66+
return new String[]{JSONDocUtils.getObjectNameFromAnnotatedClass(clazz), null, null, null};
5067
} else {
51-
return JSONDocUtils.UNDEFINED;
68+
return new String[]{JSONDocUtils.UNDEFINED, null, null, null};
5269
}
53-
} else if(method.getReturnType().isArray()) {
70+
} else if (method.getReturnType().isArray()) {
5471
Class<?> classArr = parameter;
55-
return JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType());
56-
72+
return new String[]{JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType()), null, null, null};
73+
5774
}
58-
return JSONDocUtils.getObjectNameFromAnnotatedClass(parameter);
75+
return new String[]{JSONDocUtils.getObjectNameFromAnnotatedClass(parameter), null, null, null};
5976
}
60-
61-
public ApiBodyObjectDoc(String object, String multiple) {
77+
78+
public ApiBodyObjectDoc(String object, String mapKeyObject, String mapValueObject, String multiple, String map) {
6279
super();
6380
this.object = object;
6481
this.multiple = multiple;
82+
this.mapKeyObject = mapKeyObject;
83+
this.mapValueObject = mapValueObject;
84+
this.map = map;
6585
}
6686

6787
public String getObject() {
@@ -72,4 +92,16 @@ public String getMultiple() {
7292
return multiple;
7393
}
7494

95+
public String getMapKeyObject() {
96+
return mapKeyObject;
97+
}
98+
99+
public String getMapValueObject() {
100+
return mapValueObject;
101+
}
102+
103+
public String getMap() {
104+
return map;
105+
}
106+
75107
}

jsondoc-core/src/main/java/org/jsondoc/core/pojo/ApiObjectFieldDoc.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.reflect.ParameterizedType;
55
import java.lang.reflect.Type;
66
import java.util.Collection;
7+
import java.util.Map;
78
import java.util.UUID;
89

910
import org.jsondoc.core.annotation.ApiObjectField;
@@ -17,34 +18,79 @@ public class ApiObjectFieldDoc {
1718
private String description;
1819
private String format;
1920
private String[] allowedvalues;
21+
private String mapKeyObject;
22+
private String mapValueObject;
23+
private String map;
2024

2125
public static ApiObjectFieldDoc buildFromAnnotation(ApiObjectField annotation, Field field) {
2226
ApiObjectFieldDoc apiPojoFieldDoc = new ApiObjectFieldDoc();
2327
apiPojoFieldDoc.setName(field.getName());
2428
apiPojoFieldDoc.setDescription(annotation.description());
25-
apiPojoFieldDoc.setType(getFieldObject(field));
29+
String[] typeChecks = getFieldObject(field);
30+
apiPojoFieldDoc.setType(typeChecks[0]);
2631
apiPojoFieldDoc.setMultiple(String.valueOf(JSONDocUtils.isMultiple(field.getType())));
2732
apiPojoFieldDoc.setFormat(annotation.format());
2833
apiPojoFieldDoc.setAllowedvalues(annotation.allowedvalues());
34+
apiPojoFieldDoc.setMapKeyObject(typeChecks[1]);
35+
apiPojoFieldDoc.setMapValueObject(typeChecks[2]);
36+
apiPojoFieldDoc.setMap(typeChecks[3]);
2937
return apiPojoFieldDoc;
3038
}
3139

32-
public static String getFieldObject(Field field) {
33-
if (Collection.class.isAssignableFrom(field.getType())) {
40+
public static String[] getFieldObject(Field field) {
41+
if (Map.class.isAssignableFrom(field.getType())) {
42+
Class<?> mapKeyClazz = null;
43+
Class<?> mapValueClazz = null;
44+
45+
if (field.getGenericType() instanceof ParameterizedType) {
46+
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
47+
Type mapKeyType = parameterizedType.getActualTypeArguments()[0];
48+
Type mapValueType = parameterizedType.getActualTypeArguments()[1];
49+
mapKeyClazz = (Class<?>) mapKeyType;
50+
mapValueClazz = (Class<?>) mapValueType;
51+
}
52+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(field.getType()), (mapKeyClazz != null) ? mapKeyClazz.getSimpleName().toLowerCase() : null, (mapValueClazz != null) ? mapValueClazz.getSimpleName().toLowerCase() : null, "map" };
53+
54+
} else if (Collection.class.isAssignableFrom(field.getType())) {
3455
if (field.getGenericType() instanceof ParameterizedType) {
3556
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
3657
Type type = parameterizedType.getActualTypeArguments()[0];
3758
Class<?> clazz = (Class<?>) type;
38-
return JSONDocUtils.getObjectNameFromAnnotatedClass(clazz);
59+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(clazz), null, null, null };
3960
} else {
40-
return JSONDocUtils.UNDEFINED;
61+
return new String[] { JSONDocUtils.UNDEFINED, null, null, null };
4162
}
63+
4264
} else if (field.getType().isArray()) {
4365
Class<?> classArr = field.getType();
44-
return JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType());
66+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType()), null, null, null };
4567

4668
}
47-
return JSONDocUtils.getObjectNameFromAnnotatedClass(field.getType());
69+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(field.getType()), null, null, null };
70+
}
71+
72+
public String getMapKeyObject() {
73+
return mapKeyObject;
74+
}
75+
76+
public void setMapKeyObject(String mapKeyObject) {
77+
this.mapKeyObject = mapKeyObject;
78+
}
79+
80+
public String getMapValueObject() {
81+
return mapValueObject;
82+
}
83+
84+
public void setMapValueObject(String mapValueObject) {
85+
this.mapValueObject = mapValueObject;
86+
}
87+
88+
public String getMap() {
89+
return map;
90+
}
91+
92+
public void setMap(String map) {
93+
this.map = map;
4894
}
4995

5096
public String[] getAllowedvalues() {

jsondoc-core/src/main/java/org/jsondoc/core/pojo/ApiResponseObjectDoc.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.reflect.ParameterizedType;
55
import java.lang.reflect.Type;
66
import java.util.Collection;
7+
import java.util.Map;
78
import java.util.UUID;
89

910
import org.jsondoc.core.annotation.ApiResponseObject;
@@ -13,33 +14,53 @@ public class ApiResponseObjectDoc {
1314
public String jsondocId = UUID.randomUUID().toString();
1415
private String object;
1516
private String multiple;
17+
private String mapKeyObject;
18+
private String mapValueObject;
19+
private String map;
1620

1721
public static ApiResponseObjectDoc buildFromAnnotation(ApiResponseObject annotation, Method method) {
18-
return new ApiResponseObjectDoc(getReturnObject(method), String.valueOf(JSONDocUtils.isMultiple(method)));
22+
return new ApiResponseObjectDoc(getReturnObject(method)[0], getReturnObject(method)[1], getReturnObject(method)[2], String.valueOf(JSONDocUtils.isMultiple(method)), getReturnObject(method)[3]);
1923
}
20-
21-
public static String getReturnObject(Method method) {
22-
if(Collection.class.isAssignableFrom(method.getReturnType())) {
23-
if(method.getGenericReturnType() instanceof ParameterizedType) {
24+
25+
public static String[] getReturnObject(Method method) {
26+
if (Map.class.isAssignableFrom(method.getReturnType())) {
27+
Class<?> mapKeyClazz = null;
28+
Class<?> mapValueClazz = null;
29+
30+
if (method.getGenericReturnType() instanceof ParameterizedType) {
31+
ParameterizedType parameterizedType = (ParameterizedType) method.getGenericReturnType();
32+
Type mapKeyType = parameterizedType.getActualTypeArguments()[0];
33+
Type mapValueType = parameterizedType.getActualTypeArguments()[1];
34+
mapKeyClazz = (Class<?>) mapKeyType;
35+
mapValueClazz = (Class<?>) mapValueType;
36+
}
37+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(method.getReturnType()), (mapKeyClazz != null) ? mapKeyClazz.getSimpleName().toLowerCase() : null, (mapValueClazz != null) ? mapValueClazz.getSimpleName().toLowerCase() : null, "map" };
38+
39+
} else if (Collection.class.isAssignableFrom(method.getReturnType())) {
40+
if (method.getGenericReturnType() instanceof ParameterizedType) {
2441
ParameterizedType parameterizedType = (ParameterizedType) method.getGenericReturnType();
2542
Type type = parameterizedType.getActualTypeArguments()[0];
2643
Class<?> clazz = (Class<?>) type;
27-
return JSONDocUtils.getObjectNameFromAnnotatedClass(clazz);
44+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(clazz), null, null, null };
2845
} else {
29-
return JSONDocUtils.UNDEFINED;
46+
return new String[] { JSONDocUtils.UNDEFINED, null, null, null };
3047
}
31-
} else if(method.getReturnType().isArray()) {
48+
} else if (method.getReturnType().isArray()) {
3249
Class<?> classArr = method.getReturnType();
33-
return JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType());
34-
50+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(classArr.getComponentType()), null, null, null };
51+
3552
}
36-
return JSONDocUtils.getObjectNameFromAnnotatedClass(method.getReturnType());
53+
54+
return new String[] { JSONDocUtils.getObjectNameFromAnnotatedClass(method.getReturnType()), null, null, null };
3755
}
38-
39-
public ApiResponseObjectDoc(String object, String multiple) {
56+
57+
public ApiResponseObjectDoc(String object, String mapKeyObject, String mapValueObject, String multiple, String map) {
4058
super();
4159
this.object = object;
4260
this.multiple = multiple;
61+
this.mapKeyObject = mapKeyObject;
62+
this.mapValueObject = mapValueObject;
63+
this.map = map;
4364
}
4465

4566
public String getObject() {
@@ -50,4 +71,16 @@ public String getMultiple() {
5071
return multiple;
5172
}
5273

74+
public String getMapKeyObject() {
75+
return mapKeyObject;
76+
}
77+
78+
public String getMapValueObject() {
79+
return mapValueObject;
80+
}
81+
82+
public String getMap() {
83+
return map;
84+
}
85+
5386
}

jsondoc-core/src/test/java/org/jsondoc/core/Child.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jsondoc.core;
22

3+
import java.util.Map;
4+
35
import org.jsondoc.core.annotation.ApiObject;
46
import org.jsondoc.core.annotation.ApiObjectField;
57

@@ -11,5 +13,8 @@ public class Child extends Parent {
1113

1214
@ApiObjectField(description="the test games")
1315
private Long[] games;
16+
17+
@ApiObjectField(description="the scores for each game")
18+
private Map<String, Integer> scores;
1419

1520
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.jsondoc.core;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
public class MethodSignatures extends TypeCheckerTest {
8+
9+
// public void a(List aList) {}
10+
//
11+
// public void b(List<String> aList) {}
12+
//
13+
// public String c() {return null;}
14+
//
15+
// public List d() {return null;}
16+
//
17+
// public List<String> e() {return null;}
18+
//
19+
// public List<Parent> f() {return null;}
20+
//
21+
// public String[] g() {return null;}
22+
//
23+
// public Parent[] h() {return null;}
24+
//
25+
// public int j(List aList) {return 0;}
26+
//
27+
// public boolean k(List aList) {return true;}
28+
//
29+
// public long i(List aList) {return 0L;}
30+
//
31+
// public Set l(List aList) {return null;}
32+
//
33+
// public Long[] m() {return null;}
34+
//
35+
// public Map n() {return null;}
36+
//
37+
// public MethodSignatures o() {return null;}
38+
//
39+
// public Map<String, Integer> p() {return null;}
40+
41+
public Map<Parent, Child> q() {return null;}
42+
43+
}

0 commit comments

Comments
 (0)