Skip to content

Commit 5213b4f

Browse files
author
Shu Zhang
committed
Add support for Jersey2 BeanParam.
1 parent d32b697 commit 5213b4f

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

modules/swagger-jaxrs/src/main/scala/com/wordnik/swagger/jaxrs/JaxrsApiReader.scala

+22-20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
2424
val GenericTypeMapper = "([a-zA-Z\\.]*)<([a-zA-Z0-9\\.\\,\\s]*)>".r
2525

2626
// decorates a Parameter based on annotations, returns None if param should be ignored
27-
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter]
27+
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter]
2828

2929
// decorates an Operation
3030
def processOperation(endpoint: String, operation: Operation, method: Method, apiOperation: ApiOperation) : Operation = {
@@ -146,7 +146,7 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
146146
param.name = TYPE_BODY
147147
param.paramType = TYPE_BODY
148148

149-
Some(param.asParameter)
149+
List(param.asParameter)
150150
}
151151
}).flatten.toList
152152

@@ -260,24 +260,7 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
260260
case _ => None
261261
}
262262
// look for method-level annotated properties
263-
val parentParams: List[Parameter] = (for(field <- getAllFields(cls))
264-
yield {
265-
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
266-
if(field.getAnnotation(classOf[QueryParam]) != null || field.getAnnotation(classOf[HeaderParam]) != null ||
267-
field.getAnnotation(classOf[HeaderParam]) != null || field.getAnnotation(classOf[PathParam]) != null ||
268-
field.getAnnotation(classOf[ApiParam]) != null) {
269-
val param = new MutableParameter
270-
param.dataType = field.getType.getName
271-
Option (field.getAnnotation(classOf[ApiParam])) match {
272-
case Some(annotation) => toAllowableValues(annotation.allowableValues)
273-
case _ =>
274-
}
275-
val annotations = field.getAnnotations
276-
processParamAnnotations(param, annotations)
277-
}
278-
else None
279-
}
280-
).flatten.toList
263+
val parentParams: List[Parameter] = getAllParamsFromFields(cls)
281264

282265
for(method <- cls.getMethods) {
283266
val returnType = findSubresourceType(method)
@@ -345,6 +328,25 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
345328
}
346329
return fields;
347330
}
331+
332+
def getAllParamsFromFields(cls: Class[_]): List[Parameter] = {
333+
(for(field <- getAllFields(cls)) yield {
334+
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
335+
if(field.getAnnotation(classOf[QueryParam]) != null || field.getAnnotation(classOf[HeaderParam]) != null ||
336+
field.getAnnotation(classOf[HeaderParam]) != null || field.getAnnotation(classOf[PathParam]) != null ||
337+
field.getAnnotation(classOf[ApiParam]) != null) {
338+
val param = new MutableParameter
339+
param.dataType = field.getType.getName
340+
Option (field.getAnnotation(classOf[ApiParam])) match {
341+
case Some(annotation) => toAllowableValues(annotation.allowableValues)
342+
case _ =>
343+
}
344+
val annotations = field.getAnnotations
345+
processParamAnnotations(param, annotations)
346+
}
347+
else List.empty
348+
}).flatten.toList
349+
}
348350

349351
def pathFromMethod(method: Method): String = {
350352
val path = method.getAnnotation(classOf[javax.ws.rs.Path])

modules/swagger-jaxrs/src/main/scala/com/wordnik/swagger/jaxrs/reader/DefaultJaxrsReader.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import javax.ws.rs.core.Context
1313

1414
class DefaultJaxrsApiReader extends JaxrsApiReader {
1515
// decorates a Parameter based on annotations, returns None if param should be ignored
16-
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
16+
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
1717
var shouldIgnore = false
1818
for (pa <- paramAnnotations) {
1919
pa match {
@@ -55,9 +55,9 @@ class DefaultJaxrsApiReader extends JaxrsApiReader {
5555
mutable.paramType = TYPE_BODY
5656
mutable.name = TYPE_BODY
5757
}
58-
Some(mutable.asParameter)
58+
List(mutable.asParameter)
5959
}
60-
else None
60+
else List.empty
6161
}
6262

6363
def findSubresourceType(method: Method): Class[_] = {

modules/swagger-jersey-jaxrs/src/main/scala/com/wordnik/swagger/jersey/JerseyApiReader.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import com.sun.jersey.api.core.InjectParam
4040
class JerseyApiReader extends JaxrsApiReader {
4141
private val LOGGER = LoggerFactory.getLogger(classOf[JerseyApiReader])
4242

43-
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
43+
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
4444
var shouldIgnore = false
4545
for (pa <- paramAnnotations) {
4646
pa match {
@@ -98,9 +98,9 @@ class JerseyApiReader extends JaxrsApiReader {
9898
mutable.paramType = TYPE_BODY
9999
mutable.name = TYPE_BODY
100100
}
101-
Some(mutable.asParameter)
101+
List(mutable.asParameter)
102102
}
103-
else None
103+
else List()
104104
}
105105

106106
def findSubresourceType(method: Method): Class[_] = {

modules/swagger-jersey2-jaxrs/src/main/scala/com/wordnik/swagger/jersey/JerseyApiReader.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import org.glassfish.jersey.media.multipart.FormDataParam
3939
class JerseyApiReader extends JaxrsApiReader {
4040
private val LOGGER = LoggerFactory.getLogger(classOf[JerseyApiReader])
4141

42-
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
42+
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
4343
var shouldIgnore = false
4444
for (pa <- paramAnnotations) {
4545
pa match {
@@ -84,6 +84,10 @@ class JerseyApiReader extends JaxrsApiReader {
8484
}
8585
}
8686
}
87+
case e: BeanParam => {
88+
val cls = SwaggerContext.loadClass(mutable.dataType)
89+
return getAllParamsFromFields(cls.getRawClass)
90+
}
8791
case e: Context => shouldIgnore = true
8892
case _ =>
8993
}
@@ -93,9 +97,9 @@ class JerseyApiReader extends JaxrsApiReader {
9397
mutable.paramType = TYPE_BODY
9498
mutable.name = TYPE_BODY
9599
}
96-
Some(mutable.asParameter)
100+
List(mutable.asParameter)
97101
}
98-
else None
102+
else List.empty
99103
}
100104
def findSubresourceType(method: Method): Class[_] = {
101105
method.getGenericReturnType match {

0 commit comments

Comments
 (0)