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.
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"