Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error returning boolean in Swagger-Node

I'm new to swagger-node and I am creating an API method that returns data of type boolean. The yaml of the method is:

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

The method in the controller that this API method call is:

function IsBooting(req, res) {
   res.json(false)
}

When I call this method using PostMan, some validations fails with this message:

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)

I don't know how to make a response that contains the data. I cand send it if I use a string as type, or if I create a new complex type that encapsulates the boolean, but I think that there are not very good solutions...

Do you have any idea?

like image 256
Ruben Aguilar Avatar asked Oct 22 '15 09:10

Ruben Aguilar


1 Answers

The swagger has DataType for boolean: https://swagger.io/docs/specification/data-models/data-types/

The most correct would be to create a schema definition for the answer:

    responses:
      "200":
        description: Success
        schema:
          type: "#/definitions/TrueReponsenDefinition"

Make the proper definition, for this case:

      TrueReponsenDefinition:
        title: A bool that indicates if the deviceis booting
        type: boolean
        properties:
          booted:
            type: boolean

Response from the node, would be:

  const data = {
    booted: true,
  };
  return res.status(httpStatus.OK).json(data);

Note that the most appropriate would be to encapsulate the data. In this case, we use "booted"

The body of the answer will be:

{
  "booted": true
}
like image 129
SebastiaoRealino Avatar answered Sep 28 '22 06:09

SebastiaoRealino