Skip to content

Commit ca1cad2

Browse files
authored
fix: findOperation issues with "/" paths (#223)
* fix: findOperation issues with "/" paths * test: add check for res.operation feedback: #223 (comment)
1 parent a6ae8cc commit ca1cad2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/tooling/__tests__/oas.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,48 @@ describe('#findOperation()', () => {
185185
});
186186
});
187187

188+
it('should return result if path is slash', () => {
189+
const oas = new Oas({
190+
openapi: '3.0.0',
191+
servers: [
192+
{
193+
url: 'https://example.com',
194+
},
195+
],
196+
paths: {
197+
'/': {
198+
get: {
199+
responses: {
200+
'200': {
201+
description: 'OK',
202+
},
203+
},
204+
},
205+
},
206+
},
207+
});
208+
209+
const uri = 'https://example.com';
210+
const method = 'get';
211+
212+
const res = oas.findOperation(uri, method);
213+
expect(res.url).toStrictEqual({
214+
origin: 'https://example.com',
215+
path: '/',
216+
nonNormalizedPath: '/',
217+
slugs: {},
218+
method: 'GET',
219+
});
220+
221+
expect(res.operation).toStrictEqual({
222+
responses: {
223+
'200': {
224+
description: 'OK',
225+
},
226+
},
227+
});
228+
});
229+
188230
it('should return result if in server variable defaults', () => {
189231
const oas = new Oas(serverVariables);
190232
const uri = 'https://demo.example.com:443/v2/post';

packages/tooling/src/oas.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ class Oas {
167167
if (!targetServer) return undefined;
168168
targetServer.url = this.replaceUrl(targetServer.url, targetServer.variables || {});
169169

170-
const [, pathName] = url.split(targetServer.url);
170+
let [, pathName] = url.split(targetServer.url);
171171
if (pathName === undefined) return undefined;
172+
if (pathName === '') pathName = '/';
172173
const annotatedPaths = generatePathMatches(paths, pathName, targetServer.url);
173174
if (!annotatedPaths.length) return undefined;
174175

0 commit comments

Comments
 (0)