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

json encoder error for List[String] parameter #34

Open
danielkirch opened this issue Jun 15, 2020 · 1 comment
Open

json encoder error for List[String] parameter #34

danielkirch opened this issue Jun 15, 2020 · 1 comment

Comments

@danielkirch
Copy link

Hello,

thanks for developing maru and maru_swagger.
I am currently trying to add a swagger documentation for my maru web service and I am struggling with the following error:

16:28:05.906 [error] #PID<0.488.0> running MaruSwaggerTest.Api (connection #PID<0.487.0>, stream id 1) terminated
Server: 0.0.0.0:3456 (http)
Request: GET /swagger
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for {:list, "string"} of type Tuple, Jason.Encoder protocol must always be explicitly implemented. This protocol is implemented for the following type(s): Date, BitString, Jason.Fragment, Any, Map, NaiveDateTime, List, Integer, Time, DateTime, Decimal, Atom, Float
        (jason) lib/jason.ex:150: Jason.encode!/2
        (maru_swagger) lib/maru_swagger/plug.ex:12: MaruSwagger.Plug.call/2
        (maru_compile) lib/api.ex:1: anonymous fn/1 in MaruSwaggerTest.Api.call/2
        (plug_cowboy) lib/plug/cowboy/handler.ex:12: Plug.Cowboy.Handler.init/2
        (cowboy) deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy) deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
        (cowboy) deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Apparently, Swagger is trying to encode the tuple {:list, "string"} into a json and that does not work. My api file looks like this:

defmodule MaruSwaggerTest.Api do
  use MaruSwaggerTest.Server
  use MaruSwagger

  plug Plug.Logger

  swagger(
    at: "/swagger",
    pretty: true,
  
    swagger_inject: [
      host: "myapp",
      basePath: "/api",
      schemes:  [ "https" ],
      consumes: [ "application/json" ],
      produces: [ "application/json" ]
    ])

  params do
    optional :foo, type: List[String]
  end
  get "/" do
    IO.inspect(params[:foo])

    conn
    |> send_resp(200, "hello world")
    |> halt()
  end
end

The error occurs at runtime when I call the /swagger endpoint.
Is this a bug or am I missing something here?
What would be a possible workaround?

@danielkirch
Copy link
Author

Hello again,

I think the issue is that List[String] is not supported as a parameter type for get requests as of now. I created a pull request where I try to address this issue.

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

No branches or pull requests

1 participant