Skip to content
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

Add failing test for multipart array parameters #630

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stevehodgkiss
Copy link

@stevehodgkiss stevehodgkiss commented Aug 19, 2024

Adds a failing spec for the issue described in #441

  1) test query params - basics handles multipart/form-data array parameters (OpenApiSpex.Plug.CastTest)
     test/plug/cast_test.exs:54
     match (=) failed
     code:  assert %{"data" => ["file1.txt", "file2.txt"]} = body
     left:  %{"data" => ["file1.txt", "file2.txt"]}
     right: %{"errors" => [%{"detail" => "Missing field: files[]", "source" => %{"pointer" => "/files[]"}, "title" => "Invalid value"}]}
     stacktrace:
       test/plug/cast_test.exs:79: (test)

Array parameters need to be supplied with [] on the end of the parameter name, because plug won't parse them into lists without it:

iex> Plug.Conn.Query.decode("files=1&files=2")
%{"files" => "2"}
iex> Plug.Conn.Query.decode("files[]=1&files[]=2")
%{"files" => ["1", "2"]}

This means the key for the openapi spec needs to have [] at the end of array parameters, otherwise clients generated from the openapi.json file will be using the incorrect name in requests (i.e. files instead of files[]).

Plug's param parsing happens before the CastAndValidate plug, so the plug sees files where the user actually provided files[] in the request.

How can this be handled?

One option could be to add a parsed_param_name option to Schema, and use that to cast/validate. Users would then have to provide parsed_param_name: "files" in this scenario. Maybe there's a way to automatically derive the parsed name from the key name.

Another option is to strip [] from the end of key names in the cast/validate plug. This is what the solution provided by @christmoore is doing in #441 (comment) (thanks for that @christmoore!). It wouldn't be possible to support replace_params: true with this approach though.


plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true, replace_params: false

@doc request_body: {"Files", "multipart/form-data", Schemas.UploadRequest},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this could be handled by setting the encoding on the mediaType, something like:

{"Files",
          %{
            "multipart/form-data" => [
              encoding: %{"files" => %OpenApiSpex.Encoding{style: "deepObject"}}
            ]
          }, Schemas.UploadRequest},

However it didn't seem to have any effect on the generated curl commands or request body sent from the swagger UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants