Skip to content

Commit

Permalink
Add urlDecode property to ServletCookieValueMethodArgumentResolver
Browse files Browse the repository at this point in the history
Closes gh-26989
  • Loading branch information
rstoyanchev committed Dec 7, 2023
1 parent e36d035 commit 7534090
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -37,14 +37,31 @@
*/
public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {

private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
private UrlPathHelper urlPathHelper = new UrlPathHelper();


public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {
super(beanFactory);
}


/**
* Whether to apply URL decoding to cookie values via
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
* A shortcut for doing the same by setting a {@link UrlPathHelper} with
* its {@code urlDecode} property set accordingly.
* <p>By default set to "true" in which case cookie values are decoded.
* @since 6.1.2
*/
public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(urlDecode);
}

/**
* Set the {@code UrlPathHelper} to use to decode cookie values with via
* {@link UrlPathHelper#decodeRequestString(HttpServletRequest, String)}.
* For most cases you can use {@link #setUrlDecode(boolean)} instead.
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
this.urlPathHelper = urlPathHelper;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -79,8 +79,20 @@ public void resolveCookieStringArgument() throws Exception {
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
}

@Test // gh-26989
public void resolveCookieWithEncodingTurnedOff() throws Exception {
Cookie cookie = new Cookie("name", "Tl=Q/0AUSOx[n)2z4(t]20FZv#?[Ge%H");
request.setCookies(cookie);

this.resolver.setUrlDecode(false);
String result = (String) resolver.resolveArgument(cookieStringParameter, null, webRequest, null);

assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
}


public void params(@CookieValue("name") Cookie cookie,
public void params(
@CookieValue("name") Cookie cookie,
@CookieValue(name = "name", defaultValue = "bar") String cookieString) {
}

Expand Down

0 comments on commit 7534090

Please sign in to comment.