Skip to content

Commit 4e85db4

Browse files
author
Christophe Krebser
committed
fix(UrlMatcher): isOptional always false for empty parameter
1 parent 87f10df commit 4e85db4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/urlMatcherFactory.js

-2
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,6 @@ function $UrlMatcherFactory() {
920920
type = getType(config, type, location);
921921
var arrayMode = getArrayMode();
922922
type = arrayMode ? type.$asArray(arrayMode, location === "search") : type;
923-
if (type.name === "string" && !arrayMode && location === "path" && config.value === undefined)
924-
config.value = ""; // for 0.2.x; in 0.3.0+ do not automatically default to ""
925923
var isOptional = config.value !== undefined;
926924
var squash = getSquashPolicy(config, isOptional);
927925
var replace = getReplace(config, arrayMode, isOptional, squash);

test/urlMatcherFactorySpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,22 @@ describe("UrlMatcher", function () {
156156
var m = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
157157
expect(m.exec('/users/123/details/what/thisShouldBeDigits', {})).toBeNull();
158158
});
159+
160+
it("should not use optional regexp for '/'", function () {
161+
var m = new UrlMatcher('/{language:(?:fr|en|de)}');
162+
expect(m.exec('/', {})).toBeNull();
163+
});
164+
165+
it("should work with empty default value", function () {
166+
var m = new UrlMatcher('/foo/:str', { params: { str: { value: "" } } });
167+
expect(m.exec('/foo/', {})).toEqual({ str: "" });
168+
});
159169

170+
it("should work with empty default value for regex", function () {
171+
var m = new UrlMatcher('/foo/{param:(?:foo|bar|)}', { params: { param: { value: "" } } });
172+
expect(m.exec('/foo/', {})).toEqual({ param: "" });
173+
});
174+
160175
it("should treat the URL as already decoded and does not decode it further", function () {
161176
expect(new UrlMatcher('/users/:id').exec('/users/100%25', {})).toEqual({ id: '100%25'});
162177
});

0 commit comments

Comments
 (0)