1

How do I define a required requestBody in swagger (openapi 3.0) ?

The following generates the request object but the validation methods are trivial and not requiring that the request's properties to be set:

/data/{idParam}:
    put:
      tags:
        - Data
      parameters:
        - $ref: "./commonDTO.yaml#/components/parameters/dataIdParam"
      summary: My rquest
      description: |-
        Allows operator to import data.

      operationId: importData
      requestBody:
        $ref: "#/components/requestBodies/ImportDataDTO"


requestBodies:
  ImportDataDTO:
    required: true
    content:
      application/json:
        schema:
          properties:
            name:
              type: string
            dataId:
              $ref: "./commonDTO.yaml#/components/schemas/dataID"
            requestHeader:
              $ref: "./commonDTO.yaml#/components/schemas/RequestHeaderDTO"
            dataDTO:
              $ref: "#/components/schemas/MyDataObj" 

What I get out is this:


bool ImportData_request::validate(std::stringstream& msg, const std::string& pathPrefix) const
{
    bool success = true;
    const std::string _pathPrefix = pathPrefix.empty() ? "ImportData_request" : pathPrefix;

                    
    return success;
}

Instead I should check that the id, name, dataDTO, etc are all set.

I'm using the cpp-pistache-server generator.

ventsyv
  • 3,316
  • 3
  • 27
  • 49

1 Answers1

2

required: true on the request body level requires the presence of any request body. For example, an empty object {} will be valid.

To require specific properties within the body, you need to specify the list of required fields within the schema. For example, to require the name, dataId, and dataDTO properties, add the following lines to the schema:

requestBodies:
  ImportDataDTO:
    required: true
    content:
      application/json:
        schema:
          type: object
          required:        # <--------
            - name         # <--------
            - dataId       # <--------
            - dataDTO      # <--------
          properties:
            name:
              type: string
            dataId:
              $ref: "./commonDTO.yaml#/components/schemas/dataID"
            requestHeader:
              $ref: "./commonDTO.yaml#/components/schemas/RequestHeaderDTO"
            dataDTO:
              $ref: "#/components/schemas/MyDataObj"
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Yeah, so I thought. I made the properties required, and those in turn have other object with required properties. After a couple of levels it gets down to primitives which are also required. But when I gun the generator, the validate function is still unchanged. I don't understand it. Maybe it's a bug in the generator ... – ventsyv Jun 06 '23 at 03:03
  • 2
    Seems to be a codegen bug indeed. There's a [similar bug report](https://github.com/OpenAPITools/openapi-generator/issues/5880) about required query parameters. – Helen Jun 06 '23 at 07:27