Search code examples
scalaplayframeworkswagger

Body missing in scala play post from swagger


I have a controller

import models.UserRegistration
import javax.inject._
import play.api.mvc._
import com.example.WritableImplicits._

@Singleton
class UserController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {

  def postExample = Action { implicit request: Request[AnyContent] =>
    val userRegistration = UserRegistration.form.bindFromRequest().get
    Ok(userRegistration)
  }

  def getExample() = Action { implicit request: Request[AnyContent] =>
    val user1 : UserRegistration = new UserRegistration("ian", "[email protected]", "123")
    val user2 : UserRegistration = new UserRegistration("asdfasdf", "[email protected]", "1233123")
    val list: List[UserRegistration] = List(user1, user2)
    Ok(list)
  }
}

and a routes entry

###
#   summary: post a user
#   parameters:
#       - in: body
#         name: name
#         schema:
#           type: string
#         required: true
#         description: post a user in json format eg {name:"foo", email:"sdfsd", hash:"13123"}
#   tags:
#       - users
#   responses:
#       '200':
#         description: OK
#         content:
#            application/json:
#              schema:
#                type: string
###
+nocsrf
POST     /api/users                  controllers.UserController.postExample()

when posting in swagger like so enter image description here I get a npe, debugging shows that my body is empty

enter image description here

looking at the post from swagger it appears the payload is missing

enter image description here

possibly there is an issue with the swagger docs above the routes entry? but i am lost


Solution

  • I did indeed have the swaggerdoc wrong, rather than parameters it should have been

    #   requestBody:
    #     content:
    #        application/json:
    #          schema:
    #            type: object
    #          examples:
    #            Basic:
    #              value:
    #                name:
    #                   "123"
    #                email:
    #                   "9780987654321"
    #                hash:
    #                   "9780987654321"