From ca1cad2da6c60f522c184b2392e08b94175083e5 Mon Sep 17 00:00:00 2001 From: Kanad Gupta Date: Wed, 15 Jul 2020 20:13:01 -0500 Subject: [PATCH] fix: findOperation issues with "/" paths (#223) * fix: findOperation issues with "/" paths * test: add check for res.operation feedback: https://github.com/readmeio/oas/pull/223#discussion_r455448217 --- packages/tooling/__tests__/oas.test.js | 42 ++++++++++++++++++++++++++ packages/tooling/src/oas.js | 3 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/tooling/__tests__/oas.test.js b/packages/tooling/__tests__/oas.test.js index 986469be..ce6c0b81 100644 --- a/packages/tooling/__tests__/oas.test.js +++ b/packages/tooling/__tests__/oas.test.js @@ -185,6 +185,48 @@ describe('#findOperation()', () => { }); }); + it('should return result if path is slash', () => { + const oas = new Oas({ + openapi: '3.0.0', + servers: [ + { + url: 'https://example.com', + }, + ], + paths: { + '/': { + get: { + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + }, + }); + + const uri = 'https://example.com'; + const method = 'get'; + + const res = oas.findOperation(uri, method); + expect(res.url).toStrictEqual({ + origin: 'https://example.com', + path: '/', + nonNormalizedPath: '/', + slugs: {}, + method: 'GET', + }); + + expect(res.operation).toStrictEqual({ + responses: { + '200': { + description: 'OK', + }, + }, + }); + }); + it('should return result if in server variable defaults', () => { const oas = new Oas(serverVariables); const uri = 'https://demo.example.com:443/v2/post'; diff --git a/packages/tooling/src/oas.js b/packages/tooling/src/oas.js index 740f8fde..ea0e47f5 100644 --- a/packages/tooling/src/oas.js +++ b/packages/tooling/src/oas.js @@ -167,8 +167,9 @@ class Oas { if (!targetServer) return undefined; targetServer.url = this.replaceUrl(targetServer.url, targetServer.variables || {}); - const [, pathName] = url.split(targetServer.url); + let [, pathName] = url.split(targetServer.url); if (pathName === undefined) return undefined; + if (pathName === '') pathName = '/'; const annotatedPaths = generatePathMatches(paths, pathName, targetServer.url); if (!annotatedPaths.length) return undefined;