swagger-api/swagger-codegen

[Scala] Error Uploading File as Multipart Form

Open

#5,855 创建于 2017年6月15日

在 GitHub 查看
 (4 评论) (0 反应) (0 负责人)HTML (5,474 fork)batch import
Client: ScalaIssue: Bughelp wanted

仓库指标

Star
 (12,701 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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.

贡献者指南