scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 229 forks source link

Schema not Updating #343

Closed necrolingus closed 1 year ago

necrolingus commented 1 year ago

Good day

I am not sure if this is a swagger-ui-express issue but my schema never updates. Even if I completely delete the schema from my code and restart the Node Express server, the schema still renders in the api-docs, albeit the old schema. If I add or modify any functions, for example, I add or remove a DELETE or PUT, that updates perfectly in the api-docs. Right now, I just start the server up with "node index.js" and I do not cache anything.

I am using swagger-ui-express@4.6.2 and Node.js v18.16.0

Here is my schema

/**
 * @swagger
 * components:
 *   schemas:
 *     Book:
 *       type: object
 *       required:
 *         - id
 *         - title
 *         - author
 *         - finished
 *       properties:
 *         id:
 *           type: string
 *           description: The auto-generated id of the book
 *         title:
 *           type: string
 *           description: The title of your book
 *         author:
 *           type: string
 *           description: The book author
 *         finished:
 *           type: string
 *           description: Whether you have finished reading the book
 *         createdAt:
 *           type: string
 *           format: date
 *           description: The date the book was added
 *       example:
 *         id: abc123
 *         title: The New Turing Omnibus
 *         author: Alexander K. Dewdney
 *         finished: yes
 *         createdAt: 2023-03-10T04:05:06.157Z
*/

And here is my index.js file:

const swaggerOptions = require("./config/swaggerSepcOptions")
const swaggerJsdoc = require("swagger-jsdoc")
const swaggerUi = require("swagger-ui-express")
const specs = swaggerJsdoc(swaggerOptions.options);

const express = require('express')
const app = express()

app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded({ extended: true })) // to support url-encoded bodies
app.use("/books", require("./routes/booksRoutes"));

const appConfig = require("./config/envValues");
const port = appConfig.nodePort;

app.use(
  "/api-docs",
  swaggerUi.serve,
  swaggerUi.setup(specs, { explorer: true })
);

app.listen(port, () => {
    console.log('Started up on port: ' + port)
})

Here is my full bookRoutes.js file:

const express = require("express");
const router = express.Router();
const bookController = require("../controllers/bookController");

/**
 * @swagger
 * components:
 *   schemas:
 *     Book:
 *       type: object
 *       required:
 *         - id
 *         - title
 *         - author
 *         - finished
 *       properties:
 *         id:
 *           type: string
 *           description: The auto-generated id of the book
 *         title:
 *           type: string
 *           description: The title of your book
 *         author:
 *           type: string
 *           description: The book author
 *         finished:
 *           type: string
 *           description: Whether you have finished reading the book
 *         createdAt:
 *           type: string
 *           format: date
 *           description: The date the book was added
 *       example:
 *         id: abc123
 *         title: The New Turing Omnibus
 *         author: Alexander K. Dewdney
 *         finished: yes
 *         createdAt: 2023-03-10T04:05:06.157Z
*/

/** 
 * @swagger
 * tags:
 *   name: Books
 *   description: The books managing API
 * /books:
 *   get:
 *     summary: Lists all the books
 *     tags: [Books]
 *     responses:
 *       200:
 *         description: The list of the books
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Book'
 *   post:
 *     summary: Create a new book
 *     tags: [Books]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Book'
 *     responses:
 *       200:
 *         description: The created book.
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Book'
 *       500:
 *         description: Some server error
 *   delete:
 *     summary: Remove the book by id
 *     tags: [Books]
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: string
 *         required: true
 *         description: The book id
 *
 *     responses:
 *       200:
 *         description: The book was deleted
 *       404:
 *         description: The book was not found
 * /books/{id}:
 *   get:
 *     summary: Get the book by id
 *     tags: [Books]
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: string
 *         required: true
 *         description: The book id
 *     responses:
 *       200:
 *         description: The book response by id
 *         contens:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Book'
 *       404:
 *         description: The book was not found
*/

router.get('/', bookController.selectAll)
router.get('/:id', bookController.selectOne)
router.post('/', bookController.insertOne)
router.delete('/:id', bookController.deleteOne)

module.exports = router;

If you need anything else please let me know.

Thank you

necrolingus commented 1 year ago

Forgot to add the example. Here is what my api-docs URL look like: image

And here is the schema: image

I just noticed even if I delete the schema from my code and restart my server, api-docs still render correctly and still shows the old schema.

I cleared browser cache and also used a separate browser to rule out browser caching, but in all instances the old schema still shows.

necrolingus commented 1 year ago

Found my issue. I backed up my original JS file and it caused the original schema to be re-loaded. Renaming/removing the extension sorted it out.

image