fix(openapi): correctly merge plugin router prefix with route paths#25616
fix(openapi): correctly merge plugin router prefix with route paths#25616jhoward1994 merged 5 commits intostrapi:developfrom
Conversation
|
@Akash504-ai is attempting to deploy a commit to the Strapi Team on Vercel. A member of the Team first needs to authorize it. |
|
Thank your for raising this PR @Akash504-ai! |
| : // Else, extract and flatten every route from each router | ||
| Object.values(routes).flatMap((router) => router.routes); | ||
| ? routes | ||
| : Object.values(routes).flatMap((router: any) => { |
There was a problem hiding this comment.
Could we use the type "router: Core.Router" here?
(import type { Core } from '@strapi/types';)
There was a problem hiding this comment.
I'll update the type to router: Core.Router instead of any and push the change.
| Object.values(routes).flatMap((router) => router.routes); | ||
| ? routes | ||
| : Object.values(routes).flatMap((router: any) => { | ||
| const prefix = router.prefix ?? ''; |
There was a problem hiding this comment.
I think this might have to be something like this
const hasOwnPrefix =
route.config != null &&
Object.prototype.hasOwnProperty.call(route.config, 'prefix');
const effectivePrefix = hasOwnPrefix
? (route.config.prefix ?? '')
: (router.prefix ?? '');
Otherwise the changes would generate incorrect OpenAPI paths for e.g. packages/plugins/users-permissions/server/routes/content-api/auth.js as they have route level prefixes defined at config.prefix ?
There was a problem hiding this comment.
I'll update the implementation to respect route-level prefixes before falling back to router.prefix and push the change shortly.
|
Hi! @jhoward1994 I have pushed the requested changes. |
|
Thanks @Akash504-ai ! One more thing, would you be able to add this code to This will make sure we have unit tests that cover this new behaviour |
|
@jhoward1994 I have added the unit tests for the plugin route prefix behavior and route-level prefix overrides. I also ran the tests locally to ensure everything passes. Please let me know if any adjustments are needed. |
|
thank your for your help @Akash504-ai! |
|
You're welcome @nclsndr! Happy to help |
What does it do?
Fixes an issue in the OpenAPI generator where plugin route prefixes were not merged with their route paths.
PluginRoutesProviderwas returningrouter.routesdirectly and ignoringrouter.prefix, causing prefixed plugin routes (e.g. upload) to be generated under/instead of their actual base path.This change merges
router.prefixwithroute.pathand normalizes the resulting path to prevent duplicate or trailing slashes.Why is it needed?
When running: strapi openapi generate
Upload plugin routes were generated like:
Instead of:
This resulted in incorrect OpenAPI specifications for plugin routes.
How to test it?
developbuildexamples/kitchensink-tsyarn developyarn strapi openapi generatespecification.jsonBefore fix:
After fix:
Related issue(s)/PR(s)
Fixes #25591