Skip to content

Commit 649ba55

Browse files
committed
Merge pull request #749 from tpetr/unwrap-resource-optionals2
properly unwrap Optionals in Jersey resources
2 parents 2299893 + 3b57b13 commit 649ba55

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ trait JaxrsApiReader extends ClassReader with ClassReaderUtils {
5959
case Some(e) => e._2.qualifiedType
6060
case None => qt
6161
}
62-
"%s[%s]".format(normalizeContainer(container), b)
62+
if (container.endsWith(".Optional") || container.equals("scala.Option")) {
63+
b
64+
} else {
65+
"%s[%s]".format(normalizeContainer(container), b)
66+
}
6367
}
6468
case _ => paramType.getName
6569
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import com.wordnik.swagger.config.SwaggerConfig
2+
import com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader
3+
import org.junit.runner.RunWith
4+
import org.scalatest.{Matchers, FlatSpec}
5+
import org.scalatest.junit.JUnitRunner
6+
import testresources.ResourceWithOptionals
7+
8+
@RunWith(classOf[JUnitRunner])
9+
class ResourceWithOptionalsTest extends FlatSpec with Matchers {
10+
it should "read an api and extract an error model" in {
11+
val reader = new DefaultJaxrsApiReader
12+
val config = new SwaggerConfig()
13+
val apiResource = reader.read("/api-docs", classOf[ResourceWithOptionals], config).getOrElse(fail("should not be None"))
14+
15+
apiResource.apis.size should be (1)
16+
17+
val api = apiResource.apis.filter(_.path == "/optional/test").head
18+
val ops = api.operations
19+
ops.size should be (1)
20+
21+
ops.head.parameters.filter(_.paramType == "query").head.dataType should be ("string")
22+
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testresources
2+
3+
import javax.ws.rs.core.Response
4+
import javax.ws.rs.{QueryParam, GET, Path}
5+
6+
import com.wordnik.swagger.annotations._
7+
8+
@Path("/optional")
9+
@Api(value = "/optional", description = "Resource with optional query params")
10+
class ResourceWithOptionals {
11+
@GET
12+
@Path("/test")
13+
@ApiOperation(value = "Test out optional query param",
14+
notes = "No details provided",
15+
position = 1)
16+
@ApiResponses(Array(
17+
new ApiResponse(code = 404, message = "object not found")))
18+
def getMaybeString(@ApiParam(value = "optional string input", required = false) @QueryParam("maybeString") maybeString: Option[String]) = {
19+
Response.ok.entity(maybeString).build
20+
}
21+
}

0 commit comments

Comments
 (0)