Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

559: fix multiple encodings of params #738

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/main/java/org/springframework/hateoas/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.experimental.Wither;

import java.io.Serializable;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -49,7 +50,7 @@
*/
@XmlType(name = "link", namespace = Link.ATOM_NAMESPACE)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = "templated", ignoreUnknown = true)
@JsonIgnoreProperties(value = "templated", ignoreUnknown = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@EqualsAndHashCode(of = { "rel", "href", "hreflang", "media", "title", "deprecation", "affordances" })
Expand Down Expand Up @@ -131,7 +132,7 @@ protected Link() {

/**
* Returns safe copy of {@link Affordance}s.
*
*
* @return
*/
public List<Affordance> getAffordances() {
Expand Down Expand Up @@ -166,7 +167,7 @@ public Link andAffordance(Affordance affordance) {

/**
* Create new {@link Link} with additional {@link Affordance}s.
*
*
* @param affordances must not be {@literal null}.
* @return
*/
Expand All @@ -181,7 +182,7 @@ public Link andAffordances(List<Affordance> affordances) {

/**
* Creats a new {@link Link} with the given {@link Affordance}s.
*
*
* @param affordances must not be {@literal null}.
* @return
*/
Expand Down Expand Up @@ -227,7 +228,12 @@ public boolean isTemplated() {
* @return
*/
public Link expand(Object... arguments) {
return new Link(getUriTemplate().expand(arguments).toString(), getRel());

URI uri = (arguments == null || arguments.length == 0) ?
getUriTemplate().checkParams(arguments) :
getUriTemplate().expand(arguments);

return new Link(uri.toString(), getRel());
}

/**
Expand All @@ -242,7 +248,7 @@ public Link expand(Map<String, ? extends Object> arguments) {

/**
* Returns whether the current {@link Link} has the given link relation.
*
*
* @param rel must not be {@literal null} or empty.
* @return
*/
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/springframework/hateoas/UriTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ public URI expand(Object... parameters) {
return builder.build().toUri();
}

/**
* Expands the {@link UriTemplate} using the given parameters. The values will be applied in the order of the
* variables discovered.
*
* @param parameters
* @return
* @see #expand(Map)
*/
public URI checkParams(Object... parameters) {
Climax85 marked this conversation as resolved.
Show resolved Hide resolved
org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri);
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters));
Iterator<Object> iterator = Arrays.asList(parameters).iterator();

for (TemplateVariable variable : getOptionalVariables()) {

Object value = iterator.hasNext() ? iterator.next() : null;
appendToBuilder(builder, variable, value);
}

return URI.create(baseUri);
}

/**
* Expands the {@link UriTemplate} using the given parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ public void linksToMethodWithPathVariableContainingBlank() {
assertThat(link.getHref()).endsWith("/something/with%20blank/foo");
}

@Test
public void uriComponentsToMethodWithPathVariableContainingBlankAnsOptionalQueryParams() {

Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("with blank", 0, 10)).withSelfRel();

UriComponents components = toComponents(link);
assertThat(components.toUriString(), containsString("/something/with%20blank/foo"));
assertThat(components.toUriString(), containsString("offset=0"));
assertThat(components.toUriString(), containsString("limit=10"));
}

@Test
public void uriComponentsToMethodWithPathVariableContainingBlankAnsOptionalQueryParamNull() {

Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("with blank", null, 10)).withSelfRel();

UriComponents components = toComponents(link);
assertThat(components.toUriString(), containsString("/something/with%20blank/foo"));
assertThat(components.toUriString(), containsString("limit=10"));
}

/**
* @see #192
*/
Expand Down Expand Up @@ -590,6 +611,18 @@ public void alternativePathVariableParameter() {
assertThat(link.getHref()).isEqualTo("http://localhost/something/bar/foo");
}

@Test
public void uriComponentsToMethodWithPathVariableAndRequestParamsContainingNull()
{
Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("1", null, 10)).withSelfRel();

UriComponents components = toComponents(link);
assertThat(components.getPath(), is(equalTo("/something/1/foo")));

MultiValueMap<String, String> queryParams = components.getQueryParams();
assertThat(queryParams.get("limit"), contains("10"));
}

private static UriComponents toComponents(Link link) {
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
}
Expand Down