swagger-api / swagger-play

Apache License 2.0
330 stars 181 forks source link

No response header or body displays on 400 Bad Request #140

Closed vickumar1981 closed 7 years ago

vickumar1981 commented 7 years ago

We are using swagger-play version 1.5.3 with Play 2.5.

Below are the relevant dependencies in the build.sbt:

libraryDependencies ++= Seq(
...
  "io.swagger" %% "swagger-play2" % "1.5.3",
  "org.webjars" %% "webjars-play" % "2.5.0-4",
  "org.webjars" % "swagger-ui" % "2.2.0"
)

When I curl my endpoint to get a 400 error: `curl -vvv -X POST http://localhost:9000/users/login -H "X-Logon-Id: baduser" -H 'X-Password: badpassword'

I get this response:

< HTTP/1.1 400 Bad Request
< errorCode: -66
< errorDesc: INVALID DATABASE USER
< Content-Length: 66
< Content-Type: application/json
< Date: Wed, 17 May 2017 14:49:50 GMT
< 
* Connection #0 to host localhost left intact
{"returnStatus":-66,"returnStatusMessage":"INVALID DATABASE USER"}

However, when I try the same thing in the swagger ui, I see neither the response headers nor the response body.

Below is the code from the controller:

@ApiOperation(value = "User login")
  @ApiImplicitParams(Array(
    new ApiImplicitParam(name = logonHeader, dataType = "java.lang.String", required = true,
      paramType = "header", value = ""),
    new ApiImplicitParam(name = pwHeader, dataType = "java.lang.String", required = true,
      paramType = "header", value = "")))
  @ApiResponses(Array(
    new ApiResponse(code = 200, message = "Success", response = classOf[models.ApiResponse]),
    new ApiResponse(code = 400, message = "Error", response = classOf[models.ApiResponse])))
  def loginUser() = Action { request => {
      val response = loginDAO.loginUser(
        request.headers.get(logonHeader).getOrElse(""),
        request.headers.get(pwHeader).getOrElse(""))
      if (response.returnStatus == 0)
        Ok(Json.toJson(response))
      else
        Result(
        header = ResponseHeader(400,
          Map("errorCode" -> response.returnStatus.toString, "errorDesc" -> response.returnStatusMessage)),
        body = HttpEntity.Strict(ByteString(Json.toJson(response).toString()), Some("application/json"))
      )
    }

I used Result instead of BadRequest b/c I wanted to set additional header information and see if that was the issue.

I believe either I haven't done the annotations correctly, or this is possibly a bug in the swagger ui.

vickumar1981 commented 7 years ago
screen shot 2017-05-17 at 10 55 47 am
vickumar1981 commented 7 years ago

Sorry, turns out not to be an issue with play-swagger, but with swagger-ui newer versions.