zerocode icon indicating copy to clipboard operation
zerocode copied to clipboard

ClassCastException Thrown when referring json file in request body with content type "multipart/form-data"

Open srinivasa78 opened this issue 5 years ago • 7 comments

Issue:

With content-type:"multipart/form-data" getting java.lang.ClassCastException when referring json file for additional payload in Request Body apart from "files": [ "file:media/test.wav" ]

Request step:

{
  "name": "save_config",
  "url": "/config",
  "operation": "POST",
  "request": {
    "headers": "${JSON.FILE:standard_headers_media.json}",
    "body": {
      "files": [
        "file:media/test.wav"
      ],
      "metadata": "${JSON.FILE:metadata.json}"
    }
  },
  "assertions": {
    "status": 201,
    "headers": {
      "${correlationId.header}": "$NOT.NULL"
    }
  }
}

metadata.json

{
  "name": "name",
  "fileName": "test.wav",
  "location": "location",
}

standard_headers_media.json:

{
  "Content-Type": "multipart/form-data"
}

Error:

java.lang.RuntimeException: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String
 (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')

Stactrace:

 Caused by: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String
  (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')
	at org.jsmart.zerocode.core.httpclient.utils.FileUploadUtils.buildOtherRequestParams(FileUploadUtils.java:60)
	at org.jsmart.zerocode.core.httpclient.BasicHttpClient.createFileUploadRequestBuilder(BasicHttpClient.java:356)
	at org.jsmart.zerocode.core.httpclient.BasicHttpClient.createRequestBuilder(BasicHttpClient.java:400)
	at org.jsmart.zerocode.core.httpclient.BasicHttpClient.execute(BasicHttpClient.java:126)
	at org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl.executeRESTInternal(JsonServiceExecutorImpl.java:131)
	at org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl.executeRESTService(JsonServiceExecutorImpl.java:92)

srinivasa78 avatar Jun 09 '20 09:06 srinivasa78

@srinivasa78 , just checking... have you tried this(below replacing the external content) in a manual testing tool or a REST client like Postman?

"body": {
      "files": [
        "file:media/test.wav"
      ],
      "metadata": {
           "name": "name",
           "fileName": "test.wav",
          "location": "location",
       }
    }

What does the API server responds with?

authorjapps avatar Jun 09 '20 14:06 authorjapps

@authorjapps My apologies for responding late. Getting 201 Created response. Please let me know if it's need to be fixed or something. I want to contribute to this project. And though I got experience this would be my first open source contribution. (I know I know I am too late :) )

srinivasa78 avatar Jun 18 '20 15:06 srinivasa78

Thanks @srinivasa78 for your ineterest in contribution. Yes, you can try to fix this.

The code is here needs fix:

org.jsmart.zerocode.core.httpclient.utils.FileUploadUtils#buildOtherRequestParams

Please raise a PR once it's stable.

Do you know how to fork etc? Otherwise, here are the guidelines - CONTRIBUTING.md(Click)

Also please join Slack(click to join) channel if you haven't already!

authorjapps avatar Jun 20 '20 10:06 authorjapps

@srinivasa78, did you have a chance to look at it?

authorjapps avatar Jul 30 '20 21:07 authorjapps

Forked the repo, started working on this. Will raise PR when build is stable.

rebugged avatar Sep 26 '20 09:09 rebugged

@authorjapps Raised a PR, please let me know if I need to add this specific scenario in the test cases for HTTP. Check https://github.com/authorjapps/zerocode/pull/435

rebugged avatar Sep 27 '20 18:09 rebugged

@authorjapps reminder! the PR has been pending for a while. Please can you help to merge it.

rebugged avatar Oct 04 '20 05:10 rebugged

PR merged to master. This fix is available in 1.3.36(and higher release versions) when released.

authorjapps avatar Jan 22 '24 16:01 authorjapps