Skip to content

Commit

Permalink
Merge pull request #537 from junbo/develop
Browse files Browse the repository at this point in the history
Add @BeanParam support for Jersey2.
  • Loading branch information
fehguy committed Apr 20, 2014
2 parents 33424a2 + ee1ed29 commit 436681a
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ object ModelConverters {
else converters += c
}

def removeConverter(c: ModelConverter) = {
converters -= c
}

def read(cls: Class[_], t: Map[String, String] = Map.empty): Option[Model] = {
val types = {
if(t.isEmpty)typeMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import org.scalatest.matchers.ShouldMatchers
class CustomConverterTest extends FlatSpec with ShouldMatchers {
it should "ignore properties with type Bar" in {
// add the custom converter
ModelConverters.addConverter(new CustomConverter, true)
val customConverter = new CustomConverter
ModelConverters.addConverter(customConverter, true)

// make sure the field bar: converter.Bar is not present
ModelConverters.read(classOf[Foo]) match {
case Some(model) => model.properties.get("bar") should be (None)
case _ => fail("didn't read anything")
}
ModelConverters.removeConverter(customConverter)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import scala.reflect.BeanProperty

@RunWith(classOf[JUnitRunner])
class JaveDateTimeOverride extends FlatSpec with ShouldMatchers {
ModelConverters.addConverter(new JavaDateTimeConverter, true)
val javaDateTimeConverter = new JavaDateTimeConverter
ModelConverters.addConverter(javaDateTimeConverter, true)
val models = ModelConverters.readAll(classOf[ModelWithDate])
JsonSerializer.asJson(models) should be ("""[{"id":"ModelWithDate","properties":{"dateValue":{"type":"integer","format":"int64"}}}]""")
// cleanup to avoid impacting other test cases with Date model members
ModelConverters.removeConverter(javaDateTimeConverter)
}

class JavaDateTimeConverter extends SwaggerSchemaConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import javax.xml.bind.annotation._
class SnakeCaseConverterTest extends FlatSpec with ShouldMatchers {
it should "ignore properties with type Bar" in {
// add the custom converter
ModelConverters.addConverter(new SnakeCaseConverter, true)
val snakeCaseConverter = new SnakeCaseConverter
ModelConverters.addConverter(snakeCaseConverter, true)

// make sure the field bar: converter.Bar is not present
ModelConverters.read(classOf[SnakeCaseModel]) match {
Expand All @@ -34,6 +35,9 @@ class SnakeCaseConverterTest extends FlatSpec with ShouldMatchers {
}
case _ => fail("didn't read anything")
}

// cleanup to avoid impacting other test cases with Date model members
ModelConverters.removeConverter(snakeCaseConverter)
}
}

Expand Down
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]

// Finds the type of the subresource this method produces, in case it's a subresource locator
// In case it's not a subresource locator the entity type is returned
Expand Down Expand Up @@ -167,7 +167,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 @@ -264,6 +264,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 = processDataType(field.getType, field.getGenericType)
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 @@ -69,24 +69,7 @@ class BasicJaxrsReader extends JaxrsApiReader {
else ((List(), List(), List(), 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 @@ -146,7 +129,7 @@ class BasicJaxrsReader 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 @@ -188,9 +171,9 @@ class BasicJaxrsReader 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 @@ -49,24 +49,7 @@ class DefaultJaxrsApiReader extends JaxrsApiReader {
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 @@ -128,7 +111,7 @@ 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 @@ -170,9 +153,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 @@ -89,7 +89,7 @@ class JavaPathParamTargetTest extends FlatSpec with ShouldMatchers {

// verify the 2nd api
val detailsOps = apis.filter(_.path == "/javaPathParamTest/{id}/details").head.operations
val detailOp = detailsOps.head
val detailOp = detailsOps.filter(_.method == "POST").head

detailOp.parameters.size should be (3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,7 @@ class JerseyApiReader extends JaxrsApiReader {
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 @@ -153,7 +136,7 @@ class JerseyApiReader extends JaxrsApiReader {
else None
}

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 @@ -211,9 +194,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 @@ -75,24 +75,7 @@ class JerseyApiReader extends JaxrsApiReader {
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 @@ -153,7 +136,7 @@ class JerseyApiReader extends JaxrsApiReader {
else None
}

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 @@ -198,6 +181,10 @@ class JerseyApiReader extends JaxrsApiReader {
}
}
}
case e: BeanParam => {
val cls = SwaggerContext.loadClass(mutable.dataType)
return getAllParamsFromFields(cls)
}
case e: Context => shouldIgnore = true
case _ =>
}
Expand All @@ -207,9 +194,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
44 changes: 44 additions & 0 deletions modules/swagger-jersey2-jaxrs/src/test/scala/BeanParamTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import com.wordnik.swagger.jersey.JerseyApiReader
import testresources._

import com.wordnik.swagger.jaxrs.reader._
import com.wordnik.swagger.model._
import com.wordnik.swagger.config._

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class BeanParamTest extends FlatSpec with ShouldMatchers {
it should "read beanparam parameters" in {
val reader = new JerseyApiReader
val config = new SwaggerConfig()
val apiResource = reader.read("/api-docs", classOf[BeanParamResource], config).getOrElse(fail("should not be None"))

val apis = apiResource.apis
apis.size should be (1)

val ops = apis.head.operations
ops.size should be (1)

val op = ops.head
op.parameters.size should be (3)

val param0 = op.parameters(0)
param0.name should be ("ids")
param0.dataType should be ("Set[string]")
param0.paramType should be ("query")

val param1 = op.parameters(1)
param1.name should be ("startDate")
param1.dataType should be ("Date")
param1.paramType should be ("query")

val param2 = op.parameters(2)
param2.name should be ("name")
param2.dataType should be ("string")
param2.paramType should be ("query")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testmodels

import com.wordnik.swagger.annotations.ApiParam
import javax.ws.rs.QueryParam
import java.util.Date

class BeanParamModel {
@ApiParam(value = "sample set param")
@QueryParam("ids")
var ids: Set[String] = _

@ApiParam(value = "sample date param")
@QueryParam("startDate")
var startDate: Date = _

@ApiParam(value = "sample string param")
@QueryParam("name")
var name: String = _
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package testresources

import javax.ws.rs._
import com.wordnik.swagger.annotations._
import testmodels._
import javax.ws.rs.core.Response

@Path("/beanParam")
@Api(value = "/beanParam", description = "Bean Param Resource")
class BeanParamResource {
@GET
@ApiOperation(value = "Search Object", notes = "No details provided", position = 0)
def searchObject(@BeanParam params: BeanParamModel) {
Response.ok.entity("ok").build
}
}

0 comments on commit 436681a

Please sign in to comment.