fix(openapi): normalize path parameters to curly brace notation for Swagger compatibility#704
Conversation
…wagger compatibility Closes axe-api#703
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an incompatibility issue where dynamic path parameters in the generated Swagger documentation were not correctly recognized, leading to execution problems. By standardizing the path parameter format to the OpenAPI-compliant curly brace notation, the change ensures that all dynamic routes are properly interpreted and functional within the Swagger UI and related tools. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly normalizes path parameters from :param to the OpenAPI-compliant {param} format for standard endpoints. This is a good fix for ensuring compatibility with tools like Swagger UI. However, the same normalization is missing for custom routes, which could lead to them being represented incorrectly in the generated documentation. I've left a specific comment on how to address this to make the fix complete.
| for (const endpoint of docs.get()) { | ||
| if (paths[endpoint.url] === undefined) { | ||
| paths[endpoint.url] = {}; | ||
| const normalizePath = endpoint.url.replace(/:([^/]+)/g, "{$1}"); |
There was a problem hiding this comment.
This is a good fix for standard endpoints. To improve reusability and ensure all routes are handled consistently, I recommend extracting this normalization logic into a dedicated helper function.
Currently, the custom routes processed later in this function (lines 643-660) are not being normalized. This means that custom routes with parameters (e.g., /users/:id/action) will still have incorrect paths in the generated OpenAPI specification. Using a shared helper function for both standard and custom routes would solve this issue and make the code more maintainable.
| const normalizePath = endpoint.url.replace(/:([^/]+)/g, "{$1}"); | |
| const normalizePath = normalizeSwaggerPath(endpoint.url); |
There was a problem hiding this comment.
Pull request overview
This PR updates the Swagger/OpenAPI document generation so that dynamic route parameters are emitted in OpenAPI-compatible curly-brace syntax ({param}) instead of Express-style colon syntax (:param), fixing “try it out” execution for parameterized endpoints (Issue #703).
Changes:
- Added a
normalizeSwaggerPath()helper to convert:paramsegments to{param}. - Updated generated
pathsandmodelPatternskeys to use the normalized path for both standard and custom documented routes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| const normalizeSwaggerPath = (path: string) => { | ||
| return path.replace(/:([^/]+)/g, "{$1}"); | ||
| } |
There was a problem hiding this comment.
normalizeSwaggerPath is missing a terminating semicolon after the function expression, which is inconsistent with the surrounding const ... = (...) => { ... }; style in this file and can violate the project's lint/format rules. Add the semicolon after the closing brace.
| } | |
| }; |
| const normalizePath = normalizeSwaggerPath(endpoint.url); | ||
| if (paths[normalizePath] === undefined) { | ||
| paths[normalizePath] = {}; |
There was a problem hiding this comment.
The local variable name normalizePath is misleading (it reads like a function). Since it holds the normalized path string, consider renaming it to normalizedPath (and update its uses in this block) to make the code easier to follow.
| @@ -634,26 +638,27 @@ const generateDocumentation = async () => { | |||
| path.requestBody = requestBody; | |||
| } | |||
|
|
|||
| paths[endpoint.url][endpoint.method.toLowerCase()] = path; | |||
| paths[normalizePath][endpoint.method.toLowerCase()] = path; | |||
There was a problem hiding this comment.
This PR changes the generated OpenAPI paths keys to use {param} syntax, but there are no tests asserting the new behavior. Since SwaggerBuilder.spec.js already exists for this module, add coverage that verifies (1) an endpoint url like /api/v1/books/:id becomes a paths entry /api/v1/books/{id}, and (2) custom routes are normalized similarly, so Swagger "try it out" uses the substituted parameter value.
|
|
@mahendra7041 , thank you very much for your contribution. 🤗 |



Description
This PR fixes the issue with dynamic path parameters in the auto-generated Swagger documentation.
Dynamic routes are now generated using the OpenAPI-compliant
{param}syntax so endpoints execute correctly.Closes #703