Closed neslinesli93 closed 2 weeks ago
@neslinesli93 what error response do you get from the CURLs and what error logging output do you get from the server?
If i send the curl without indexes, only one element is picked, so the parsed+casted+validated body looks like this:
%MyProject.Schemas.FilesInput{
files: [
%{
name: "Name2",
date: ~D[2024-02-01],
file: %Plug.Upload{
path: "/tmp/plug-1720-22TL/multipart-1720510488-280198958045-1",
content_type: "image/png",
filename: "test2"
}
}
]
}
Otherwise, if I send the curl with indexes, the request body on the plug side looks like this:
%{
"0" => %{"file" => "...", "name" => "...", "date" => "..."},
"1" => %{"file" => "...", "name" => "...", "date" => "..."}
}
and the server replies this:
{"errors":[{"title":"Invalid value","source":{"pointer":"/files"},"detail":"Invalid array. Got: object"}]}
The problems seems that Plug (or whatever) cannot understand that it's an array and not an object with indexes as keys...
Thanks @neslinesli93 this does appear to be a limitation of Plug. The tests for Plug.Parsers show multiple files can be uploaded as long as they have different parameter names (https://github.com/elixir-plug/plug/blob/9f67dd924c2809ab07b4a798a3c3e718c9da4107/test/plug/parsers_test.exs#L213)
You could try changing the array to an object schema with additionalProperties
for allowing any keys:
OpenApiSpex.schema(%{
description: "A file with content and metadata",
type: :object,
additionalProperties: %Schema{
type: :object,
properties: %{
file: %Schema{
type: :string,
format: :binary
},
name: %Schema{
type: :string
},
date: %Schema{
type: :string,
format: :date
}
},
required: [:file, :name, :date]
}
}, struct?: false)
Then upload with:
curl -X 'POST' -v \
'http://localhost:4002/files' \
-H 'Content-Type: multipart/form-data' \
-F 'file_0[file]=@file1.jpg;type=image/jpeg;filename=test1' \
-F 'file_0[name]=Name1' \
-F 'file_0[date]=2024-01-01' \
-F 'file_1[file]=@file2.png;type=image/png;filename=test2' \
-F 'file_1[name]=Name2' \
-F 'file_1[date]=2024-02-01' \
It worked amazingly, thank you so much! If you need any help (a PR to clarify this behavior, or add an example, whatever) please just ask. I'm closing the issue anyway, thank you again
Hi, I'm trying to use your library to describe and endpoint that accepts a POST request with an array of multipart/form-data values, including files, but I'm failing to achieve that. This is my elixir schema file:
and here is my controller:
I can't seem to get this to work with either this curl (no array index):
or the same command but with indexes in arrays:
Is there a way to do this with the Openapi 3.0 spec and your library? Thank you