When using MockMultipartFile with an empty originalFilename (""), it now gets added to multi part requests just as a part, not as a file. When migrating from Spring Boot 2.3 to 2.4.1, this made my MockMvc test fail for a controller using a @RequestPart MultipartFile parameter causing a longer debugging session :-) It would be nice, if there had been a check (exception), that empty originalFilenames are no longer supported for MockMultipartFiles.

If the nullability of the originalFilename in MultipartFile is intended, org.springframework.web.multipart.support.StandardMultipartHttpServletRequest#parseRequest seems to be broken as it recognizes parts without filename not as files, so that they don't match MultipartFile parameters.

Comment From: jhoeller

This seems to be a consequence of #25602. I'll revisit the assertions in MockMultipartFile, we should indeed reject empty filenames there then. It's just not entirely clear to me yet where specifically we're evaluating MockMultipartFile-specified empty original filenames to plain parts...

Comment From: rstoyanchev

This is likely impacted by the more recent change in #26166. There is also a separate new report https://github.com/spring-projects/spring-boot/issues/24483.

Comment From: jhoeller

Good catch, @rstoyanchev - so that effect seems to come from the intermediate MockPart handling since there is filename differentiation being made for Part (but not for MultipartFile which is always clearly a file, never a parameter). I guess this is to be treated as a regression then...

Comment From: yankee42

After a long debug session about the same problem after upgrading from Spring 5.2.12 to 5.3.2 and following git blame, I ended up here :-).

When using MockMultipartFile with an empty originalFilename (""), it now gets added to multi part requests just as a part, not as a file.

Just to clarify... (and document this for other people googling this issue): In version 5.3.2 this was always converted to a part, not just if the originalFilename was empty:

https://github.com/spring-projects/spring-framework/blob/17e6cf1cc1b57bd65bfae8da8cddc8314a89408d/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilder.java#L144

In my case I first got a org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present.

After giving the file an "originalFilename" I got an UnsupportedOperationException because I was calling MultipartFile.write which delegates to StandardMultipartFile.transferTo which in turn delegates to MockPart.write which always throws. Reading the code associated with this ticket, I think that should be fixed now, but I did not test, yet.

Comment From: rstoyanchev

Thanks for the comment @yankee42 and sorry for the time spent. If you are able to verify the fix with 5.3.3-SNAPSHOT via https://repo.spring.io/snapshot that would be great.

Comment From: yankee42

If you are able to verify the fix with 5.3.3-SNAPSHOT via https://repo.spring.io/snapshot that would be great.

I did so and confirm that this is fixed in 5.3.3-SNAPSHOT.

Comment From: liorderei

is there a workaround for this issue on 2.4.1? I also get it for MultipartFile and not just for MockMultipartFile

Comment From: narugoyal

is there a workaround for this issue on 2.4.1? I also get it for MultipartFile and not just for MockMultipartFile

For me, using constructor with passing some originalFilename works.

public MockMultipartFile( String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content)