swagger-akka-http/swagger-scala-module

Case class as request body parameter must be required

chameleon82 opened this issue · 4 comments

Next code will produce optional body

case class Pet(petId: Int, petName: String);

@POST
def createPet(pet: Pet)

as openapi result:

openapi: 3.0.1
paths:
 /pets:
    post:
      operationId: createPet
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'

but OpenApi said body is optional and not required by default ( https://swagger.io/docs/specification/describing-request-body/ )

Expected behavior is to have parameter required because it is defined as mandatory parameter:

      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true       <---

thanks @chameleon82, could you do a PR?

@chameleon82 most of the OpenAPI generation is done using io.swagger.v3.jaxrs2.Reader which is in a different lib from this - it might be that the fix needs to go there - but have a look yourself

@chameleon82 this is not even the right lib for this issue - maybe https://github.com/swagger-api/swagger-core (for swagger-jaxrs2) or https://github.com/swagger-akka-http/swagger-akka-http but not this - this lib is for generating the models not the request body definitions.

When you add swagger annotations, you could explicity set required = true.

 @Operation(summary = "Add integers", description = "Add integers",
    requestBody = new RequestBody(required = true, content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionRequest])))),
    responses = Array(
      new ApiResponse(responseCode = "200", description = "Add response",
        content = Array(new Content(schema = new Schema(implementation = classOf[AddOptionResponse])))),
      new ApiResponse(responseCode = "500", description = "Internal server error"))
  )

@chameleon82 I updated https://github.com/pjfanning/swagger-akka-http-sample to set the required=true in the Operation annotations