Skip to content

Commit 0cbf0e1

Browse files
CountRedClawb41ex
authored andcommitted
fix: path mapping for a real case with mistyped slashes
1 parent 3de6a50 commit 0cbf0e1

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/openapi/openapi3.mapping.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { MapKeysResult, MappingResolver, NodeContext } from '../types'
2-
import { difference, getStringValue, intersection, objectKeys, onlyExistedArrayIndexes } from '../utils'
2+
import {
3+
difference,
4+
getStringValue,
5+
intersection,
6+
objectKeys,
7+
onlyExistedArrayIndexes,
8+
removeSlashes,
9+
} from '../utils'
310
import { mapPathParams } from './openapi3.utils'
411
import { OpenAPIV3 } from 'openapi-types'
512

@@ -206,11 +213,7 @@ export const extractOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): st
206213

207214
export function createPathUnifier(nodeContext: NodeContext): (path: string) => string {
208215
const serverPrefix = extractOperationBasePath((nodeContext.root as OpenAPIV3.Document).servers) // /api/v2
209-
return (path) => (
210-
serverPrefix
211-
? `${serverPrefix}${hidePathParamNames(path)}`
212-
: hidePathParamNames(path)
213-
)
216+
return (path) => removeSlashes(`${serverPrefix}${hidePathParamNames(path)}`)
214217
}
215218

216219
export function hidePathParamNames(path: string): string {

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,7 @@ export function difference(array1: string[], array2: string[]): string[] {
231231
const set2 = new Set(array2)
232232
return [...new Set(array1.filter(x => !set2.has(x)))]
233233
}
234+
235+
export function removeSlashes(input: string): string {
236+
return input.replace(/\//g, '')
237+
}

test/openapi.pathAndMethodMapping.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,33 @@ describe('Path and method mapping', () => {
5757
}),
5858
]))
5959
})
60+
61+
it('Remove mistyped slashes', () => {
62+
const before = openapiBuilder
63+
.addPath({
64+
path: '//path1/',
65+
responses: { '200': { description: 'OK' } },
66+
})
67+
.getSpec()
68+
69+
openapiBuilder.reset()
70+
71+
const after = openapiBuilder
72+
.addPath({
73+
path: '/path1',
74+
responses: { '200': { description: 'OK' } },
75+
})
76+
.getSpec()
77+
78+
const { diffs } = apiDiff(before, after)
79+
80+
expect(diffs).toEqual(diffsMatcher([
81+
expect.objectContaining({
82+
beforeDeclarationPaths: [['paths', '//path1/']],
83+
afterDeclarationPaths: [['paths', '/path1']],
84+
action: DiffAction.rename,
85+
type: breaking, // todo should be annotation
86+
})
87+
]))
88+
})
6089
})

0 commit comments

Comments
 (0)