Comments (4)
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
from open_api_spex.
@neslinesli93 what error response do you get from the CURLs and what error logging output do you get from the server?
from open_api_spex.
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...
from open_api_spex.
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][email protected];type=image/jpeg;filename=test1' \
-F 'file_0[name]=Name1' \
-F 'file_0[date]=2024-01-01' \
-F 'file_1[file][email protected];type=image/png;filename=test2' \
-F 'file_1[name]=Name2' \
-F 'file_1[date]=2024-02-01' \
from open_api_spex.
Related Issues (20)
- Is it possible to pretty print error responses? HOT 3
- Clash of different schemas with the same title
- Params by plug convention should contain merged path, body and query parameters HOT 1
- AllOf cast returns a map, but I expected a struct HOT 3
- How to pass dynamic key-value pairs in the query
- Problem with nested oneOf in allOf HOT 2
- Nullable and patern HOT 1
- Default JsonErrorResponse schema references out-of-date name for HTTP 422 HOT 1
- New release for ymlr v5 support? HOT 4
- Accepts non-RFC3339 timestamps in date-time HOT 1
- Accept a nonce for SwaggerUI to allow strict CSPs HOT 1
- Custom error messages HOT 1
- Cast number schema should return float HOT 1
- Response's content property spec seems reversed HOT 1
- Add a `--check` flag to `mix openapi.spec.json` and `mix openapi.spec.yaml` HOT 4
- spec not implemented for all version example function HOT 1
- infinity recursion
- Property with type array in Schema mentioned in allOf not properly casted to struct
- Recursive schema not handled correctly? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from open_api_spex.