Skip to content
This repository was archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Fixes Issue #93 - File upload not working (#98)
Browse files Browse the repository at this point in the history
Thanks @petebuletza  for this PR and thanks for the unit-test.
  • Loading branch information
petebuletza authored and phiz71 committed Feb 10, 2018
1 parent 5e3e458 commit 8836475
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ public class {{classname}}Verticle extends AbstractVerticle {
{{/isString}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isFile}}
{{{dataType}}} {{paramName}} = new {{{dataType}}}(message.body().getString("{{baseName}}"));
{{/isFile}}
{{^isFile}}
{{{dataType}}} {{paramName}} = Json.mapper.readValue(message.body().getJsonObject("{{baseName}}").encode(), {{{dataType}}}.class);
{{/isFile}}
{{/isPrimitiveType}}
{{/isListContainer}}
{{/allParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ public void testUUID() throws IOException {
FileUtils.deleteDirectory(new File("temp"));
}

@Test
public void testFile() throws IOException {
String[] args = new String[7];
args[0] = "generate";
args[1] = "-l";
args[2] = "java-vertx";
args[3] = "-i";
args[4] = "testFile.json";
args[5] = "-o";
args[6] = "temp/test-server";
SwaggerCodegen.main(args);

File testApiVerticleFile = new File(
"temp/test-server/src/main/java/io/swagger/server/api/verticle/TestApiVerticle.java");

Assert.assertTrue(FileUtils.readFileToString(testApiVerticleFile).contains(
"File file = new File(message.body().getString(\"file\"));"));

Assert.assertFalse(FileUtils.readFileToString(testApiVerticleFile).contains(
"File file = Json.mapper.readValue(message.body().getJsonObject(\"file\").encode(), File.class);"));

FileUtils.deleteDirectory(new File("temp"));
}

@Test
public void testImplGenWithoutImplDefault() throws IOException {
String[] args = new String[7];
Expand Down
45 changes: 45 additions & 0 deletions modules/vertx-swagger-codegen/src/test/resources/testFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "test MainApiVerticle option"
},
"host": "localhost",
"schemes": [
"http"
],
"paths": {
"/file/{originalFileName}": {
"post": {
"tags": [
"Test"
],
"summary": "test generator options",
"operationId": "dummy",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "file",
"in": "formData",
"description": "file to upload",
"required": false,
"type": "file"
}
],
"responses": {
"default": {
"schema":{
"type":"string"
},
"description": "OK"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public class PetApiVerticle extends AbstractVerticle {
User user = SwaggerRouter.extractAuthUserFromMessage(message);
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
String additionalMetadata = message.body().getString("additionalMetadata");
File file = Json.mapper.readValue(message.body().getJsonObject("file").encode(), File.class);
File file = new File(message.body().getString("file"));
service.uploadFile(petId, additionalMetadata, file, io.vertx.rxjava.ext.auth.User.newInstance(user)).subscribe(
verticleHelper.getRxResultHandler(message, true, new TypeReference<ModelApiResponse>(){}),
verticleHelper.getErrorAction(message, UPLOADFILE_SERVICE_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public class PetApiVerticle extends AbstractVerticle {
User user = SwaggerRouter.extractAuthUserFromMessage(message);
Long petId = Json.mapper.readValue(message.body().getString("petId"), Long.class);
String additionalMetadata = message.body().getString("additionalMetadata");
File file = Json.mapper.readValue(message.body().getJsonObject("file").encode(), File.class);
File file = new File(message.body().getString("file"));
service.uploadFile(petId, additionalMetadata, file, user, verticleHelper.getAsyncResultHandler(message, UPLOADFILE_SERVICE_ID, true, new TypeReference<ModelApiResponse>(){}));

} catch (Exception e) {
Expand Down

0 comments on commit 8836475

Please sign in to comment.