11import outdent from 'outdent' ;
22import { parseYamlToDocument } from '../../__tests__/utils.js' ;
3- import { parseRef , refBaseName } 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%2Fcomplex~name' ;
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 `
@@ -139,4 +158,29 @@ describe('ref-utils', () => {
139158 expect ( refBaseName ( 'abcdefg' ) ) . toStrictEqual ( 'abcdefg' ) ;
140159 } ) ;
141160 } ) ;
161+
162+ describe ( 'escapePointerFragment' , ( ) => {
163+ it ( 'should escape a simple pointer fragment with ~ and / correctly' , ( ) => {
164+ expect ( escapePointerFragment ( 'scope/complex~name' ) ) . toStrictEqual ( 'scope~1complex~0name' ) ;
165+ } ) ;
166+
167+ it ( 'should escape a pointer fragment with a number correctly' , ( ) => {
168+ expect ( escapePointerFragment ( 123 ) ) . toStrictEqual ( 123 ) ;
169+ } ) ;
170+
171+ it ( 'should not URI-encode other special characters when escaping pointer fragments per https://datatracker.ietf.org/doc/html/rfc6901#section-6' , ( ) => {
172+ expect ( escapePointerFragment ( 'curly{braces}' ) ) . toStrictEqual ( 'curly{braces}' ) ;
173+ expect ( escapePointerFragment ( 'plus+' ) ) . toStrictEqual ( 'plus+' ) ;
174+ } ) ;
175+ } ) ;
176+
177+ describe ( 'unescapePointerFragment' , ( ) => {
178+ it ( 'should unescape a pointer with a percent sign correctly' , ( ) => {
179+ expect ( unescapePointerFragment ( 'activity_level_%25' ) ) . toStrictEqual ( 'activity_level_%' ) ;
180+ } ) ;
181+
182+ it ( 'should unescape a pointer correctly' , ( ) => {
183+ expect ( unescapePointerFragment ( 'scope~1complex~0name' ) ) . toStrictEqual ( 'scope/complex~name' ) ;
184+ } ) ;
185+ } ) ;
142186} ) ;
0 commit comments