Dave
Dave

Reputation: 19150

How do I configure swagger codegen (maven plugin) to return a bad request if one of my enum fields doesn't match?

I'm using the swagger-codegen-maven-plugin 3.0.20 with Java 10. I have these config options

                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <java8>false</java8>                        
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>.</sourceFolder>
                    <throwsException>true</throwsException>
                    <useTags>true</useTags>
                </configOptions>

I have this in my OpenAPI 3.0 schema

    post:
      tags:
        - accommodation
      summary: add new
      operationId: addAccommodation
      parameters:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/accommodationDTO'
        required: true
      responses:
    ...
  schemas:
    accommodationDTO:
      type: object
      required:
        - category
        - masterCustomerSetId
        - accommodationNm       
        - effectiveBeginDt 
      description: accommodation object
      properties:
        category:
          required: true
          type: string
          enum:
            - FARM         
            - APARTMENT
            - HOUSE

However, when I submit a request with this DTO JSON ...

 {
    ...
        "category": "TRASH CAN",
    ...
    }

the "category" field in my DTO is just getting converted to a null when passed to the controller and I would prefer a bad request to come back from my controller. What is the proper way to configure Swagger so that this happens (if it is possible at all)?

Upvotes: 4

Views: 457

Answers (1)

Nilanka Manoj
Nilanka Manoj

Reputation: 3728

use parameters:

post:
  tags:
    - accommodation
  summary: add new
  operationId: addAccommodation
  parameters:
    - in: query
      name: category
      description: The category of the accommodation.
      schema:
        type: string
        enum:
          - FARM         
          - APARTMENT
          - HOUSE
      required: true

    ...

Upvotes: 0

Related Questions