Skip to content

Commit c091fba

Browse files
author
bnasslahsen
committed
Added Support for @RequestParam for file upload #377 - Fixes #377
1 parent 6d5380f commit c091fba

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractParameterBuilder.java

+16
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ private boolean isRequestBodySchema(RequestBodyInfo requestBodyInfo) {
253253

254254
protected abstract boolean isFile(JavaType ct);
255255

256+
public boolean isFile(java.lang.reflect.Parameter parameter) {
257+
boolean result =false;
258+
Type type = parameter.getParameterizedType();
259+
JavaType javaType = constructType(parameter.getType());
260+
if (isFile(javaType)) {
261+
result =true;
262+
}
263+
else if (type instanceof ParameterizedType) {
264+
ParameterizedType parameterizedType = (ParameterizedType) type;
265+
if (isFile(parameterizedType)) {
266+
result =true;
267+
}
268+
}
269+
return result;
270+
}
271+
256272
<A extends Annotation> A getParameterAnnotation(HandlerMethod handlerMethod,
257273
java.lang.reflect.Parameter parameter, int i, Class<A> annotationType) {
258274
A parameterDoc = AnnotationUtils.getAnnotation(parameter, annotationType);

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private Parameter buildParams(ParameterInfo parameterInfo, Components components
262262
parameter = buildParam(parameterInfo, components, requestInfo, jsonView);
263263

264264
}
265-
else if (requestParam != null) {
265+
else if (requestParam != null && !parameterBuilder.isFile(parameterInfo.getParameter())) {
266266
boolean isOptional = Optional.class.equals(parameters.getType());
267267
requestInfo = new RequestInfo(ParameterType.QUERY_PARAM, requestParam.value(), requestParam.required() && !isOptional,
268268
requestParam.defaultValue());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app63.toto;
20+
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
@RestController
25+
public class helloController2 {
26+
27+
@GetMapping("/test1")
28+
public void test1(String hello) {
29+
}
30+
31+
}

Diff for: springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app11/HelloController.java

+11
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020

2121
import java.util.List;
2222

23+
import org.springframework.http.MediaType;
2324
import org.springframework.http.ResponseEntity;
2425
import org.springframework.web.bind.annotation.PostMapping;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RequestMethod;
28+
import org.springframework.web.bind.annotation.RequestParam;
2529
import org.springframework.web.bind.annotation.RequestPart;
30+
import org.springframework.web.bind.annotation.ResponseBody;
2631
import org.springframework.web.bind.annotation.RestController;
2732
import org.springframework.web.multipart.MultipartFile;
2833

@@ -34,4 +39,10 @@ public ResponseEntity<String> uploadDocuments(@RequestPart("doc") List<Multipart
3439
return null;
3540
}
3641

42+
@RequestMapping(value = "/tracks", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
43+
public @ResponseBody
44+
String postTrack(@RequestParam("file") MultipartFile file) {
45+
return "redirect:/";
46+
}
47+
3748
}

Diff for: springdoc-openapi-webmvc-core/src/test/resources/results/app11.json

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111
}
1212
],
1313
"paths": {
14+
"/tracks": {
15+
"post": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "postTrack",
20+
"requestBody": {
21+
"content": {
22+
"multipart/form-data": {
23+
"schema": {
24+
"type": "object",
25+
"properties": {
26+
"file": {
27+
"type": "string",
28+
"format": "binary"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
},
35+
"responses": {
36+
"200": {
37+
"description": "default response",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"type": "string"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
1449
"/documents": {
1550
"post": {
1651
"tags": [

0 commit comments

Comments
 (0)