我是swagger-node的新手,我正在创建一个返回boolean类型数据的API方法。该方法的yaml为:
/IsBooting:
get:
summary: "Returns if the device is booting"
description: "Returns true when is in booting state"
x-swagger-router-controller: printer_status
operationId: IsBooting
responses:
200:
description: "Returns a bool that indicates if the deviceis booting"
schema:
type: "boolean"
default:
description: "Unexpected error"
schema:
$ref: "#/definitions/Error"
此API方法调用的控制器中的方法为:
function IsBooting(req, res) {
res.json(false)
}
当我使用PostMan调用此方法时,一些验证失败,并显示以下消息:
Error: Response validation failed: not a valid boolean: false
at throwErrorWithCode (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:121:13)
at validateTypeAndFormat (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:536:7)
at Object.module.exports.validateSchemaConstraints (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:627:7)
at validateValue (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\middleware\swagger-validator.js:117:16)
at ServerResponse.res.end (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\middleware\swagger-validator.js:242:9)
at ServerResponse.send (C:\...\node_modules\express\lib\response.js:204:10)
at ServerResponse.json (C:\...\node_modules\express\lib\response.js:249:15)
at IsBooting (C:\...\api\controllers\printer_status.js:54:7)
at swaggerRouter (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\middleware\swagger-router.js:407:20)
at swagger_router (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\fittings\swagger_router.js:31:5)
我不知道如何做出包含数据的响应。如果我使用字符串作为类型,或者如果我创建了一个新的复杂类型来封装布尔值,我可以发送它,但我认为没有很好的解决方案……
你有什么想法吗?
发布于 2017-12-05 21:35:00
swagger的布尔值为DataType:https://swagger.io/docs/specification/data-models/data-types/
最正确的方法是为答案创建一个模式定义:
responses:
"200":
description: Success
schema:
type: "#/definitions/TrueReponsenDefinition"
针对这种情况,给出适当的定义:
TrueReponsenDefinition:
title: A bool that indicates if the deviceis booting
type: boolean
properties:
booted:
type: boolean
来自节点的响应将是:
const data = {
booted: true,
};
return res.status(httpStatus.OK).json(data);
请注意,最合适的方法是封装数据。在本例中,我们使用"booted“
答案的主体将是:
{
"booted": true
}
发布于 2015-11-14 00:15:09
我也有同样的问题。我刚刚将响应模式类型更改为字符串。从某种意义上说,所有http响应都将是字符串,您需要对其进行解析,以将其转换为在应用程序中使用的类型。
这是我的招摇规格。
responses:
'200':
description: a string that represents a boolean value
schema:
type: string
发布于 2021-01-22 01:20:00
Swagger 3.0.0中的
responses:
'200':
description: Success
content:
application/json:
schema:
type: object
items:
$ref: "#/components/schemas/TrueReponsenDefinition"
和组件/模式定义将是:
components:
schemas:
TrueReponsenDefinition:
title: A bool that indicates if the app notification is ON/OFF
type: object
properties:
appNotificationOn:
type: boolean
响应将是JSON对象:
{
"appNotificationOn":boolean
}
https://stackoverflow.com/questions/33278130
复制相似问题