swagger-akka-http / swagger-scala-module

Swagger support for scala
Apache License 2.0
10 stars 10 forks source link

Case class as request body parameter must be required #118

Closed chameleon82 closed 3 years ago

chameleon82 commented 3 years ago

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       <---
pjfanning commented 3 years ago

thanks @chameleon82, could you do a PR?

pjfanning commented 3 years ago

@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

pjfanning commented 3 years ago

@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"))
  )
pjfanning commented 3 years ago

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