Skip to content

Commit

Permalink
Add support for Jersey2 BeanParam.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shu Zhang committed Apr 16, 2014
1 parent d32b697 commit 5213b4f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
val GenericTypeMapper = "([a-zA-Z\\.]*)<([a-zA-Z0-9\\.\\,\\s]*)>".r

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

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

Some(param.asParameter)
List(param.asParameter)
}
}).flatten.toList

Expand Down Expand Up @@ -260,24 +260,7 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
case _ => None
}
// look for method-level annotated properties
val parentParams: List[Parameter] = (for(field <- getAllFields(cls))
yield {
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
if(field.getAnnotation(classOf[QueryParam]) != null || field.getAnnotation(classOf[HeaderParam]) != null ||
field.getAnnotation(classOf[HeaderParam]) != null || field.getAnnotation(classOf[PathParam]) != null ||
field.getAnnotation(classOf[ApiParam]) != null) {
val param = new MutableParameter
param.dataType = field.getType.getName
Option (field.getAnnotation(classOf[ApiParam])) match {
case Some(annotation) => toAllowableValues(annotation.allowableValues)
case _ =>
}
val annotations = field.getAnnotations
processParamAnnotations(param, annotations)
}
else None
}
).flatten.toList
val parentParams: List[Parameter] = getAllParamsFromFields(cls)

for(method <- cls.getMethods) {
val returnType = findSubresourceType(method)
Expand Down Expand Up @@ -345,6 +328,25 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
}
return fields;
}

def getAllParamsFromFields(cls: Class[_]): List[Parameter] = {
(for(field <- getAllFields(cls)) yield {
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
if(field.getAnnotation(classOf[QueryParam]) != null || field.getAnnotation(classOf[HeaderParam]) != null ||
field.getAnnotation(classOf[HeaderParam]) != null || field.getAnnotation(classOf[PathParam]) != null ||
field.getAnnotation(classOf[ApiParam]) != null) {
val param = new MutableParameter
param.dataType = field.getType.getName
Option (field.getAnnotation(classOf[ApiParam])) match {
case Some(annotation) => toAllowableValues(annotation.allowableValues)
case _ =>
}
val annotations = field.getAnnotations
processParamAnnotations(param, annotations)
}
else List.empty
}).flatten.toList
}

def pathFromMethod(method: Method): String = {
val path = method.getAnnotation(classOf[javax.ws.rs.Path])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.ws.rs.core.Context

class DefaultJaxrsApiReader extends JaxrsApiReader {
// decorates a Parameter based on annotations, returns None if param should be ignored
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
var shouldIgnore = false
for (pa <- paramAnnotations) {
pa match {
Expand Down Expand Up @@ -55,9 +55,9 @@ class DefaultJaxrsApiReader extends JaxrsApiReader {
mutable.paramType = TYPE_BODY
mutable.name = TYPE_BODY
}
Some(mutable.asParameter)
List(mutable.asParameter)
}
else None
else List.empty
}

def findSubresourceType(method: Method): Class[_] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import com.sun.jersey.api.core.InjectParam
class JerseyApiReader extends JaxrsApiReader {
private val LOGGER = LoggerFactory.getLogger(classOf[JerseyApiReader])

def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
var shouldIgnore = false
for (pa <- paramAnnotations) {
pa match {
Expand Down Expand Up @@ -98,9 +98,9 @@ class JerseyApiReader extends JaxrsApiReader {
mutable.paramType = TYPE_BODY
mutable.name = TYPE_BODY
}
Some(mutable.asParameter)
List(mutable.asParameter)
}
else None
else List()
}

def findSubresourceType(method: Method): Class[_] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import org.glassfish.jersey.media.multipart.FormDataParam
class JerseyApiReader extends JaxrsApiReader {
private val LOGGER = LoggerFactory.getLogger(classOf[JerseyApiReader])

def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): Option[Parameter] = {
def processParamAnnotations(mutable: MutableParameter, paramAnnotations: Array[Annotation]): List[Parameter] = {
var shouldIgnore = false
for (pa <- paramAnnotations) {
pa match {
Expand Down Expand Up @@ -84,6 +84,10 @@ class JerseyApiReader extends JaxrsApiReader {
}
}
}
case e: BeanParam => {
val cls = SwaggerContext.loadClass(mutable.dataType)
return getAllParamsFromFields(cls.getRawClass)

This comment has been minimized.

Copy link
@shuz

shuz Apr 16, 2014

Owner

For the public swagger branch, change this line to:

return getAllParamsFromFields(cls)
}
case e: Context => shouldIgnore = true
case _ =>
}
Expand All @@ -93,9 +97,9 @@ class JerseyApiReader extends JaxrsApiReader {
mutable.paramType = TYPE_BODY
mutable.name = TYPE_BODY
}
Some(mutable.asParameter)
List(mutable.asParameter)
}
else None
else List.empty
}
def findSubresourceType(method: Method): Class[_] = {
method.getGenericReturnType match {
Expand Down

0 comments on commit 5213b4f

Please sign in to comment.