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

protocol Jason.Encoder not implemented for %Plug.Upload #808

Closed
thiagogsr opened this Issue Jan 8, 2019 · 6 comments

Comments

Projects
None yet
3 participants
@thiagogsr
Copy link

thiagogsr commented Jan 8, 2019

Hello

I'm upgrading my Phoenix to 1.4 and it is raising error in controllers that I use %Plug.Upload{}:

** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %Plug.Upload{content_type: "jpg", filename: "bar.jpg", path: "/foo/bar.jpg"}, Jason.Encoder protocol must always be explicitly implemented.

Shouldn't it be implemented by the plug or we should define it in my app using Protocol.derive(Jason.Encoder, Plug.Upload)?

@OvermindDL1

This comment has been minimized.

Copy link

OvermindDL1 commented Jan 8, 2019

How would you implement Plug.Upload into a Jason.Encoder, it is a structure that is pointing to an entirely temporary file on the filesystem that will be wiped at the end of the request. I'm not sure how you would serialize that into JSON in a way that would make sense?

@josevalim

This comment has been minimized.

Copy link
Member

josevalim commented Jan 8, 2019

Plug doesn't know anything about Jason. So it is either up to your app to do it. You can implement it yourself with the semantics you find better, if you can find any, per the reasons mentioned by @OvermindDL1.

@josevalim josevalim closed this Jan 8, 2019

@thiagogsr

This comment has been minimized.

Copy link

thiagogsr commented Jan 8, 2019

The problem occurs when I try to test my controller.

test "returns the image url when the upload has successful", %{conn: conn} do
  upload = %Plug.Upload{path: "/foo/bar.jpg", filename: "bar.jpg", content_type: "jpg"}
  conn = post(conn, image_path(conn, :create), %{"image" => upload})

  assert %{"data" => %{"link" => "url_to_image"}} = json_response(conn, 200)
end

I guess the test try to encode the upload variable. I just changed it to send a simple map and it worked 👍

@OvermindDL1

This comment has been minimized.

Copy link

OvermindDL1 commented Jan 8, 2019

@thiagogsr For a test like that I wouldn't use an endpoint test, I'd test the controller straight, which would accept the structure fine and it would more properly and specifically test the specific functionality that the test is trying to test for instead of testing the whole pipeline. :-)

@thiagogsr

This comment has been minimized.

Copy link

thiagogsr commented Jan 8, 2019

@OvermindDL1 thank you. I'm not sure if I know how to do it, I'm using mox library to mock the upload and I'm testing if the response is the expected. Couldn't you send some example or doc url?

@OvermindDL1

This comment has been minimized.

Copy link

OvermindDL1 commented Jan 8, 2019

@thiagogsr I'd recommend asking about how to handle that test best on the ElixirForums, you'll get more detailed and better answers there. It doesn't really belong in this github issue and I don't want to pollute it. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment