|
16 | 16 |
|
17 | 17 | package io.swagger.jaxrs; |
18 | 18 |
|
19 | | -import com.fasterxml.jackson.databind.JavaType; |
20 | | -import com.fasterxml.jackson.databind.type.TypeFactory; |
21 | | -import com.google.common.collect.Collections2; |
22 | | - |
23 | 19 | import io.swagger.annotations.Api; |
24 | 20 | import io.swagger.annotations.ApiImplicitParam; |
25 | 21 | import io.swagger.annotations.ApiImplicitParams; |
26 | 22 | import io.swagger.annotations.ApiOperation; |
27 | | -import io.swagger.annotations.ApiParam; |
28 | 23 | import io.swagger.annotations.ApiResponse; |
29 | 24 | import io.swagger.annotations.ApiResponses; |
30 | 25 | import io.swagger.annotations.Authorization; |
|
40 | 35 | import io.swagger.jaxrs.config.ReaderListener; |
41 | 36 | import io.swagger.jaxrs.ext.SwaggerExtension; |
42 | 37 | import io.swagger.jaxrs.ext.SwaggerExtensions; |
| 38 | +import io.swagger.jaxrs.utils.ReaderUtils; |
43 | 39 | import io.swagger.jaxrs.utils.ReflectionUtils; |
44 | 40 | import io.swagger.models.Contact; |
45 | 41 | import io.swagger.models.ExternalDocs; |
|
63 | 59 | import io.swagger.models.properties.Property; |
64 | 60 | import io.swagger.models.properties.RefProperty; |
65 | 61 |
|
| 62 | +import com.fasterxml.jackson.databind.JavaType; |
| 63 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
66 | 64 | import org.apache.commons.lang3.StringUtils; |
67 | 65 | import org.slf4j.Logger; |
68 | 66 | import org.slf4j.LoggerFactory; |
69 | 67 |
|
70 | | -import javax.ws.rs.Consumes; |
71 | | -import javax.ws.rs.HeaderParam; |
72 | | -import javax.ws.rs.HttpMethod; |
73 | | -import javax.ws.rs.PathParam; |
74 | | -import javax.ws.rs.Produces; |
75 | | -import javax.ws.rs.QueryParam; |
76 | | - |
77 | 68 | import java.lang.annotation.Annotation; |
78 | | -import java.lang.reflect.Constructor; |
79 | | -import java.lang.reflect.Field; |
80 | 69 | import java.lang.reflect.Method; |
81 | 70 | import java.lang.reflect.ParameterizedType; |
82 | 71 | import java.lang.reflect.Type; |
83 | 72 | import java.util.ArrayList; |
84 | 73 | import java.util.Arrays; |
85 | | -import java.util.Collection; |
86 | 74 | import java.util.Collections; |
87 | 75 | import java.util.EnumSet; |
88 | 76 | import java.util.HashMap; |
|
93 | 81 | import java.util.Map; |
94 | 82 | import java.util.Set; |
95 | 83 |
|
| 84 | +import javax.ws.rs.Consumes; |
| 85 | +import javax.ws.rs.HttpMethod; |
| 86 | +import javax.ws.rs.Produces; |
| 87 | + |
96 | 88 | public class Reader { |
97 | 89 | private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class); |
98 | 90 | private static final String SUCCESSFUL_OPERATION = "successful operation"; |
99 | 91 | private static final String PATH_DELIMITER = "/"; |
100 | 92 |
|
101 | | - private static final Set<Class<? extends Annotation>> FIELD_ANNOTATIONS; |
102 | 93 | private final ReaderConfig config; |
103 | 94 | private Swagger swagger; |
104 | 95 |
|
@@ -242,6 +233,14 @@ protected Swagger read(Class<?> cls, String parentPath, String parentMethod, boo |
242 | 233 |
|
243 | 234 | // handle sub-resources by looking at return type |
244 | 235 |
|
| 236 | + final List<Parameter> globalParameters = new ArrayList<Parameter>(); |
| 237 | + |
| 238 | + // look for constructor-level annotated properties |
| 239 | + globalParameters.addAll(ReaderUtils.collectConstructorParameters(cls, swagger)); |
| 240 | + |
| 241 | + // look for field-level annotated properties |
| 242 | + globalParameters.addAll(ReaderUtils.collectFieldParameters(cls, swagger)); |
| 243 | + |
245 | 244 | // parse the method |
246 | 245 | final javax.ws.rs.Path apiPath = cls.getAnnotation(javax.ws.rs.Path.class); |
247 | 246 | Method methods[] = cls.getMethods(); |
@@ -288,7 +287,7 @@ protected Swagger read(Class<?> cls, String parentPath, String parentMethod, boo |
288 | 287 | final ApiOperation apiOperation = getAnnotation(method, ApiOperation.class); |
289 | 288 | String httpMethod = extractOperationMethod(apiOperation, method, SwaggerExtensions.chain()); |
290 | 289 |
|
291 | | - Operation operation = parseMethod(method, collectGlobalParameters(cls)); |
| 290 | + Operation operation = parseMethod(method, globalParameters); |
292 | 291 | if (operation == null) { |
293 | 292 | continue; |
294 | 293 | } |
@@ -978,30 +977,7 @@ private boolean isIgnored(String path) { |
978 | 977 | return !javax.ws.rs.core.Response.class.isAssignableFrom(cls) && !isResourceClass(cls); |
979 | 978 | } |
980 | 979 |
|
981 | | - private List<Parameter> collectGlobalParameters(Class<?> cls) { |
982 | | - final List<Parameter> globalParameters = new ArrayList<Parameter>(); |
983 | | - |
984 | | - // look for constructor-level annotated properties |
985 | | - final Constructor<?> constructor = ReflectionUtils.findConstructor(cls); |
986 | | - if (constructor != null) { |
987 | | - final Type[] genericParameterTypes = constructor.getGenericParameterTypes(); |
988 | | - final Annotation[][] annotations = constructor.getParameterAnnotations(); |
989 | | - for (int i = 0; i < genericParameterTypes.length; i++) { |
990 | | - globalParameters.addAll(getParameters(genericParameterTypes[i], Arrays.asList(annotations[i]))); |
991 | | - } |
992 | | - } |
993 | | - |
994 | | - // look for field-level annotated properties |
995 | | - for (Field field : cls.getDeclaredFields()) { |
996 | | - final List<Annotation> annotations = Arrays.asList(field.getAnnotations()); |
997 | | - final Collection<Class<? extends Annotation>> types = Collections2.transform(annotations, ReflectionUtils.createAnnotationTypeGetter()); |
998 | | - if (!Collections.disjoint(types, FIELD_ANNOTATIONS)) { |
999 | | - globalParameters.addAll(getParameters(field.getGenericType(), annotations)); |
1000 | | - } |
1001 | | - } |
1002 | | - |
1003 | | - return globalParameters; |
1004 | | - } private static boolean isResourceClass(Class<?> cls) { |
| 980 | + private static boolean isResourceClass(Class<?> cls) { |
1005 | 981 | return cls.getAnnotation(Api.class) != null; |
1006 | 982 | } |
1007 | 983 |
|
@@ -1063,14 +1039,4 @@ public Property wrap(String container, Property property) { |
1063 | 1039 |
|
1064 | 1040 | protected abstract Property doWrap(Property property); |
1065 | 1041 | } |
1066 | | - |
1067 | | - static { |
1068 | | - final Set<Class<? extends Annotation>> fieldAnnotations = new HashSet<Class<? extends Annotation>>(); |
1069 | | - fieldAnnotations.add(PathParam.class); |
1070 | | - fieldAnnotations.add(QueryParam.class); |
1071 | | - fieldAnnotations.add(HeaderParam.class); |
1072 | | - fieldAnnotations.add(ApiParam.class); |
1073 | | - fieldAnnotations.add(ApiImplicitParam.class); |
1074 | | - FIELD_ANNOTATIONS = Collections.unmodifiableSet(fieldAnnotations); |
1075 | | - } |
1076 | 1042 | } |
0 commit comments