Skip to content

Commit 0adb22e

Browse files
committed
Merge pull request #547 from giovannibotta/patch-2
Add support for bean property annotation #546
2 parents 2d4af9e + 9c8284d commit 0adb22e

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

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

+50-12
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,60 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
266266
}
267267

268268
def getAllParamsFromFields(cls: Class[_]): List[Parameter] = {
269-
(for(field <- getAllFields(cls)) yield {
270-
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
271-
if(field.getAnnotation(classOf[QueryParam]) != null || field.getAnnotation(classOf[HeaderParam]) != null ||
272-
field.getAnnotation(classOf[HeaderParam]) != null || field.getAnnotation(classOf[PathParam]) != null ||
273-
field.getAnnotation(classOf[ApiParam]) != null) {
269+
// find getter/setters
270+
(cls.getDeclaredMethods map {
271+
method =>
272+
if (method.getAnnotation(classOf[QueryParam]) != null ||
273+
method.getAnnotation(classOf[HeaderParam]) != null ||
274+
method.getAnnotation(classOf[PathParam]) != null ||
275+
method.getAnnotation(classOf[ApiParam]) != null) {
276+
createParameterFromGetterOrSetter(method).toList
277+
} else Nil
278+
}).flatten.toList ++
279+
(for (field <- getAllFields(cls)) yield {
280+
// only process fields with @ApiParam, @QueryParam, @HeaderParam, @PathParam
281+
if (field.getAnnotation(classOf[QueryParam]) != null ||
282+
field.getAnnotation(classOf[HeaderParam]) != null ||
283+
field.getAnnotation(classOf[PathParam]) != null ||
284+
field.getAnnotation(classOf[ApiParam]) != null) {
285+
val param = new MutableParameter
286+
param.dataType = processDataType(field.getType, field.getGenericType)
287+
Option(field.getAnnotation(classOf[ApiParam])) match {
288+
case Some(annotation) => toAllowableValues(annotation.allowableValues)
289+
case _ =>
290+
}
291+
val annotations = field.getAnnotations
292+
processParamAnnotations(param, annotations)
293+
}
294+
else Nil
295+
}).flatten.toList
296+
}
297+
298+
private val getterPattern = """get(.+)""".r
299+
private val setterPattern = """set(.+)""".r
300+
301+
private def createParameterFromGetterOrSetter(method: Method): List[Parameter] = {
302+
(method.getName match {
303+
case getterPattern(propertyName) =>
274304
val param = new MutableParameter
275-
param.dataType = processDataType(field.getType, field.getGenericType)
276-
Option (field.getAnnotation(classOf[ApiParam])) match {
305+
// TODO: not sure this will work
306+
param.dataType = processDataType(method.getReturnType, method.getGenericReturnType)
307+
Some(param)
308+
case setterPattern(propertyName) =>
309+
val param = new MutableParameter
310+
// TODO: not sure this will work
311+
param.dataType = processDataType(method.getParameterTypes()(0), method.getGenericParameterTypes()(0))
312+
Some(param)
313+
case _ => None
314+
}).toList.map {
315+
param =>
316+
Option(method.getAnnotation(classOf[ApiParam])) match {
277317
case Some(annotation) => toAllowableValues(annotation.allowableValues)
278318
case _ =>
279319
}
280-
val annotations = field.getAnnotations
320+
val annotations = method.getAnnotations
281321
processParamAnnotations(param, annotations)
282-
}
283-
else List.empty
284-
}).flatten.toList
322+
}.flatten
285323
}
286324

287325
def pathFromMethod(method: Method): String = {
@@ -374,4 +412,4 @@ class MutableParameter(param: Parameter) {
374412
paramType,
375413
paramAccess)
376414
}
377-
}
415+
}

0 commit comments

Comments
 (0)