UriComponentsBuilder supports query without value

Fix UriComponentsBuilder to support query parameters that do not
include a value without losing '='. The following styles are now
supported:

    http://example.com/foo?bar=baz
    http://example.com/foo?bar=
    http://example.com/foo?bar

Issue: SPR-10215
master
Phillip Webb 12 years ago
parent f32ce3a613
commit 203b22b246
  1. 10
      spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
  2. 22
      spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -53,7 +53,7 @@ import org.springframework.util.StringUtils;
*/
public class UriComponentsBuilder {
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)=?([^&]+)?");
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
private static final String SCHEME_PATTERN = "([^:/?#]+):";
@ -496,8 +496,10 @@ public class UriComponentsBuilder {
Matcher m = QUERY_PARAM_PATTERN.matcher(query);
while (m.find()) {
String name = m.group(1);
String value = m.group(2);
queryParam(name, value);
String eq = m.group(2);
String value = m.group(3);
queryParam(name, (value != null ? value :
(StringUtils.hasLength(eq) ? "" : null)));
}
}
else {

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -27,10 +27,12 @@ import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Phillip Webb
*/
public class UriComponentsBuilderTests {
@ -312,4 +314,22 @@ public class UriComponentsBuilderTests {
assertEquals("mailto:foo@example.com", result.toUriString());
}
@Test
public void queryParamWithValueWithEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=baz").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar=baz"));
}
@Test
public void queryParamWithoutValueWithEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar="));
}
@Test
public void queryParamWithoutValueWithoutEquals() throws Exception {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar").build();
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar"));
}
}

Loading…
Cancel
Save