swagger-api / swagger-api/swagger-codegen

[Scala] Error Uploading File as Multipart Form

Open
#5,855 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Scala help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

For one of our endpoints that requires file upload, we are seeing errors with the multipart content type.

Model of Endpoint:

"post": {
        "description": "Uploads a file.",
        "consumes": ["multipart/form-data"],
        "parameters": [{
          "name": "file",
          "in": "formData",
          "description": "Uploaded file",
          "type": "file",
          "required": true
        }],
        "summary": "File Upload",
        "responses": {
              ...
        }
      }

Stack Trace:

Cannot get entity on a MultiPart instance (through reference chain: com.sun.jersey.multipart.FormDataMultiPart["entity"])
com.fasterxml.jackson.databind.JsonMappingException: Cannot get entity on a MultiPart instance (through reference chain: com.sun.jersey.multipart.FormDataMultiPart["entity"])
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:348)
	at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:343)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:698)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
	at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3681)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3057)
	at io.swagger.client.ApiInvoker.serialize(ApiInvoker.scala:107)
	at io.swagger.client.ApiInvoker.invokeApi(ApiInvoker.scala:145)
	

The line in ApiInvoker which is causing the error is contained in the serialize method below

 def serialize(obj: AnyRef): String = {
    if (obj != null) {
      obj match {
        case e: List[_] => mapper.writeValueAsString(obj.asInstanceOf[List[_]].asJava)
        case _ => mapper.writeValueAsString(obj)  // <-- ERROR occurs here
      }
    } else null
  }
Swagger-codegen version

We have tested on the most recent release of 2.2, as well as building our own jar from the master branch at commit 802641bf919225cad6c924f681942576982d3d3a (May 21). Also, have checked out the 2.3 branch to see if this is fixed, but find the Scala client to be very reorganized and things like auth header functionality to be missing.

Swagger declaration file content or url
"post": {
        "description": "Uploads a file.",
        "consumes": ["multipart/form-data"],
        "parameters": [{
          "name": "file",
          "in": "formData",
          "description": "Uploaded file",
          "type": "file",
          "required": true
        }],
        "summary": "File Upload",
        "responses": {
              ...
        }
      }

Command line used for generation

Not command line, but rather an SBT build time dependency on the jar.

SwaggerCodegen.main("generate -l io.swagger.codegen.languages.ScalaClientCodegen -i apidocs.json"
Steps to reproduce
  1. Generate a scala api client from a declaration file containing an endpoint specifying parameters type:file, in:formData, and consumes:['multipart/form-data'] as above.

  2. Write a test of the generated code that attempts to upload a File object. api.method(file)

  3. Observe that the request does not even get made, serialization of the body fails in the ApiInvoker.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ApiInvoker.scala and its serialize method, then reproduce the generated Scala client request using the multipart/form-data declaration and a File object. The work is done when the upload request reaches the endpoint without the Jackson serialization failure shown in the stack trace.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.