Search code examples
node.jsexpressswaggerswagger-ui

Swagger UI doesn't show responses


I have added Swagger and Swagger UI into my ExpressJS application. When I try to execute any of the requests, the UI won't show any responses.

When checking the network traffic using the browser's developer tool, the server seem to sent the correct Json-formatted response to the browser, but the Swagger UI won't seem to do anything to it. What am I missing?

Browser screenshot

On my main app.js file I have added Swagger like this:

const swaggerJSDoc    = require('swagger-jsdoc');
const swaggerUi       = require('swagger-ui-express');

/* SWAGGER ================================================================= */
const swaggerDefinition = {
  openapi: '3.0.0',
  info: {
    title: 'Express API',
    version: '1.0.0',
    description: "Bla bla",
    contact: {
      name: 'Website',
      url: 'https://www.somethingsomething.fi'
    }
  },
  servers: [
    {
      url: 'http://localhost:3000',
      description: 'Local development server'
    }
  ]
};

const options = {
  swaggerDefinition,
  // Paths to files containing OpenAPI definitions
  apis: ['./routes/api/v1/*.js'],
};

const swaggerSpec = swaggerJSDoc(options);

//add API doc route
app.use('/apidoc', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

This is how I have documented the end-point that is shown in the screenshot:

/**
 * @swagger
 * /v1/frameagreements:
 *  get:
 *    tags:
 *      - Frame Agreements
 *    summary: Retrieve a list of Frame Agreements.
 *    description: Retrieve a list of all Lease Objects. The sensitive information is removed from the results.
 */
 router.get("/", async (req, res, next) => {

...etc

Solution

  • Thanks @Helen for pointing out I was missing the responses. I didn't think they would be mandatory.

    I made the following edit to the end-point documentation and this solved the issue:

    /**
     * @swagger
     * /v1/frameagreements:
     *  get:
     *    tags:
     *      - Frame Agreements
     *    summary: Retrieve a list of Frame Agreements.
     *    description: Retrieve a list of all Lease Objects. The sensitive information is removed from the results.
     *    responses:
     *     '200':
     *        description: OK
     *        content:
     *          application/json:
     *            schema:
     *              type: object
     */