File tree Expand file tree Collapse file tree 4 files changed +12
-18
lines changed
tests/e2e/lint/no-invalid-schema-examples-oas3.1-error Expand file tree Collapse file tree 4 files changed +12
-18
lines changed Original file line number Diff line number Diff line change @@ -168,10 +168,6 @@ describe('ref-utils', () => {
168168 expect ( escapePointerFragment ( 123 ) ) . toStrictEqual ( 123 ) ;
169169 } ) ;
170170
171- it ( 'should URI-encode percent sign when escaping pointer fragments' , ( ) => {
172- expect ( escapePointerFragment ( 'percent%' ) ) . toStrictEqual ( 'percent%25' ) ;
173- } ) ;
174-
175171 it ( 'should not URI-encode other special characters when escaping pointer fragments per https://datatracker.ietf.org/doc/html/rfc6901#section-6' , ( ) => {
176172 expect ( escapePointerFragment ( 'curly{braces}' ) ) . toStrictEqual ( 'curly{braces}' ) ;
177173 expect ( escapePointerFragment ( 'plus+' ) ) . toStrictEqual ( 'plus+' ) ;
Original file line number Diff line number Diff line change @@ -41,21 +41,18 @@ export class Location {
4141}
4242
4343export function unescapePointerFragment ( fragment : string ) : string {
44+ const unescaped = fragment . replace ( / ~ 1 / g, '/' ) . replace ( / ~ 0 / g, '~' ) ;
45+
4446 try {
45- return decodeURIComponent ( fragment . replaceAll ( '~1' , '/' ) . replaceAll ( '~0' , '~' ) ) ;
46- } catch ( error ) {
47- return fragment . replaceAll ( '~1' , '/' ) . replaceAll ( '~0' , '~' ) ;
47+ return decodeURIComponent ( unescaped ) ;
48+ } catch ( e ) {
49+ return unescaped ;
4850 }
4951}
5052
5153export function escapePointerFragment < T extends string | number > ( fragment : T ) : T {
5254 if ( typeof fragment === 'number' ) return fragment ;
53- let result : string = fragment ;
54- // add only % escape if % is present
55- if ( result . includes ( '%' ) ) {
56- result = result . replaceAll ( '%' , '%25' ) ;
57- }
58- return result . replaceAll ( '~' , '~0' ) . replaceAll ( '/' , '~1' ) as T ;
55+ return fragment . replaceAll ( '~' , '~0' ) . replaceAll ( '/' , '~1' ) as T ;
5956}
6057
6158export function parseRef ( ref : string ) : { uri : string | null ; pointer : string [ ] } {
Original file line number Diff line number Diff line change @@ -44,12 +44,13 @@ function getAjvValidator(
4444 allowAdditionalProperties : boolean
4545) : ValidateFunction | undefined {
4646 const ajv = getAjv ( resolve , allowAdditionalProperties ) ;
47+ const $id = encodeURI ( loc . absolutePointer ) ;
4748
48- if ( ! ajv . getSchema ( loc . absolutePointer ) ) {
49- ajv . addSchema ( { $id : loc . absolutePointer , ...schema } , loc . absolutePointer ) ;
49+ if ( ! ajv . getSchema ( $id ) ) {
50+ ajv . addSchema ( { $id, ...schema } , $id ) ;
5051 }
5152
52- return ajv . getSchema ( loc . absolutePointer ) ;
53+ return ajv . getSchema ( $id ) ;
5354}
5455
5556export function validateJsonSchema (
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ referenced from openapi.yaml:36:19 at #/paths/~1my_post/post/requestBody/content
3232Error was generated by the no-invalid-schema-examples rule.
3333
3434
35- [3] openapi.yaml:44:28 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%25 /example
35+ [3] openapi.yaml:44:28 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%/example
3636
3737Example value must conform to the schema: type must be number.
3838
@@ -43,7 +43,7 @@ Example value must conform to the schema: type must be number.
434345 |
444446 | responses:
4545
46- referenced from openapi.yaml:43:19 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%25
46+ referenced from openapi.yaml:43:19 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%
4747
4848Error was generated by the no-invalid-schema-examples rule.
4949
You can’t perform that action at this time.
0 commit comments