Skip to content

Commit 5cbbbed

Browse files
committed
Use given name in MultipartBodyBuilder::part
Make sure that we use the parameter name in MultipartBodyBuilder::part when adding a Part, instead of using the name specified in the 'Content-Disposition' header that might have been in the part's headers. Closes gh-27007
1 parent 6f2fe5d commit 5cbbbed

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

spring-web/src/main/java/org/springframework/http/client/MultipartBodyBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,7 +131,12 @@ public PartBuilder part(String name, Object part, @Nullable MediaType contentTyp
131131
Part partObject = (Part) part;
132132
PartBuilder builder = asyncPart(name, partObject.content(), DataBuffer.class);
133133
if (!partObject.headers().isEmpty()) {
134-
builder.headers(headers -> headers.putAll(partObject.headers()));
134+
builder.headers(headers -> {
135+
headers.putAll(partObject.headers());
136+
String filename = headers.getContentDisposition().getFilename();
137+
// reset to parameter name
138+
headers.setContentDispositionFormData(name, filename);
139+
});
135140
}
136141
if (contentType != null) {
137142
builder.contentType(contentType);

spring-web/src/test/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriterTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,7 +105,7 @@ public String getFilename() {
105105
FilePart mockPart = mock(FilePart.class);
106106
HttpHeaders partHeaders = new HttpHeaders();
107107
partHeaders.setContentType(MediaType.TEXT_PLAIN);
108-
partHeaders.setContentDispositionFormData("filePublisher", "file.txt");
108+
partHeaders.setContentDispositionFormData("foo", "file.txt");
109109
partHeaders.add("foo", "bar");
110110
given(mockPart.headers()).willReturn(partHeaders);
111111
given(mockPart.content()).willReturn(bufferPublisher);
@@ -159,17 +159,20 @@ public String getFilename() {
159159
assertThat(part.headers().getContentLength()).isEqualTo(utf8.getFile().length());
160160

161161
part = requestParts.getFirst("json");
162+
assertThat(part).isNotNull();
162163
assertThat(part.name()).isEqualTo("json");
163164
assertThat(part.headers().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
164165
String value = decodeToString(part);
165166
assertThat(value).isEqualTo("{\"bar\":\"bar\"}");
166167

167168
part = requestParts.getFirst("publisher");
169+
assertThat(part).isNotNull();
168170
assertThat(part.name()).isEqualTo("publisher");
169171
value = decodeToString(part);
170172
assertThat(value).isEqualTo("foobarbaz");
171173

172174
part = requestParts.getFirst("filePublisher");
175+
assertThat(part).isNotNull();
173176
assertThat(part.name()).isEqualTo("filePublisher");
174177
assertThat(part.headers()).containsEntry("foo", Collections.singletonList("bar"));
175178
assertThat(((FilePart) part).filename()).isEqualTo("file.txt");

0 commit comments

Comments
 (0)