Skip to content

Fixes so that a RequestPart with a Map is added to the RequestBody #3051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class ParameterInfo {
private String paramType;

/**
* if the paramater type is RequestPart
* if the parameter type is RequestPart
*/
private boolean requestPart;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ public boolean isParamToIgnore(MethodParameter parameter) {
return false;
if (isRequestBodyWithMapType(parameter))
return false;
if (isRequestPartWithMapType(parameter))
return false;
return isRequestTypeToIgnore(parameter.getParameterType());
}

Expand Down Expand Up @@ -510,6 +512,23 @@ private boolean isRequestBodyWithMapType(MethodParameter parameter) {
return Map.class.isAssignableFrom(parameterType);
}

/**
* Is request part with map type
*
* @param parameter the parameter
* @return the boolean
*/
private boolean isRequestPartWithMapType(MethodParameter parameter) {
// Check for @RequestPart annotation
org.springframework.web.bind.annotation.RequestPart requestPart = parameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestPart.class);
if (requestPart == null) {
return false;
}
// Check if the parameter type is assignable to Map
Class<?> parameterType = parameter.getParameterType();
return Map.class.isAssignableFrom(parameterType);
}

/**
* Sets params.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,52 @@

package test.org.springdoc.api.v30.app52;

import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
public class HelloController {

@PostMapping(value = "/test1/{username}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest1(@PathVariable String username, @RequestPart("test") MyTestDto test,
@RequestPart("image") MultipartFile imageFile) {
return null;
}
@PostMapping(value = "/test1/{username}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest1(
@PathVariable String username,
@RequestPart("test") MyTestDto test,
@RequestPart("image") MultipartFile imageFile) {
return null;
}

@PostMapping(value = "/test2/{username}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest2(
@PathVariable String username,
@RequestPart("image") MultipartFile imageFile,
@RequestPart("test") MyTestDto test,
@RequestHeader("My-Header") String workspaceId) {
return null;
}

@PostMapping(value = "/test2/{username}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest2(@PathVariable String username, @RequestPart("image") MultipartFile imageFile,
@RequestPart("test") MyTestDto test, @RequestHeader("My-Header") String workspaceId) {
return null;
}
@PostMapping(value = "/test3", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest3(
@RequestPart("test") MyTestDto test,
@RequestPart("doc") List<MultipartFile> multipartFiles) {
return null;
}

@PostMapping(value = "/test3", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest3(@RequestPart("test") MyTestDto test,
@RequestPart("doc") List<MultipartFile> multipartFiles) {
return null;
}
@PostMapping(value = "/test4", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String createTest4(
@RequestPart List<MultipartFile> multipartFiles,
@RequestPart Map<String, String> map) {
return null;
}

class MyTestDto {
public String object1;
class MyTestDto {
public String object1;

public String object2;
public String object2;

public String object3;
}
public String object3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,54 @@
}
],
"paths": {
"/test4": {
"post": {
"tags": [
"hello-controller"
],
"operationId": "createTest4",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"map",
"multipartFiles"
],
"type": "object",
"properties": {
"multipartFiles": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
},
"map": {
"type": "object",
"additionalProperties" : {
"type": "string"
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test3": {
"post": {
"tags": [
Expand Down