Browse Source

Return MultiValueMap from ServerRequest.queryParams instead of List

This commit changes ServerRequest.queryParams from returning a
List<String> given a String name, to returning a
MultiValueMap<String, String>, which gives more flexibility.
pull/1468/head
Arjen Poutsma 8 years ago
parent
commit
f8589d9eca
  1. 5
      spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java
  2. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java
  3. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java
  4. 14
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java
  5. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java
  6. 5
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java
  7. 19
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapperTests.java

5
spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java

@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath; @@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
@ -168,8 +169,8 @@ public class MockServerRequest implements ServerRequest { @@ -168,8 +169,8 @@ public class MockServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return Collections.unmodifiableList(this.queryParams.get(name));
public MultiValueMap<String, String> queryParams() {
return CollectionUtils.unmodifiableMultiValueMap(this.queryParams);
}
@Override

5
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java

@ -156,9 +156,8 @@ class DefaultServerRequest implements ServerRequest { @@ -156,9 +156,8 @@ class DefaultServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
List<String> queryParams = request().getQueryParams().get(name);
return queryParams != null ? queryParams : Collections.emptyList();
public MultiValueMap<String, String> queryParams() {
return request().getQueryParams();
}
@Override

4
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

@ -535,8 +535,8 @@ public abstract class RequestPredicates { @@ -535,8 +535,8 @@ public abstract class RequestPredicates {
}
@Override
public List<String> queryParams(String name) {
return this.request.queryParams(name);
public MultiValueMap<String, String> queryParams() {
return this.request.queryParams();
}
@Override

14
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java

@ -146,7 +146,7 @@ public interface ServerRequest { @@ -146,7 +146,7 @@ public interface ServerRequest {
* @return the parameter value
*/
default Optional<String> queryParam(String name) {
List<String> queryParams = this.queryParams(name);
List<String> queryParams = queryParams().get(name);
if (queryParams.isEmpty()) {
return Optional.empty();
}
@ -160,12 +160,9 @@ public interface ServerRequest { @@ -160,12 +160,9 @@ public interface ServerRequest {
}
/**
* Return all query parameter with the given name.
* <p>Returns an empty list if no values could be found.
* @param name the parameter name
* @return the parameter values
* Return all query parameters for this request.
*/
List<String> queryParams(String name);
MultiValueMap<String, String> queryParams();
/**
* Return the path variable with the given name, if present.
@ -184,13 +181,12 @@ public interface ServerRequest { @@ -184,13 +181,12 @@ public interface ServerRequest {
}
/**
* Return all path variables for the current request.
* @return a {@code Map} from path variable name to associated value
* Return all path variables for this request.
*/
Map<String, String> pathVariables();
/**
* Return the web session for the current request. Always guaranteed to
* Return the web session for this request. Always guaranteed to
* return an instance either matching to the session id requested by the
* client, or with a new session id either because the client did not
* specify one or because the underlying session had expired. Use of this

4
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java

@ -139,8 +139,8 @@ public class ServerRequestWrapper implements ServerRequest { @@ -139,8 +139,8 @@ public class ServerRequestWrapper implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return this.delegate.queryParams(name);
public MultiValueMap<String, String> queryParams() {
return this.delegate.queryParams();
}
@Override

5
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java

@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath; @@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
@ -167,8 +168,8 @@ public class MockServerRequest implements ServerRequest { @@ -167,8 +168,8 @@ public class MockServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return Collections.unmodifiableList(this.queryParams.get(name));
public MultiValueMap<String, String> queryParams() {
return CollectionUtils.unmodifiableMultiValueMap(this.queryParams);
}
@Override

19
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapperTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@ -18,7 +18,6 @@ package org.springframework.web.reactive.function.server.support; @@ -18,7 +18,6 @@ package org.springframework.web.reactive.function.server.support;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -26,12 +25,12 @@ import org.junit.Before; @@ -26,12 +25,12 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.ServerRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* @author Arjen Poutsma
@ -105,11 +104,11 @@ public class ServerRequestWrapperTests { @@ -105,11 +104,11 @@ public class ServerRequestWrapperTests {
@Test
public void queryParams() throws Exception {
String name = "foo";
List<String> value = Collections.singletonList("bar");
when(mockRequest.queryParams(name)).thenReturn(value);
MultiValueMap<String, String> value = new LinkedMultiValueMap<>();
value.add("foo", "bar");
when(mockRequest.queryParams()).thenReturn(value);
assertSame(value, wrapper.queryParams(name));
assertSame(value, wrapper.queryParams());
}
@Test

Loading…
Cancel
Save