I am implementing Swagger into an ExpressJs API application. In the controller files, we are using nested folders and "const router = require('express').Router({ mergeParams: true });" to inherit path variables from parent folders. When I run my swagger implementation, anything deeper than the parent folder will not dynamically put in the route variables provided from the swagger UI. Example: I will put in the variables "mineID' and "incidentId" from the text boxes in swagger ui, then this is the exact curl request shown in swagger ui, where you can see that the second route parameter has not been filled as it should have been from the UI:
curl -X 'GET' \
'http://localhost:4900/v2/mines/5555/incidents/{incidentId}/actions' \
***** bearer token etc here
In my implementation of swagger, I am using jsdocs in a seperate file, but in the same folder as the controller code. My JsDocs definition is using the same parameter names as shown below:
/**
* @swagger
* /v2/mines/{mineId}/incidents/{incidentId}/actions:
* get:
* summary: Get all actions associated with a specific incident
* description: |
* Retrieves all actions related to a specified incident, including details about each action's outcomes.
*
* **Permissions Required**
* - `IncidentPermissions.VIEW_ACTIONS`
* tags:
* - Mines/Incidents/Actions
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: mineId
* required: true
* schema:
* type: string
* description: The unique identifier of the mine.
* - in: path
* name: incidentId
* required: true
* schema:
* type: string
* description: The unique identifier of the incident.
And this is my ExpressJs controller code with the route xxx/v2/mines/:mineId/incidents/:incidentId/actions. This route is fucntioning as pexected in postman and is in production, so I am confident it's not an issue with the controller:
router.get('/', async function (req, res, next) {
// @ts-ignore mineId is defined in *** file and brought in with "mergeParams"
const mineId = req.params.mineId;
// @ts-ignore incidentId is defined in *** file and brought in with "mergeParams"
const incidentId = req.params.incidentId;
// rest of controller code
});
How might I get these variables to populate correctly? It is working elsewhere in the application and I only need solutions for the swagger implementation, the API code is tested and working fine on its own. I have searched extensively and cannot find an answer.
There was a bug with path parameters in Swagger UI v. 5.17.7. It was fixed in v. 5.17.8.
Make sure to update the Swagger UI package (either swagger-ui
, swagger-ui-dist
, or swagger-ui-react
) in your dependencies to v. 5.17.8 or later.