Skip to content

Commit 53b3252

Browse files
authored
fix: invalid local pointers are treated as external by resolveInlineRef (#123)
1 parent 1bf76ec commit 53b3252

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/__tests__/resolveInlineRef.spec.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('resolveInlineRef', () => {
137137

138138
it('should resolve top-level $ref', () => {
139139
const doc = {
140-
$ref: '#/$defs/User',
140+
$ref: '#/%24defs/User',
141141
$defs: {
142142
User: {
143143
type: 'object',
@@ -169,16 +169,16 @@ describe('resolveInlineRef', () => {
169169
type: 'array',
170170
contains: {
171171
summary: 'Bear cave',
172-
$ref: '#/$defs/Cave',
172+
$ref: '#/%24defs/Cave',
173173
description: 'Apparently Tom likes bears',
174174
},
175175
},
176176
greatestBear: {
177-
$ref: '#/$defs/Bear',
177+
$ref: '#/%24defs/Bear',
178178
description: 'The greatest bear!',
179179
},
180180
bestBear: {
181-
$ref: '#/$defs/Bear',
181+
$ref: '#/%24defs/Bear',
182182
summary: 'The best bear!',
183183
},
184184
},
@@ -223,4 +223,46 @@ describe('resolveInlineRef', () => {
223223
expect(resolveInlineRefWithLocation(doc, '#/properties/bestBear')).toHaveProperty('location', ['$defs', 'Bear']);
224224
});
225225
});
226+
227+
it('handles encoded characters', () => {
228+
const doc = {
229+
type: 'object',
230+
$defs: {
231+
'Cool Bear': {
232+
type: 'string',
233+
},
234+
'самый крутой медведь?': {
235+
const: 'винни пух)',
236+
},
237+
},
238+
};
239+
240+
expect(resolveInlineRef(doc, '#/%24defs/Cool%20Bear')).toStrictEqual({
241+
type: 'string',
242+
});
243+
244+
expect(
245+
resolveInlineRef(
246+
doc,
247+
'#/%24defs/%D1%81%D0%B0%D0%BC%D1%8B%D0%B9%20%D0%BA%D1%80%D1%83%D1%82%D0%BE%D0%B9%20%D0%BC%D0%B5%D0%B4%D0%B2%D0%B5%D0%B4%D1%8C%3F',
248+
),
249+
).toStrictEqual({
250+
const: 'винни пух)',
251+
});
252+
});
253+
254+
it('gracefully handles unencoded characters', () => {
255+
const doc = {
256+
type: 'object',
257+
$defs: {
258+
'Cool Bear': {
259+
type: 'string',
260+
},
261+
},
262+
};
263+
264+
expect(resolveInlineRef(doc, '#/$defs/Cool Bear')).toStrictEqual({
265+
type: 'string',
266+
});
267+
});
226268
});

src/extractSourceFromRef.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { isLocalRef } from './isLocalRef';
1+
import { isExternalRef } from './isExternalRef';
22

33
export const extractSourceFromRef = (ref: unknown): string | null => {
4-
if (typeof ref !== 'string' || ref.length === 0 || isLocalRef(ref)) {
4+
if (typeof ref !== 'string' || ref.length === 0 || !isExternalRef(ref)) {
55
return null;
66
}
77

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './getJsonPathForPosition';
1111
export * from './getLastPathSegment';
1212
export * from './getLocationForJsonPath';
1313
export * from './hasRef';
14+
export * from './isExternalRef';
1415
export * from './isLocalRef';
1516
export * from './isPlainObject';
1617
export * from './parseWithPointers';

src/isExternalRef.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isExternalRef = (pointer: string) => pointer.length > 0 && pointer[0] !== '#';

0 commit comments

Comments
 (0)