I have an API that uses multipart/form-data, it will receive an image binary and a payload in json.
Here is how the API is defined:
OpenApiSpex.schema(%{
type: :object,
properties: %{
image: %Schema{type: :string, format: :binary},
payload: %Schema{
type: :object,
properties: %{
...
}
}
}
})
And there is the controller spec:
operation :simple,
summary: "Something",
request_body:
{"Params", "multipart/form-data", __MODULE__.MySchema, required: true},
responses: [
ok: ...
]
I also have cast and validation enabled in my controller:
plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true
Then, I try to send a request via CURL:
curl -X POST http://localhost:4000/api/qr_code/simple \
-H "Accept: application/json" \
-F "file=@logo.svg;type=image/svg+xml" \
-F 'payload={
"userId": "12345",
"description": "Example SVG upload",
"tags": ["icon", "vector"]
}'
NOTE: I also tried the same request with Req, giving the same reply.
What I expected is to receive in my controller is a struct containig both the image in a Plug.Upload struct and the payload in json format.
But calling that API will return the following error:
%{
"errors" => [
%{
"detail" => "Invalid object. Got: string",
"source" => %{"pointer" => "/payload"},
"title" => "Invalid value"
}
]
}
Which indicates that the payload value is being returned as a string, not as a decoded json.
I disabled the cast and validation plug in my controller and sure enough, this is what I'm receiving:
%{
"file" => %Plug.Upload{
path: "/tmp/plug-1684-gCHg/multipart-1765843491-22636011688-4",
content_type: "image/svg+xml",
filename: "logo.svg"
},
"payload" => "{\n \"userId\": \"12345\",\n \"description\": \"Example SVG upload\",\n \"tags\": [\"icon\", \"vector\"]\n }"
}
So, my question is, shouldn't OpenApiSpex cast identify that the payload in this case is a string and decode it using JSON?
Is this a bug or an I doing something wrong with my schema?
I have an API that uses
multipart/form-data, it will receive an image binary and a payload in json.Here is how the API is defined:
And there is the controller spec:
I also have cast and validation enabled in my controller:
Then, I try to send a request via CURL:
NOTE: I also tried the same request with Req, giving the same reply.
What I expected is to receive in my controller is a struct containig both the
imagein aPlug.Uploadstruct and thepayloadin json format.But calling that API will return the following error:
Which indicates that the
payloadvalue is being returned as astring, not as a decodedjson.I disabled the cast and validation plug in my controller and sure enough, this is what I'm receiving:
So, my question is, shouldn't OpenApiSpex cast identify that the payload in this case is a string and decode it using JSON?
Is this a bug or an I doing something wrong with my schema?