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

File upload not working #93

Closed
vaIgarashi opened this issue Dec 15, 2017 · 5 comments
Closed

File upload not working #93

vaIgarashi opened this issue Dec 15, 2017 · 5 comments
Milestone

Comments

@vaIgarashi
Copy link

vaIgarashi commented Dec 15, 2017

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
Copy link
Author

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

@vaIgarashi
Copy link
Author

vaIgarashi commented Dec 21, 2017

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
Copy link
Author

vaIgarashi commented Dec 21, 2017

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
Copy link
Owner

phiz71 commented Jan 5, 2018

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.

petebuletza added a commit to petebuletza/vertx-swagger that referenced this issue Feb 9, 2018
phiz71 pushed a commit that referenced this issue Feb 10, 2018
Thanks @petebuletza  for this PR and thanks for the unit-test.
@phiz71 phiz71 added this to the v1.6.0 milestone Feb 10, 2018
@cburnicki
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants