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 all commits
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().checkRequiredParams() :
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
16 changes: 16 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,22 @@ public URI expand(Object... parameters) {
return builder.build().toUri();
}

/**
* Checks the {@link UriTemplate} for unset but required params.
*
* @return
*/
public URI checkRequiredParams() {
for (TemplateVariable variable : getOptionalVariables()) {
if (variable.isRequired()) {
throw new IllegalArgumentException(
String.format("Template variable %s is required but no value was given!", variable.getName()));
}
}

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 @@ -231,6 +231,42 @@ public void linksToMethodWithPathVariableAndRequestParams() {
assertThat(queryParams.get("offset"), contains("10"));
}

@Test
public void linksToMethodWithPathVariableAndRequestParamsWithNull() {

Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("1", null, 5)).withSelfRel();

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

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

@Test
public void linksToMethodWithPathVariableWithBlankAndRequestParamsWithNull() {

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

UriComponents components = toComponents(link);
assertThat(components.getPath()).isEqualTo("/something/with%20blank/foo");

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

@Test
public void linksToMethodWithRequestParam() {

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

UriComponents components = toComponents(link);
assertThat(components.getPath()).isEqualTo("/something/with%20blank/foo");

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

/**
* @see #26, #39
*/
Expand Down Expand Up @@ -474,6 +510,15 @@ public void encodesRequestParameterWithSpecialValue() {
assertThat(link.getHref()).endsWith("/something/foo?id=Spring%23%0A");
}

@Test
public void encodesAndExpandsPathvariableWithSpecialValueAndRequestParameterWithNull() {

Link link = linkTo(methodOn(ControllerWithMethods.class).methodForNextPage("Spring#\n", null, 10)).withSelfRel().expand();

assertThat(link.getRel()).isEqualTo(Link.REL_SELF);
assertThat(link.getHref()).endsWith("/something/Spring%23%0A/foo?limit=10");
}

/**
* @see #169
*/
Expand Down