-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multipart/form-data validation failing due to Plug.Conn.Query.decode #441
Comments
I've updated to remove requirement for changing properties to support string, as the core issue here is that multipart fields that are arrays are parsed to remove brackets in field names by |
@MrManicotti, as workaround I have created plug which splits specified parameters by pattern and updates both conn.body_params and conn.params:
Call it before OpenApiSpex.Plug.CastAndValidate
|
I'm not quite sure this resolves the issue with validation here, as the problem specifically is in regards to what field name gets validated by
params: %{
"array_items[]" => [
%Plug.Upload{
content_type: "application/pdf",
filename: "empty_pdf_1.pdf",
path: "/var/folders/lx/8gvgf9jd24x6mcc11g747j500000gn/T//plug-1648/multipart-1648673751-336866617448758-3"
},
%Plug.Upload{
content_type: "application/pdf",
filename: "empty_pdf_2.pdf",
path: "/var/folders/lx/8gvgf9jd24x6mcc11g747j500000gn/T//plug-1648/multipart-1648673751-11087878838975-3"
}
]
} to params: %{
"array_items" => [
%Plug.Upload{
content_type: "application/pdf",
filename: "empty_pdf_1.pdf",
path: "/var/folders/lx/8gvgf9jd24x6mcc11g747j500000gn/T//plug-1648/multipart-1648673751-336866617448758-3"
},
%Plug.Upload{
content_type: "application/pdf",
filename: "empty_pdf_2.pdf",
path: "/var/folders/lx/8gvgf9jd24x6mcc11g747j500000gn/T//plug-1648/multipart-1648673751-11087878838975-3"
}
]
} The later is correct and what's expected conventionally, however there's a problem with open-api-spex's validation. The issue is two-fold really:
{
"errors": [
{
"detail": "Missing field: array_items[]",
"source": {
"pointer": "/array_items[]"
},
"title": "Invalid value"
}
]
}
I believe to resolve this we may need to pass field names through the decoder prior to validation, so that |
I've got a hacky solution in the meantime to fix the spec prior to validation:
I had to create my own cast_and_validate plug and inject this modified spec |
A little backstory - operations which contain a
multipart/form-data
request_body , which within themselves contain an array field don't get sent as arrays via SwaggerUI.curl
is being generated without appending to the array field:This generates the following Swagger UI:
Note the
curl
here.array_items
needs to bearray_items[]
in order for Plug to create an array ofPlug.Upload
constructs. Without this, the conn object contains:This is a known issue with swagger. The solution has been to add brackets to the name of the fields so that curl interpolates currently. See https://stackoverflow.com/questions/53213067/swagger-ui-open-api-3-multipart-form-data-array-problem
Taking that curl from the example above and modifying to
array_items[]
creates the array as expected:To resolve this, I've got a hacky solution that involves changing the field name to include brackets:
But downstream, validations will fail with
required
fields for:"array_items[]"
due to the way this ends up getting interpolated.Plug.Conn.Query.decode
cuts out brackets. If I end up just putting:array_items
in the required field, I won't be able to submit my request in Swagger due to missing a required field (Swagger sees array_items[] not array_items)Ideally validations follow the conventions for removing brackets on field names for
multipart
data. Ergo, ifrequired: [:"array_items[]"]
is set, on validation it will be checked againstarray_items
EDIT: It's
Plug.Conn.Query.decode
that cuts out brackets from field names.https://hexdocs.pm/plug/Plug.Conn.Query.html#content
The text was updated successfully, but these errors were encountered: