phiz71 / vertx-swagger

Swagger integration in Eclipse Vert.X world. A dynamic Vert.X router, configured with a swagger file and a swagger-codegen plugin to generate a server stub.
Apache License 2.0
86 stars 35 forks source link

File upload not working #93

Closed vaIgarashi closed 6 years ago

vaIgarashi commented 6 years ago

It's not possible to read image from multipart/form-data.

Swagger:

   "/map/background/": {
      "post": {
        "operationId": "createMap",
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "name": "background",
            "in": "formData",
            "required": true,
            "type": "file"
          },
          {
            "$ref": "#/parameters/Coords"
          }
        ],
      }
    }

Request:

Content-Disposition: form-data; name="coords"

[[61.79849228683366,34.33080877304078,0],[61.79849228683366,34.411638793945315,0],[61.77514442095853,34.411638793945315,0],[61.77514442095853,34.33080877304078,0]]
------WebKitFormBoundary1gK4Tb8jEjgsiWy1
Content-Disposition: form-data; name="background"; filename="cat.png"
Content-Type: image/png

------WebKitFormBoundary1gK4Tb8jEjgsiWy1--

Exception: java.lang.ClassCastException: java.lang.String cannot be cast to io.vertx.core.json.JsonObject

It's try parse file-uploads/c54807b6-ffa2-44da-be50-6424ab179cad string as File.

vaIgarashi commented 6 years ago

Also with current aproach it's not possible to obtain original file name.

vaIgarashi commented 6 years ago

To fix it should be at least like that:

File background = new File(message.body().getString("background"));

instead of:

File background = Json.mapper.readValue(message.body().getJsonObject("background").encode(), File.class);
vaIgarashi commented 6 years ago

Small patch to fix this:


IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- modules/vertx-swagger-codegen/src/main/resources/javaVertXServer/apiVerticle.mustache   (revision 354448ba45b13c7c1ffc3d8bd8c99e5edd00982e)
+++ modules/vertx-swagger-codegen/src/main/resources/javaVertXServer/apiVerticle.mustache   (date 1513845883000)
@@ -73,7 +73,12 @@
                     {{/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}}```
phiz71 commented 6 years ago

Hi. I have been very busy these past two months. But now, I have time again for the project :) I will take a look soon.

cburnicki commented 6 years ago

I downloaded the current v1.6 from maven central and although it seems to contain this fix, code generation still spits out the wrong line for the petstore.json: File file = Json.mapper.readValue(message.body().getJsonObject("file").encode(), File.class);

My sample code is in a github repo: https://github.com/cburnicki/vertx-file-upload-test I have no idea why this is still happening.