提交 6e79dcdc 编写于 作者: R Rossen Stoyanchev

Merge pull request #23070 from L00kian/23060-fix

/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
......@@ -96,19 +96,12 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageReaderArgu
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(parameter.getParameterType());
if (adapter != null) {
// Mono<Part> or Flux<Part>
MethodParameter elementType = parameter.nested();
if (Part.class.isAssignableFrom(elementType.getNestedParameterType())) {
parts = (adapter.isMultiValue() ? parts : parts.take(1));
return Mono.just(adapter.fromPublisher(parts));
}
// We have to decode the content for each part, one at a time
if (adapter.isMultiValue()) {
return Mono.just(decodePartValues(parts, elementType, bindingContext, exchange, isRequired));
}
return Mono.just(adapter.fromPublisher(
Part.class.isAssignableFrom(elementType.getNestedParameterType()) ?
parts : decodePartValues(parts, elementType, bindingContext, exchange, isRequired)));
}
// <T> or Mono<T>
return decodePartValues(parts, parameter, bindingContext, exchange, isRequired)
.next().cast(Object.class);
}
......
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,13 +53,18 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
import static org.springframework.web.method.MvcAnnotationPredicates.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.web.method.MvcAnnotationPredicates.requestPart;
/**
* Unit tests for {@link RequestPartMethodArgumentResolver}.
* @author Rossen Stoyanchev
* @author Ilya Lukyanovich
*/
public class RequestPartMethodArgumentResolverTests {
......@@ -131,6 +136,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("James", actual.get(1).getName());
}
@Test // gh-23060
public void listPersonNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(List.class, Person.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
List<Person> actual = resolveArgument(param, bodyBuilder);
assertEquals(Collections.emptyList(), actual);
}
@Test
public void monoPerson() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class);
......@@ -141,6 +155,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("Jones", actual.block().getName());
}
@Test // gh-23060
public void monoPersonNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Mono.class, Person.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
Mono<Person> actual = resolveArgument(param, bodyBuilder);
assertNull(actual.block());
}
@Test
public void fluxPerson() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class);
......@@ -154,6 +177,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("James", persons.get(1).getName());
}
@Test // gh-23060
public void fluxPersonNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Flux.class, Person.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
Flux<Person> actual = resolveArgument(param, bodyBuilder);
assertEquals(Collections.emptyList(), actual.collectList().block());
}
@Test
public void part() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
......@@ -177,6 +209,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("{\"name\":\"James\"}", partToUtf8String(actual.get(1)));
}
@Test // gh-23060
public void listPartNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(List.class, Part.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
List<Part> actual = resolveArgument(param, bodyBuilder);
assertEquals(Collections.emptyList(), actual);
}
@Test
public void monoPart() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Mono.class, Part.class);
......@@ -188,6 +229,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("{\"name\":\"Jones\"}", partToUtf8String(part));
}
@Test // gh-23060
public void monoPartNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Mono.class, Part.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
Mono<Part> actual = resolveArgument(param, bodyBuilder);
assertNull(actual.block());
}
@Test
public void fluxPart() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Part.class);
......@@ -201,6 +251,15 @@ public class RequestPartMethodArgumentResolverTests {
assertEquals("{\"name\":\"James\"}", partToUtf8String(parts.get(1)));
}
@Test // gh-23060
public void fluxPartNotRequired() {
MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Flux.class, Part.class);
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
Flux<Part> actual = resolveArgument(param, bodyBuilder);
assertEquals(Collections.emptyList(), actual.collectList().block());
}
@Test
public void personRequired() {
MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
......@@ -278,7 +337,13 @@ public class RequestPartMethodArgumentResolverTests {
@RequestPart("name") Flux<Part> partFlux,
@RequestPart("name") List<Part> partList,
@RequestPart(name = "anotherPart", required = false) Person anotherPerson,
@RequestPart(name = "name", required = false) Mono<Person> anotherPersonMono,
@RequestPart(name = "name", required = false) Flux<Person> anotherPersonFlux,
@RequestPart(name = "name", required = false) List<Person> anotherPersonList,
@RequestPart(name = "anotherPart", required = false) Part anotherPart,
@RequestPart(name = "name", required = false) Mono<Part> anotherPartMono,
@RequestPart(name = "name", required = false) Flux<Part> anotherPartFlux,
@RequestPart(name = "name", required = false) List<Part> anotherPartList,
Person notAnnotated) {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册