11import outdent from 'outdent' ;
22import { parseYamlToDocument } from '../../__tests__/utils.js' ;
3- import { escapePointer , parseRef , refBaseName , unescapePointer } from '../ref-utils.js' ;
3+ import {
4+ escapePointerFragment ,
5+ parseRef ,
6+ refBaseName ,
7+ unescapePointerFragment ,
8+ } from '../ref-utils.js' ;
49import { lintDocument } from '../lint.js' ;
510import { createConfig } from '../config/index.js' ;
611import { BaseResolver } from '../resolve.js' ;
@@ -35,8 +40,8 @@ describe('ref-utils', () => {
3540 } ) ;
3641
3742 it ( `should unescape complex urlencoded paths` , ( ) => {
38- const referene = 'somefile.yaml#/components/schemas/scope~1complex~0name ' ;
39- expect ( parseRef ( referene ) ) . toMatchInlineSnapshot ( `
43+ const reference = 'somefile.yaml#/components/schemas/scope%2Fcomplex~name ' ;
44+ expect ( parseRef ( reference ) ) . toMatchInlineSnapshot ( `
4045 {
4146 "pointer": [
4247 "components",
@@ -48,6 +53,20 @@ describe('ref-utils', () => {
4853 ` ) ;
4954 } ) ;
5055
56+ it ( `should unescape escaped paths` , ( ) => {
57+ const reference = 'somefile.yaml#/components/schemas/scope~1complex~0name with spaces' ;
58+ expect ( parseRef ( reference ) ) . toMatchInlineSnapshot ( `
59+ {
60+ "pointer": [
61+ "components",
62+ "schemas",
63+ "scope/complex~name with spaces",
64+ ],
65+ "uri": "somefile.yaml",
66+ }
67+ ` ) ;
68+ } ) ;
69+
5170 it ( `should validate definition with urlencoded paths` , async ( ) => {
5271 const document = parseYamlToDocument (
5372 outdent `
@@ -140,32 +159,37 @@ describe('ref-utils', () => {
140159 } ) ;
141160 } ) ;
142161
143- describe ( 'escapePointer ' , ( ) => {
144- it ( 'should escape a pointer correctly' , ( ) => {
145- expect ( escapePointer ( 'scope/complex~name' ) ) . toStrictEqual ( 'scope~1complex~0name' ) ;
162+ describe ( 'escapePointerFragment ' , ( ) => {
163+ it ( 'should escape a simple pointer fragment with ~ and / correctly' , ( ) => {
164+ expect ( escapePointerFragment ( 'scope/complex~name' ) ) . toStrictEqual ( 'scope~1complex~0name' ) ;
146165 } ) ;
147166
148- it ( 'should escape a pointer with a number correctly' , ( ) => {
149- expect ( escapePointer ( 123 ) ) . toStrictEqual ( 123 ) ;
167+ it ( 'should escape a pointer fragment with a number correctly' , ( ) => {
168+ expect ( escapePointerFragment ( 123 ) ) . toStrictEqual ( 123 ) ;
150169 } ) ;
151170
152- it ( 'should escape a pointer with a string correctly' , ( ) => {
153- expect ( escapePointer ( 'scope/complex~name' ) ) . toStrictEqual ( 'scope~1complex~0name' ) ;
171+ it ( 'should URI-encode special characters when escaping pointer fragments per https://datatracker.ietf.org/doc/html/rfc6901#section-6' , ( ) => {
172+ expect ( escapePointerFragment ( 'percent%' ) ) . toStrictEqual ( 'percent%25' ) ;
173+ expect ( escapePointerFragment ( 'caret^' ) ) . toStrictEqual ( 'caret%5E' ) ;
174+ expect ( escapePointerFragment ( 'pipe|' ) ) . toStrictEqual ( 'pipe%7C' ) ;
175+ expect ( escapePointerFragment ( 'doublebackslash\\\\' ) ) . toStrictEqual ( 'doublebackslash%5C' ) ;
176+ expect ( escapePointerFragment ( 'backslash\\' ) ) . toStrictEqual ( 'backslash%22' ) ;
177+ expect ( escapePointerFragment ( 'space ' ) ) . toStrictEqual ( 'space%20' ) ;
154178 } ) ;
155179
156- it ( 'should not URI-encode special characters when escaping pointers ' , ( ) => {
157- // escapePointer should only handle JSON Pointer escaping, not URI encoding
158- expect ( escapePointer ( 'activity_level_% ') ) . toStrictEqual ( 'activity_level_%25 ' ) ;
180+ it ( 'should not URI-encode other special characters when escaping pointer fragments per https://datatracker.ietf.org/doc/html/rfc6901#section-6 ' , ( ) => {
181+ expect ( escapePointerFragment ( 'curly{braces}' ) ) . toStrictEqual ( 'curly{braces}' ) ;
182+ expect ( escapePointerFragment ( 'plus+ ') ) . toStrictEqual ( 'plus+ ' ) ;
159183 } ) ;
160184 } ) ;
161185
162- describe ( 'unescapePointer ' , ( ) => {
186+ describe ( 'unescapePointerFragment ' , ( ) => {
163187 it ( 'should unescape a pointer with a percent sign correctly' , ( ) => {
164- expect ( unescapePointer ( 'activity_level_%25' ) ) . toStrictEqual ( 'activity_level_%' ) ;
188+ expect ( unescapePointerFragment ( 'activity_level_%25' ) ) . toStrictEqual ( 'activity_level_%' ) ;
165189 } ) ;
166190
167191 it ( 'should unescape a pointer correctly' , ( ) => {
168- expect ( unescapePointer ( 'scope~1complex~0name' ) ) . toStrictEqual ( 'scope/complex~name' ) ;
192+ expect ( unescapePointerFragment ( 'scope~1complex~0name' ) ) . toStrictEqual ( 'scope/complex~name' ) ;
169193 } ) ;
170194 } ) ;
171195} ) ;
0 commit comments