Skip to content

MockPart is not added to parameter list anymore for Content-Type application/json #26400

Closed
@cmdjulian

Description

@cmdjulian

Affects: 5.3.3


After the update to version 5.3.3 a few of my MockMvc tests failed. After hours of debuging I found out that it is related to #26261.
In my test I do the following.

mvc.multipart(POSTS_ME) {
        part(mockMultipartJson("video_attributes", payload))
        file(mockMultipartFile("video", "sample_video.mp4", "video/mp4", video))
}

protected fun mockMultipartFile(name: String, file: String, type: String, resource: Resource) =
    MockMultipartFile(name, file, type, resource.inputStream)

protected fun mockMultipartJson(partName: String, payload: Map<String, Any?>) =
    MockPart(partName, JSONObject(payload).toString().encodeToByteArray()).apply {
        headers.contentType = MediaType.APPLICATION_JSON
}

Using Postman everything works as expected. I found out my Part file is not getting set as request parameter inside the MockHttpRequest. My Controller has the following methods:

@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE], params = ["image_attributes"])
fun createImagePost(
    @RequestPart("image") image: MultipartFile,
    @Valid @RequestPart("image_attributes") attributes: CreatePostWithContentCommand,
    request: HttpServletRequest
): ResponseEntity<PostDto>

@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE], params = ["video_attributes"])
fun createVideoPost(
    @RequestPart("video") video: MultipartFile,
    @Valid @RequestPart("video_attributes") attributes: CreatePostWithContentCommand,
    request: HttpServletRequest
): ResponseEntity<PostDto>

My Controller decides based on the Request-Paramter which method to call. The methods do various different things with the underlying image / video file. In the MockMultipartHttpServletRequestBuilder class of version 5.3.3 is now an additional check if the Content-Type of my Part file (video_attributes) is compatible to 'text/html', which it is not. Because of that method toParameterValue() my Parts name is not added to the parameter collection anymore. As I said, with Postman everything works.
Can you please elaborate on that for me? Is there a way to make the test work again? I would rather not change my endpoint just to get the test working, especially by dividing the endpoint or using an additional query parameter this makes my API less efficient.

Thanks in advance, looking forward to your response.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions