6

我是 swagger-node 的新手,我正在创建一个返回布尔类型数据的 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)

我不知道如何做出包含数据的响应。如果我使用字符串作为类型,或者如果我创建一个封装布尔值的新复杂类型,我可以发送它,但我认为没有很好的解决方案......

你有什么主意吗?

4

3 回答 3

4

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
}
于 2017-12-05T13:34:59.510 回答
3

我遇到过同样的问题。我只是将响应模式类型更改为字符串。从某种意义上说,所有 http 响应都是字符串,您需要对其进行解析以转换为您将在应用程序中使用的类型。

这是我的招摇规范。

responses:
'200':
  description: a string that represents a boolean value
  schema:
    type: string
于 2015-11-13T16:15:08.987 回答
1

在 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
 }
于 2021-01-21T17:19:59.743 回答