Skip to content

Commit

Permalink
fix: invalid local pointers are treated as external by resolveInlineR…
Browse files Browse the repository at this point in the history
…ef (#123)
  • Loading branch information
P0lip authored May 19, 2023
1 parent 1bf76ec commit 53b3252
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
50 changes: 46 additions & 4 deletions src/__tests__/resolveInlineRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('resolveInlineRef', () => {

it('should resolve top-level $ref', () => {
const doc = {
$ref: '#/$defs/User',
$ref: '#/%24defs/User',
$defs: {
User: {
type: 'object',
Expand Down Expand Up @@ -169,16 +169,16 @@ describe('resolveInlineRef', () => {
type: 'array',
contains: {
summary: 'Bear cave',
$ref: '#/$defs/Cave',
$ref: '#/%24defs/Cave',
description: 'Apparently Tom likes bears',
},
},
greatestBear: {
$ref: '#/$defs/Bear',
$ref: '#/%24defs/Bear',
description: 'The greatest bear!',
},
bestBear: {
$ref: '#/$defs/Bear',
$ref: '#/%24defs/Bear',
summary: 'The best bear!',
},
},
Expand Down Expand Up @@ -223,4 +223,46 @@ describe('resolveInlineRef', () => {
expect(resolveInlineRefWithLocation(doc, '#/properties/bestBear')).toHaveProperty('location', ['$defs', 'Bear']);
});
});

it('handles encoded characters', () => {
const doc = {
type: 'object',
$defs: {
'Cool Bear': {
type: 'string',
},
'самый крутой медведь?': {
const: 'винни пух)',
},
},
};

expect(resolveInlineRef(doc, '#/%24defs/Cool%20Bear')).toStrictEqual({
type: 'string',
});

expect(
resolveInlineRef(
doc,
'#/%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',
),
).toStrictEqual({
const: 'винни пух)',
});
});

it('gracefully handles unencoded characters', () => {
const doc = {
type: 'object',
$defs: {
'Cool Bear': {
type: 'string',
},
},
};

expect(resolveInlineRef(doc, '#/$defs/Cool Bear')).toStrictEqual({
type: 'string',
});
});
});
4 changes: 2 additions & 2 deletions src/extractSourceFromRef.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isLocalRef } from './isLocalRef';
import { isExternalRef } from './isExternalRef';

export const extractSourceFromRef = (ref: unknown): string | null => {
if (typeof ref !== 'string' || ref.length === 0 || isLocalRef(ref)) {
if (typeof ref !== 'string' || ref.length === 0 || !isExternalRef(ref)) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './getJsonPathForPosition';
export * from './getLastPathSegment';
export * from './getLocationForJsonPath';
export * from './hasRef';
export * from './isExternalRef';
export * from './isLocalRef';
export * from './isPlainObject';
export * from './parseWithPointers';
Expand Down
1 change: 1 addition & 0 deletions src/isExternalRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isExternalRef = (pointer: string) => pointer.length > 0 && pointer[0] !== '#';

0 comments on commit 53b3252

Please sign in to comment.