Skip to content

Commit fd117b2

Browse files
authored
feat!: not parse invalid bracket notation (#175)
1 parent 553adec commit fd117b2

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/client/src/openapi/bracket-notation.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ describe('bracketNotation', () => {
1717
expect(serializer.parsePath('a[b]c[d')).toEqual(['a[b]c[d'])
1818
expect(serializer.parsePath('a[[b]]')).toEqual(['a', '[b]'])
1919
expect(serializer.parsePath('a\\[[b]]')).toEqual(['a[', 'b]'])
20+
expect(serializer.parsePath('abc[]')).toEqual(['abc', ''])
21+
2022
expect(serializer.parsePath('abc[def')).toEqual(['abc[def'])
21-
expect(serializer.parsePath('abc[d][ef')).toEqual(['abc', 'd][ef'])
23+
expect(serializer.parsePath('abc[d][ef')).toEqual(['abc[d][ef'])
24+
expect(serializer.parsePath('abc[d][')).toEqual(['abc[d]['])
25+
expect(serializer.parsePath('abc[')).toEqual(['abc['])
26+
expect(serializer.parsePath('abc]')).toEqual(['abc]'])
2227
})
2328

2429
it.each([

packages/client/src/openapi/bracket-notation.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,26 @@ export class BracketNotationSerializer {
112112
parsePath(path: string): string[] {
113113
const segments: string[] = []
114114

115+
let inBrackets = false
115116
let currentSegment = ''
116117
let backslashCount = 0
117118

118119
for (let i = 0; i < path.length; i++) {
119120
const char = path[i]!
120121
const nextChar = path[i + 1]
121122

122-
if (char === ']' && (nextChar === undefined || nextChar === '[') && backslashCount % 2 === 0) {
123+
if (inBrackets && char === ']' && (nextChar === undefined || nextChar === '[') && backslashCount % 2 === 0) {
124+
if (nextChar === undefined) {
125+
inBrackets = false
126+
}
127+
123128
segments.push(currentSegment)
124129
currentSegment = ''
125130
i++
126131
}
127132

128133
else if (segments.length === 0 && char === '[' && backslashCount % 2 === 0) {
134+
inBrackets = true
129135
segments.push(currentSegment)
130136
currentSegment = ''
131137
}
@@ -140,14 +146,7 @@ export class BracketNotationSerializer {
140146
}
141147
}
142148

143-
if (!segments.length) {
144-
segments.push(currentSegment)
145-
}
146-
else if (currentSegment) {
147-
segments[segments.length - 1] += segments.length === 1 ? `[${currentSegment}` : `][${currentSegment}`
148-
}
149-
150-
return segments
149+
return inBrackets || segments.length === 0 ? [path] : segments
151150
}
152151
}
153152

0 commit comments

Comments
 (0)