Unwrap Part content in asyncPart builder method

Closes gh-22876
This commit is contained in:
Rossen Stoyanchev
2019-05-08 10:58:57 -04:00
parent 6a05b97df3
commit 44659f4945
2 changed files with 37 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* 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.
@@ -22,12 +22,15 @@ import java.util.Map;
import java.util.function.Consumer;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.Part;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
@@ -121,15 +124,21 @@ public final class MultipartBodyBuilder {
/**
* Add an asynchronous part with {@link Publisher}-based content.
* @param name the name of the part to add
* @param publisher the part contents
* @param publisher a Publisher of content for the part
* @param elementClass the type of elements contained in the publisher
* @return builder that allows for further customization of part headers
*/
@SuppressWarnings("unchecked")
public <T, P extends Publisher<T>> PartBuilder asyncPart(String name, P publisher, Class<T> elementClass) {
Assert.hasLength(name, "'name' must not be empty");
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(elementClass, "'elementClass' must not be null");
if (Part.class.isAssignableFrom(elementClass)) {
publisher = (P) Mono.from(publisher).flatMapMany(p -> ((Part) p).content());
elementClass = (Class<T>) DataBuffer.class;
}
HttpHeaders headers = new HttpHeaders();
PublisherPartBuilder<T, P> builder = new PublisherPartBuilder<>(headers, publisher, elementClass);
this.parts.add(name, builder);