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.
This commit is contained in:
Arjen Poutsma 2017-07-03 16:23:33 +02:00
parent 2ccbc55ffd
commit f8589d9eca
7 changed files with 26 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -26,12 +25,12 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertSame; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -105,11 +104,11 @@ public class ServerRequestWrapperTests {
@Test @Test
public void queryParams() throws Exception { public void queryParams() throws Exception {
String name = "foo"; MultiValueMap<String, String> value = new LinkedMultiValueMap<>();
List<String> value = Collections.singletonList("bar"); value.add("foo", "bar");
when(mockRequest.queryParams(name)).thenReturn(value); when(mockRequest.queryParams()).thenReturn(value);
assertSame(value, wrapper.queryParams(name)); assertSame(value, wrapper.queryParams());
} }
@Test @Test