Skip to content

Commit

Permalink
fix #308 - fix processing file upload in operations with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Aug 28, 2019
1 parent e11dfda commit 428d98d
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,12 @@ public Response apply(ContainerRequestContext ctx) {

if (argument instanceof Object[]){
Object[] args2 = (Object[]) argument;
args2[0]= args[0];
// populate with request context and any other parameters
for (int ii = 0; ii < args.length; ii++) {
if (args[ii] != null) {
args2[ii]= args[ii];
}
}
args = args2;
}else {
args[i] = argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public Object process(MediaType mediaType, InputStream entityStream, Class<?> c
return argument;

}else if (mediaType.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)){
int i = 1;
int i =
(controller
.getOperation()
.getParameters() == null || controller
.getOperation().getParameters().isEmpty())? 1 : 1 + controller
.getOperation().getParameters().size();
// get the boundary
String boundary = mediaType.getParameters().get("boundary");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.List;

public class TestController {
public ResponseContext uploadFile(RequestContext request, File inputFile, String stringMetadata, Integer integerMetadata) {
public ResponseContext uploadFile(RequestContext request, File inputFile, String stringMetadata, Integer intMetadata) {
if(inputFile != null) {
stringMetadata += ": " + String.valueOf(inputFile.length());
}
Expand All @@ -45,6 +45,27 @@ public ResponseContext uploadFile(RequestContext request, File inputFile, String
.entity(stringMetadata);
}

public ResponseContext uploadFilePathParam(RequestContext request, String testId, File inputFile) {
if(inputFile != null) {
testId += ": " + String.valueOf(inputFile.length());
}

return new ResponseContext()
.status(200)
.entity(testId);
}

public ResponseContext uploadFilePathParamQueryParam(RequestContext request, String testId, String testId2, String queryId, File inputFile, String stringMetadata, Integer intMetadata) {
if(inputFile != null) {
testId += ": " + String.valueOf(inputFile.length());
}

return new ResponseContext()
.status(200)
.entity(testId + " " + testId2 + " " + queryId + " " + stringMetadata + " " + intMetadata);
}


public ResponseContext inlineRequiredBody(RequestContext request, com.fasterxml.jackson.databind.JsonNode inlineBody) {
return new ResponseContext()
.status(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import static org.testng.Assert.assertEquals;

Expand Down Expand Up @@ -78,6 +79,90 @@ public void verifyFileUpload() throws Exception {
}
}

@Test
public void verifyFileUploadWithPathParam() throws Exception {
File file = null;
try {
String path = "/fileUploadPathParam/TESTID/content";
file = File.createTempFile("inflector-test-", ".tmp");

PrintWriter writer = new PrintWriter(file);
writer.println("The first line");
writer.println("The second line");
writer.close();

file = new File(file.getPath());

final FileDataBodyPart filePart = new FileDataBodyPart("theFile", file);

final MultiPart multipart = new FormDataMultiPart()
.bodyPart(filePart);

Entity<?> formParams = Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA_TYPE);

String str = client.invokeAPI(
path, // path
"POST", // method
new HashMap<>(), // query params
null, // body
new HashMap<>(), // header params
formParams, // form params
"multipart/form-data", // accept
null, // content-type
new String[0]); // auth names
assertEquals(str, "TESTID: " + file.length());
}
finally {
if(file != null) {
file.delete();
}
}
}

@Test
public void verifyFileUploadWithManyParam() throws Exception {
File file = null;
try {
String path = "/fileUploadPathParamQueryParam/TESTID/content/TESTID2/content";
file = File.createTempFile("inflector-test-", ".tmp");

PrintWriter writer = new PrintWriter(file);
writer.println("The first line");
writer.println("The second line");
writer.close();

file = new File(file.getPath());

final FileDataBodyPart filePart = new FileDataBodyPart("theFile", file);

final MultiPart multipart = new FormDataMultiPart()
.field("stringMetadata", "bar")
.field("intMetadata", "1")
.bodyPart(filePart);

Entity<?> formParams = Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA_TYPE);

Map<String, String> queryParams = new HashMap<>();
queryParams.put("queryId", "QUERYID");
String str = client.invokeAPI(
path, // path
"POST", // method
queryParams, // query params
null, // body
new HashMap<>(), // header params
formParams, // form params
"multipart/form-data", // accept
null, // content-type
new String[0]); // auth names
assertEquals(str, "TESTID: " + file.length() + " TESTID2 QUERYID bar 1");
}
finally {
if(file != null) {
file.delete();
}
}
}

@Test
public void verifyMultipleMediaTypeFileUpload() throws Exception {
File file = null;
Expand Down
53 changes: 53 additions & 0 deletions src/test/swagger/oas3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,59 @@ paths:
intMetadata:
type: integer
format: int32
"/fileUploadPathParam/{testId}/content":
post:
x-swagger-router-controller: TestController
operationId: uploadFilePathParam
parameters:
- name: testId
in: path
required: true
schema:
type: string
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
theFile:
type: string
format: binary
"/fileUploadPathParamQueryParam/{testId}/content/{testId2}/content":
post:
x-swagger-router-controller: TestController
operationId: uploadFilePathParamQueryParam
parameters:
- name: testId
in: path
required: true
schema:
type: string
- name: testId2
in: path
required: true
schema:
type: string
- name: queryId
in: query
required: true
schema:
type: string
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
theFile:
type: string
format: binary
stringMetadata:
type: string
intMetadata:
type: integer
format: int32
"/primitiveBody/binary":
post:
x-swagger-router-controller: TestController
Expand Down

0 comments on commit 428d98d

Please sign in to comment.