-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Problem with encoding Ecto model after upgrading to 0.14 #840
Comments
This is actually a good error to have. You were encoding private metadata information to the client. Please check how to customize your encoders in Poison or provide a default function for such that removes fields like |
@chinh7 if you or someone else still having this issue, I posted a quick coderwall post with the solution for it: https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison |
I think this should be made more obvious for newbies somewhere. It is not immediately clear from reading the Ecto or Phoenix docs that I need to add code to my application from an obscure blog post. |
Thanks @estevaoam, very helpful. I agree with @radar, there's got to be a better way to communicate this. |
Just ran into this too... it's an extremely confusing error. This came up in Phoenix... maybe Phoenix should implement a method to easily remove fields that shouldn't be encoded in the JSON response. |
Poison already provides this:
Plus Phoenix recommendation is to not rely on JSON encoding but instead to José Valimwww.plataformatec.com.br |
Thank you @josevalim :) |
i c u @radar +1 @josevalim on the mix phoenix.gen.json that's how i figured it out. |
@estevaoam why not this syntax (just a curiosity):
|
ok last thing I swear: @estevaoam your blog post doesn't quite allude to the fact that you can implement a custom implementation per model. This one breaks it down: That being said, I do think I prefer the |
The View method of serialization has some advantages, but I think flexibility is the biggest. If you ever want to change which attributes are serializer based on the current scope (user) it will be easy to control within the view. |
@sebbean thank you for your comment. Your syntax is much better, that final result happened after many tries and fails. 😄 Just a info: I'm an experienced ruby developer learning some bits of Elixir, thank you for your help. 😃 |
I use this an work:
ref: here |
I'm just a beginner with elixir and phoenix, so maybe this is a trivial question. I don't get how to use the |
@tcoopman can you post this on the Phoenix mailing-list? GH Issues are reserved for bugs plus I'm sure you'll get more response over there. |
@stevedomin thanks for the suggestion. Just posted a question. Sorry for the noise! |
Just leaving a tip for those coming here in the future: poison 2.1.0 now has an So, if you are positive that you want to encode all your fields but defmodule Model do
@derive {Poison.Encoder, except: [:__meta__]}
schema "models" do
# blah blah blah
end
end Hope this helps! |
@lucidstack Good tip, but how can you use Poison 2.1.0 when Ecto locks it at 1.5.2? |
@rubik that is a very good point. Fortunately, the current Ecto version, |
to clarify the last coment: and run |
I like having poison handle this by default, as it prevents having to specify an implementation with derive in each model (as model fields change, etc). Also, would be nice for poison to handle associations not loaded- heres what I'm using:
not a hard workaround, as this code and the implementation for ecto models in general just gets stuck in web.ex, but I could see how it not working out of the box could be frustrating for people used to |
The problem with that implementation is that it's plain wrong in many situations. We cannot use something like that as a default. |
Can you please expand? |
I meant the |
Here is not working 😢 , I'm using defmodule Momsfood.Sale do
use Ecto.Model
import Ecto.Changeset
@derive {Poison.Encoder, only: [:price, :sale_time]}
schema "sales" do
field :price, :float
field :sale_time, Ecto.DateTime
timestamps
end
def changeset(sale, params \\ %{}) do
sale
|> cast(params, [:price, :sale_time])
end
end And here's my route: post "/sales" do
sale = Momsfood.Sale.changeset(%Momsfood.Sale{}, conn.params)
if sale.valid? do
Momsfood.Repo.insert(sale)
send_resp(conn, 201, "")
else
send_resp(conn, 400, Poison.encode!(sale.errors))
end
Any idea ? Thanks guys!! |
I just experienced this problem after adding a new model to an existing Phoenix app (upgraded to Phoenix 1.2.0, Elixir 1.3.) The solution shown by @estevaoam solved the problem, with an app warning of: "warning: redefining module Poison.Encoder.Any (current version loaded from _build/dev/lib/poison/ebin/Elixir.Poison.Encoder.Any.beam) lib/myapp/encoder.ex:1" I find it strange that my original model doesn't experience this problem when served as json with its own (original) controller, yet the formats for the schemas, views and controllers are the same for both the older and newer models. 😕 |
OK, the solution shown by @estevaoam worked for me only in local development mode, but then caused an error preventing a release from building. The way to solve this problem is answered by @josevalim on Stack Overflow: Using Phoenix as an API seems to be a popular use case. @chrismccord - Perhaps the guide at http://www.phoenixframework.org/docs/ecto-models can be extended to include an example of serving json. Looks like an easy addition. Do you accept PRs for that guide? |
A great example of how to deal with json in phoenix are the generated json endpoints with |
I've used the defimpl example provided by @Dania02525 with a small change to just return
And it has worked like a charm so far. |
Привет, товарищи!! I came across with a solution with a little help from "sjoconnor" @ slack/phoenix. I hade the same issue "Poison.EncodeError" So what we propose is to tear down from the sending list the ":meta" and ":struct":
That is a pretty "Cave Man" way to do the things, but it works just good!! Happy programming |
Just ran into this too when forced to use old versions of Ecto! |
I guess one should follow the entire tutorial to figure this, or use mix to generate models. I kept thinking I was doing something wrong, but normal maps worked well, lol. Wish poison handles this internally. Is there any reason why we would not want dropping meta to be a standard encoding feature for poison? |
My Phoenix app had been running fine until recently when I upgraded Ecto to v0.14
I noticed Ecto metadata source was changed from string to tuple, resulting in a json encoding problem
Any advice to fix this?
The text was updated successfully, but these errors were encountered: