Skip to content

Commit 3ec78e2

Browse files
committed
SPR-9093: UriTemplate not throwing IllegalArgumentException when URIVariables map missing values
1 parent a3bb376 commit 3ec78e2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,9 @@ public MapTemplateVariables(Map<String, ?> uriVariables) {
10021002
}
10031003

10041004
public Object getValue(String name) {
1005+
if (!this.uriVariables.containsKey(name)) {
1006+
throw new IllegalArgumentException("Map has no value for '" + name + "'");
1007+
}
10051008
return this.uriVariables.get(name);
10061009
}
10071010
}
@@ -1010,6 +1013,7 @@ public Object getValue(String name) {
10101013
* URI template variables backed by a variable argument array.
10111014
*/
10121015
private static class VarArgsTemplateVariables implements UriTemplateVariables {
1016+
10131017
private final Iterator<Object> valueIterator;
10141018

10151019
public VarArgsTemplateVariables(Object... uriVariableValues) {
@@ -1018,7 +1022,7 @@ public VarArgsTemplateVariables(Object... uriVariableValues) {
10181022

10191023
public Object getValue(String name) {
10201024
if (!valueIterator.hasNext()) {
1021-
throw new IllegalArgumentException("Not enough variable values available to expand [" + name + "]");
1025+
throw new IllegalArgumentException("Not enough variable values available to expand '" + name + "'");
10221026
}
10231027
return valueIterator.next();
10241028
}

org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,6 +89,15 @@ public void expandMapEncoded() throws Exception {
8989
assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
9090
}
9191

92+
@Test(expected = IllegalArgumentException.class)
93+
public void expandMapUnboundVariables() throws Exception {
94+
Map<String, String> uriVariables = new HashMap<String, String>(2);
95+
uriVariables.put("booking", "42");
96+
uriVariables.put("bar", "1");
97+
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
98+
template.expand(uriVariables);
99+
}
100+
92101
@Test
93102
public void expandEncoded() throws Exception {
94103
UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");

0 commit comments

Comments
 (0)