elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

Json params raises as InvalidFormat exception #61

Closed retgoat closed 7 years ago

retgoat commented 7 years ago

Hi there! I need to pass some unstructured json document as content_type param like this:

{
    "content_type": {
        "a": 1
    }
}

content_type can be any json with any fields. When Im using Map as param type

    params do
      requires :content_type, type: Map
    end

Then params parses as empty map %{}. But if I set param type to Json, %Maru.Exceptions.InvalidFormat I'm getting %Maru.Exceptions.InvalidFormat. Here is the code:

    params do
      requires :content_type, type: Json
    end

    post do
      Logger.info(inspect(params))

      case Geronimo.ContentType.create(params[:content_type]) do
        {:ok, resp} -> respond_with(conn, resp)
        {:err, err} -> respond_with(conn, err, 422)
      end
    end

The main question is: how can I allow to push any json document as content_type parameter? Thanks in advance!

falood commented 7 years ago

you can try to make a custom type Any like this:

defmodule Maru.Types.Any do
  use Maru.Type

  def parse(input, _), do: input
end

and set type: Any

will make Any a internal type 🙂

retgoat commented 7 years ago

@falood thanks man!

retgoat commented 7 years ago

@falood Maybe it deserves to be included in maru? Just in case.

falood commented 7 years ago

map and list without do block will return the original value now, I think it's better than Any type.

here's an example: https://github.com/elixir-maru/maru/blob/c804d517b2e04f13f76aa69a262a1342c644a064/test/builder/route_test.exs#L84-L112