Skip to content

Commit

Permalink
Push canWrite down into MultipartHttpMessageWriter
Browse files Browse the repository at this point in the history
The implementation in the base class only matches the
MultipartHttpMessageWriter subclass. The other two override it anyway.

Closes gh-29631
  • Loading branch information
rstoyanchev committed Dec 5, 2022
1 parent a7bf14b commit 93ea2e1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Expand Up @@ -145,6 +145,20 @@ public HttpMessageWriter<MultiValueMap<String, String>> getFormWriter() {
}


@Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {
if (MultiValueMap.class.isAssignableFrom(elementType.toClass())) {
if (mediaType == null) {
return true;
}
for (MediaType supportedMediaType : getWritableMediaTypes()) {
if (supportedMediaType.isCompatibleWith(mediaType)) {
return true;
}
}
}
return false;
}

@Override
public Mono<Void> write(Publisher<? extends MultiValueMap<String, ?>> inputStream,
Expand Down
Expand Up @@ -24,7 +24,6 @@

import reactor.core.publisher.Mono;

import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
Expand All @@ -34,7 +33,6 @@
import org.springframework.util.Assert;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;

/**
* Support class for multipart HTTP message writers.
Expand Down Expand Up @@ -83,21 +81,6 @@ public List<MediaType> getWritableMediaTypes() {
return this.supportedMediaTypes;
}


public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {
if (MultiValueMap.class.isAssignableFrom(elementType.toClass())) {
if (mediaType == null) {
return true;
}
for (MediaType supportedMediaType : this.supportedMediaTypes) {
if (supportedMediaType.isCompatibleWith(mediaType)) {
return true;
}
}
}
return false;
}

/**
* Generate a multipart boundary.
* <p>By default delegates to {@link MimeTypeUtils#generateMultipartBoundary()}.
Expand Down
Expand Up @@ -55,6 +55,7 @@ public void canWrite() {
assertThat(this.writer.canWrite(ResolvableType.forClass(Part.class), MediaType.MULTIPART_FORM_DATA)).isTrue();
assertThat(this.writer.canWrite(ResolvableType.forClass(Part.class), MediaType.MULTIPART_MIXED)).isTrue();
assertThat(this.writer.canWrite(ResolvableType.forClass(Part.class), MediaType.MULTIPART_RELATED)).isTrue();
assertThat(this.writer.canWrite(ResolvableType.forClass(MultiValueMap.class), MediaType.MULTIPART_FORM_DATA)).isFalse();
}

@Test
Expand Down

0 comments on commit 93ea2e1

Please sign in to comment.