Optimize middleware in express when using swagger-ui-express

Hi campers,
I am using swagger-ui-express to generate an API documentation. Below is how I have implemented the /documentation endpoint. I am dynamically mutating the JSON swaggerDocument as described in the doc under “Modify swagger file on the fly before load” section.

const swaggerDocument = require("./swagger.json");
const swaggerOptions = { explorer: true, customSiteTitle: "Quiz API" };

app.use("/documentation", async (req, res, next) => {
    // Fetch data from the database and mutate swaggerDocument here
    console.log("Middleware has been called");
    req.swaggerDoc = swaggerDocument; 
    next();
  }, swaggerUi.serve, swaggerUi.setup(undefined, swaggerOptions));

Every time I hit the /documentation endpoint, the middleware above is invoked 6 times. I am not sure if there is something I am doing wrong or that is how it is supposed to be. I just want it to be invoked once since I am reading from the DB. I know I can also read from the database once by doing something like:

let updatedSwaggerDoc = false;
app.use("/documentation", async (req, res, next) => {
    if(!updatedSwaggerDoc){
      // Fetch from database and update swaggerDoc
       req.swaggerDoc = swaggerDocument; 
       updatedSwaggerDoc = true;
    }
    next();
  }, swaggerUi.serve, swaggerUi.setup(undefined, swaggerOptions));

I am wondering why the middleware is invoked 6 times. I have failed to find an answer in the documentation.