英文:
How to make requestBodies required in swagger
问题
如何在Swagger(OpenAPI 3.0)中定义一个必需的requestBody?
以下生成请求对象,但验证方法很简单,不要求设置请求的属性:
/data/{idParam}:
put:
tags:
- Data
parameters:
- $ref: "./commonDTO.yaml#/components/parameters/dataIdParam"
summary: My rquest
description: |-
允许操作员导入数据。
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"
我得到的输出是这样的:
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;
}
相反,我应该检查id、name、dataDTO等是否都已设置。
我正在使用cpp-pistache-server生成器。
英文:
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.
答案1
得分: 2
required: true
在请求体级别要求请求体的任何内容都必须存在。例如,一个空对象 {}
将是有效的。
要求请求体中特定的属性,您需要在 schema
中指定所需字段的列表。例如,要求 name
、dataId
和 dataDTO
属性,添加以下行到 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"
英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论