diff --git a/packages/kbn-router-to-openapispec/src/util.test.ts b/packages/kbn-router-to-openapispec/src/util.test.ts index d78c3bf57589c..f9692e57e1f50 100644 --- a/packages/kbn-router-to-openapispec/src/util.test.ts +++ b/packages/kbn-router-to-openapispec/src/util.test.ts @@ -281,6 +281,7 @@ describe('createOpIdGenerator', () => { }); test.each([ { input: { method: 'GET', path: '/api/file' }, output: 'get-file' }, + { input: { method: 'GET', path: '///api/file///' }, output: 'get-file' }, { input: { method: 'POST', path: '/internal/api/file' }, output: 'post-file' }, { input: { method: 'PUT', path: '/internal/file' }, output: 'put-file' }, { input: { method: 'Put', path: 'fOO/fILe' }, output: 'put-foo-file' }, diff --git a/packages/kbn-router-to-openapispec/src/util.ts b/packages/kbn-router-to-openapispec/src/util.ts index c102802706556..a5718fa92120f 100644 --- a/packages/kbn-router-to-openapispec/src/util.ts +++ b/packages/kbn-router-to-openapispec/src/util.ts @@ -194,22 +194,26 @@ export const createOpIdGenerator = (): GetOpId => { ); } - path = path.trim().toLowerCase(); + path = path + .trim() + .replace(/^[\/]+/, '') + .replace(/[\/]+$/, '') + .toLowerCase(); - const removePrefixes = ['/internal/api/', '/internal/', '/api/']; // longest to shortest + const removePrefixes = ['internal/api/', 'internal/', 'api/']; // longest to shortest for (const prefix of removePrefixes) { if (path.startsWith(prefix)) { - path = path.substring(prefix.length - 1); + path = path.substring(prefix.length); break; } } - const opId = `${method.toLowerCase()}-${path - .replace(/^\//, '') - .replace(/\/$/, '') - .replace(/[\{\}\?\*]/g, '') - .replace(/[\/_]/g, '-') - .replace(/[-]+/g, '-')}`; + path = path + .replace(/[\{\}\?\*]/g, '') // remove special chars + .replace(/[\/_]/g, '-') // everything else to dashes + .replace(/[-]+/g, '-'); // single dashes + + const opId = `${method.toLowerCase()}-${path}`; const cachedCount = idMap.get(opId) ?? 0; idMap.set(opId, cachedCount + 1);