diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java index 5fd93037..0ce8951b 100644 --- a/src/main/java/org/springframework/hateoas/UriTemplate.java +++ b/src/main/java/org/springframework/hateoas/UriTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -183,10 +183,11 @@ public class UriTemplate implements Iterable, Serializable { return URI.create(baseUri); } - UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri); + org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri); + UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters)); Iterator iterator = Arrays.asList(parameters).iterator(); - for (TemplateVariable variable : variables) { + for (TemplateVariable variable : getOptionalVariables()) { Object value = iterator.hasNext() ? iterator.next() : null; appendToBuilder(builder, variable, value); @@ -208,9 +209,11 @@ public class UriTemplate implements Iterable, Serializable { } Assert.notNull(parameters, "Parameters must not be null!"); - UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri); - for (TemplateVariable variable : variables) { + org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri); + UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters)); + + for (TemplateVariable variable : getOptionalVariables()) { appendToBuilder(builder, variable, parameters.get(variable.getName())); } diff --git a/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java b/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java index 4d3d809a..3de32feb 100644 --- a/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java +++ b/src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -232,6 +232,16 @@ public class UriTemplateUnitTest { assertThat(result.getVariableNames(), is(empty())); } + /** + * @see #271 + */ + @Test + public void expandASimplePathVariable() { + + UriTemplate template = new UriTemplate("/foo/{id}"); + assertThat(template.expand(2).toString(), is("/foo/2")); + } + private static void assertVariables(UriTemplate template, TemplateVariable... variables) { assertVariables(template, Arrays.asList(variables)); }