4

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)?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • it's very pre-mature tool (to genrate code *once*!) ... with [*many issues*](https://github.com/swagger-api/swagger-codegen/issues) (`required` not working, "bean validation" not working, spring boot:2)... what do you expect?? (since you have to implement the endpoints anyway, that should be a peanut!) ..annotating the (generated) field with `@javax.validation.constraints.NotNull` will lead (with the rest of generated code) to an (http) 406 ..at least (whereas fixing the generated code with `@JsonProperty(required=true)` doesn't change much) – xerx593 Mar 29 '23 at 20:24
  • Not my choice to use it smart guy so I have to ride the horse I was given. – Dave Mar 31 '23 at 12:54

1 Answers1

0

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

    ...

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • You're suggesting to no longer submit an object to my POST request and instead have all paramters included in the query? That is not an option if I'm understanding you correctly. – Dave Mar 29 '23 at 15:26