Skip to content

Commit 1dbf737

Browse files
committed
fix: 转义后的中文无法正确寻找ref源
1 parent f3e4c9e commit 1dbf737

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lib/ref-to-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const refToObject = <T extends object>(
77
if ('$ref' in data) {
88
const target = data.$ref.split('/').reduce<any>((carry, pathName) => {
99
if (pathName === '#') return carry;
10-
return carry[pathName];
10+
return carry[pathName] || carry[decodeURIComponent(pathName)];
1111
}, docs);
1212
return target as T;
1313
}

test/lib/ref-to-object.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test, expect } from 'vitest';
2+
import { getBasicDocument } from '../mocks/get-basic-document';
3+
import { refToObject } from '../../src/lib/ref-to-object';
4+
5+
test('中文被转译', () => {
6+
const docs = getBasicDocument();
7+
docs.components ||= {};
8+
docs.components.parameters ||= {};
9+
docs.components.parameters['你好'] = { name: 'foo', in: 'query' };
10+
11+
expect(refToObject(docs, { $ref: '#/components/parameters/你好' })).toMatchObject({
12+
in: 'query',
13+
name: 'foo',
14+
});
15+
16+
expect(
17+
refToObject(docs, { $ref: '#/components/parameters/%E4%BD%A0%E5%A5%BD' }),
18+
).toMatchObject({
19+
in: 'query',
20+
name: 'foo',
21+
});
22+
});

0 commit comments

Comments
 (0)