Git Product home page Git Product logo

Comments (4)

neslinesli93 avatar neslinesli93 commented on August 17, 2024 3

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.

mbuhot avatar mbuhot commented on August 17, 2024

@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.

neslinesli93 avatar neslinesli93 commented on August 17, 2024

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.

mbuhot avatar mbuhot commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.