Is there a way using swagger to auto generate the error responses for APIs developed using SAP CAP

73 views Asked by At

I have included cds-swagger-ui-express within my application to auto-generate the openAPI documentation as well as host it from the server itself. I was looking for a way to auto-generate the error response documentation by swagger itself. Below is my code with the existing document that is generated

// srv/todo-service.js

const cds = require("@sap/cds");
const uuid = require("uuid");

module.exports = cds.service.impl(async function () {
  const { Todos } = this.entities;

  this.on("GET", Todos, async (req) => {
    try {
      if (req?._queryOptions?.err) {
        throw new Error("something happened !");
      }
      return todos;
    } catch (e) {
      throw {
        status: 403,
        message: e.message,
        originalError: e,
      };
    }
  });
});

// srv/todo-service.cds

namespace my.todoapp;

using my.todoapp as todos from './todo-service';

entity Todos {
  key id   : String;
  title    : String;
  completed: Boolean;
}

service ToDoService {
  entity Todos as projection on todos.Todos;
}
// srv/server.js
const cds = require("@sap/cds");
module.exports = cds.server;

if (process.env.NODE_ENV !== "production") {
  const cds_swagger = require("cds-swagger-ui-express");
  cds.on("bootstrap", (app) =>
    app.use(
      cds_swagger({
        basePath: "/$api-docs", // the root path to mount the middleware on
        diagram: true, // whether to render the YUML diagram
      })
    )
  );
}

And below is the openAPI response doc generated by swagger openAPI response doc

The error response structure that is documented here does not match with the error response that is returned by the API controller.

0

There are 0 answers