在Swagger-Node

时间:2015-10-22 09:50:29

标签: swagger swagger-node-express

我是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)

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

你有什么想法吗?

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:2)

我有同样的问题。我刚刚将响应模式类型更改为string。 从某种意义上说,所有的http响应都是字符串,你需要解析它以转换为你在应用程序中使用的类型。

这是我的招摇规格。

responses:
'200':
  description: a string that represents a boolean value
  schema:
    type: string

答案 2 :(得分: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
 }