Skip to content

Commit

Permalink
fix: fix bug of unrecognized expression when containing z char
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Dec 25, 2024
1 parent 15fa4c9 commit 889dc56
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ path-segment = 1*( path-literal / template-expression )
slash = "/"
path-literal = 1*pchar
template-expression = "{" template-expression-param-name "}"
template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF ) ; every UTF8 character except { and } (from OpenAPI)
template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF / "z" ) ; every UTF8 character except { and } (from OpenAPI)
; Characters definitions (from RFC 3986)
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
Expand Down
2 changes: 1 addition & 1 deletion src/path-templating.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ path-segment = 1*( path-literal / template-expression )
slash = "/"
path-literal = 1*pchar
template-expression = "{" template-expression-param-name "}"
template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF ) ; every UTF8 character except { and } (from OpenAPI)
template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF / "z" ) ; every UTF8 character except { and } (from OpenAPI)

; Characters definitions (from RFC 3986)
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
Expand Down
9 changes: 5 additions & 4 deletions src/path-templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export default function grammar(){
// SUMMARY
// rules = 13
// udts = 0
// opcodes = 65
// opcodes = 66
// --- ABNF original opcodes
// ALT = 7
// CAT = 4
// REP = 5
// RNM = 16
// TLS = 27
// TLS = 28
// TBS = 1
// TRG = 5
// --- SABNF superset opcodes
Expand Down Expand Up @@ -82,10 +82,11 @@ export default function grammar(){
/* template-expression-param-name */
this.rules[5].opcodes = [];
this.rules[5].opcodes[0] = { type: 3, min: 1, max: Infinity };// REP
this.rules[5].opcodes[1] = { type: 1, children: [2,3,4] };// ALT
this.rules[5].opcodes[1] = { type: 1, children: [2,3,4,5] };// ALT
this.rules[5].opcodes[2] = { type: 5, min: 0, max: 121 };// TRG
this.rules[5].opcodes[3] = { type: 6, string: [124] };// TBS
this.rules[5].opcodes[4] = { type: 5, min: 126, max: 1114111 };// TRG
this.rules[5].opcodes[5] = { type: 7, string: [122] };// TLS

/* pchar */
this.rules[6].opcodes = [];
Expand Down Expand Up @@ -158,7 +159,7 @@ export default function grammar(){
str += "slash = \"/\"\n";
str += "path-literal = 1*pchar\n";
str += "template-expression = \"{\" template-expression-param-name \"}\"\n";
str += "template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF ) ; every UTF8 character except { and } (from OpenAPI)\n";
str += "template-expression-param-name = 1*( %x00-79 / %x7C / %x7E-10FFFF / \"z\" ) ; every UTF8 character except { and } (from OpenAPI)\n";
str += "\n";
str += "; Characters definitions (from RFC 3986)\n";
str += "pchar = unreserved / pct-encoded / sub-delims / \":\" / \"@\"\n";
Expand Down
35 changes: 17 additions & 18 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { test } from '../src/index.js';

describe('test', function () {
it('should detect as path template', function () {
assert.isTrue(test('/{path}'));
assert.isTrue(test('/{path}/')); // trailing slash is allowed
assert.isTrue(test('/pets/{petId}'));
assert.isTrue(test('/{entity}/me'));
assert.isTrue(test('/books/{id}'));
assert.isTrue(test('/a{test}'));
assert.isTrue(test('/foo/bar/{baz}/test/{foo_id}/baz/{bar_id}'));
assert.isTrue(test('/{test}a'));
assert.isTrue(test('/range({x},{y})')); // parentheses are allowed
assert.isTrue(test('/range({x},{y})/secondRange({x},{y})')); // repeated parameter names are allowed
Expand All @@ -29,9 +29,24 @@ describe('test', function () {
assert.isTrue(test('/users/{user-id}'));
assert.isTrue(test('/{❤️}'));
assert.isTrue(test('/{%}'));
// RFC 6570 operators
assert.isTrue(test('/{foo:}'));
assert.isTrue(test('/{foo:bar}'));
assert.isTrue(test('/{=bar}'));
assert.isTrue(test('/{$bar}'));
assert.isTrue(test('/{~bar}'));
assert.isTrue(test('/{#bar}'));
assert.isTrue(test('/{?bar}'));
assert.isTrue(test('/{/bar}'));
assert.isTrue(test('/{foo bar}'));
assert.isTrue(test('/{|bar}'));
assert.isTrue(test('/{^bar}'));
assert.isTrue(test('/{`bar}'));
// RFC 6570 operators are allowed
assert.isTrue(test('/{y,x}'), '/{y,x}');
assert.isTrue(test('/{count*}'));
assert.isTrue(test('/{;bar}'));
assert.isTrue(test('/{&bar}'));
assert.isTrue(test('/{.bar}'));
});

it('should not detect expression', function () {
Expand All @@ -46,22 +61,6 @@ describe('test', function () {
assert.isFalse(test('/#baz'));
// special characters in literals are not allowed
assert.isFalse(test('/❤️'));
// special characters in parameters names are not allowed
assert.isFalse(test('/{foo:baz}'));
assert.isFalse(test('/{=baz}'));
assert.isFalse(test('/{$baz}'));
assert.isFalse(test('/{~baz}'));
assert.isFalse(test('/{#baz}'));
assert.isFalse(test('/{?baz}'));
assert.isFalse(test('/{/baz}'));
assert.isFalse(test('/{foo baz}'));
assert.isFalse(test('/{|baz}'));
assert.isFalse(test('/{^baz}'));
assert.isFalse(test('/{`baz}'));
// RFC 6570 operators
assert.isFalse(test('/{;baz}'));
assert.isFalse(test('/{&baz}'));
assert.isFalse(test('/{.baz}'));
// invalid types
assert.isFalse(test(1));
assert.isFalse(test(null));
Expand Down

0 comments on commit 889dc56

Please sign in to comment.