diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java index 8f6466d3..c68429c3 100644 --- a/src/main/java/org/springframework/hateoas/UriTemplate.java +++ b/src/main/java/org/springframework/hateoas/UriTemplate.java @@ -89,7 +89,7 @@ public class UriTemplate implements Iterable, Serializable { */ public UriTemplate(String baseUri, TemplateVariables variables) { - Assert.hasText(baseUri,"Base URI must not be null or empty!"); + Assert.hasText(baseUri, "Base URI must not be null or empty!"); this.baseUri = baseUri; this.variables = variables == null ? TemplateVariables.NONE : variables; @@ -99,7 +99,7 @@ public class UriTemplate implements Iterable, Serializable { * Creates a new {@link UriTemplate} with the current {@link TemplateVariable}s augmented with the given ones. * * @param variables can be {@literal null}. - * @return + * @return will never be {@literal null}. */ public UriTemplate with(TemplateVariables variables) { @@ -129,6 +129,17 @@ public class UriTemplate implements Iterable, Serializable { return new UriTemplate(baseUri, this.variables.concat(result)); } + /** + * Creates a new {@link UriTemplate} with a {@link TemplateVariable} with the given name and type added. + * + * @param variableName must not be {@literal null} or empty. + * @param type must not be {@literal null}. + * @return will never be {@literal null}. + */ + public UriTemplate with(String variableName, TemplateVariable.VariableType type) { + return with(new TemplateVariables(new TemplateVariable(variableName, type))); + } + /** * Returns whether the given candidate is a URI template. * diff --git a/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java b/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java index 6b941394..81e06845 100644 --- a/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java +++ b/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java @@ -250,6 +250,17 @@ public class UriTemplateUnitTest { new UriTemplate(null, TemplateVariables.NONE); } + /** + * @see #281 + */ + @Test + public void allowsAddingTemplateVariable() { + + UriTemplate template = new UriTemplate("/").with("q", VariableType.REQUEST_PARAM); + + assertThat(template.toString(), is("/{?q}")); + } + private static void assertVariables(UriTemplate template, TemplateVariable... variables) { assertVariables(template, Arrays.asList(variables)); }