path-parser icon indicating copy to clipboard operation
path-parser copied to clipboard

Path.test fails to match when query params are omitted

Open enochcheung opened this issue 5 years ago • 0 comments

When query parameters are specified in the path pattern, it does not match paths that omit all query parameters

import { Path } from 'path-parser';

const path = new Path('/foo?:bar');

// Returns {bar: 1} as expected
console.log(path.test('/foo?bar=1'));

// Fails to match, returns null. I expect this to return {foo: undefined}
console.log(path.test('/foo'));

Seems like the reason this is happening is that the full path is passed into search-string for query param parsing (https://github.com/troch/path-parser/blob/2ee02330e94c0be365c1e9134a98dedf5520b66a/src/Path.ts#L187). For path /foo, search-string will try to interpret /foo as a query param key, and end up returning {'/foo': null}. This causes path matching to fail, because /foo is an unexpected key

enochcheung avatar Apr 15 '20 02:04 enochcheung