-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,52 @@ | ||
if Code.ensure_loaded?(Jason.Encoder) do | ||
defimpl Jason.Encoder, for: Ecto.Association.NotLoaded do | ||
def encode(%{__owner__: owner, __field__: field}, _) do | ||
raise """ | ||
cannot encode association #{inspect(field)} from #{inspect(owner)} to \ | ||
JSON because the association was not loaded. | ||
for encoder <- [Jason.Encoder, JSON.Encoder] do | ||
module = Macro.inspect_atom(:literal, encoder) | ||
|
||
You can either preload the association: | ||
if Code.ensure_loaded?(encoder) do | ||
defimpl encoder, for: Ecto.Association.NotLoaded do | ||
def encode(%{__owner__: owner, __field__: field}, _) do | ||
raise """ | ||
cannot encode association #{inspect(field)} from #{inspect(owner)} to \ | ||
JSON because the association was not loaded. | ||
Repo.preload(#{inspect(owner)}, #{inspect(field)}) | ||
You can either preload the association: | ||
Or choose to not encode the association when converting the struct \ | ||
to JSON by explicitly listing the JSON fields in your schema: | ||
Repo.preload(#{inspect(owner)}, #{inspect(field)}) | ||
defmodule #{inspect(owner)} do | ||
# ... | ||
Or choose to not encode the association when converting the struct \ | ||
to JSON by explicitly listing the JSON fields in your schema: | ||
@derive {Jason.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
defmodule #{inspect(owner)} do | ||
# ... | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
@derive {#{unquote(module)}, only: [:name, :title, ...]} | ||
schema ... do | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
end | ||
|
||
defimpl Jason.Encoder, for: Ecto.Schema.Metadata do | ||
def encode(%{schema: schema}, _) do | ||
raise """ | ||
cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ | ||
to JSON. This metadata is used internally by Ecto and should never be \ | ||
exposed externally. | ||
defimpl encoder, for: Ecto.Schema.Metadata do | ||
def encode(%{schema: schema}, _) do | ||
raise """ | ||
cannot encode metadata from the :__meta__ field for #{inspect(schema)} \ | ||
to JSON. This metadata is used internally by Ecto and should never be \ | ||
exposed externally. | ||
You can either map the schemas to remove the :__meta__ field before \ | ||
encoding or explicitly list the JSON fields in your schema: | ||
You can either map the schemas to remove the :__meta__ field before \ | ||
encoding or explicitly list the JSON fields in your schema: | ||
defmodule #{inspect(schema)} do | ||
# ... | ||
defmodule #{inspect(schema)} do | ||
# ... | ||
@derive {Jason.Encoder, only: [:name, :title, ...]} | ||
schema ... do | ||
@derive {#{unquote(module)}, only: [:name, :title, ...]} | ||
schema ... do | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
You can also use the :except option instead of :only if you would \ | ||
prefer to skip some fields. | ||
""" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
defmodule Ecto.JsonTest do | ||
use ExUnit.Case, async: true | ||
|
||
@implementations [{Jason, Jason.Encoder}, {JSON, JSON.Encoder}] | ||
|
||
loaded_implementations = | ||
for {_lib, encoder} = implementation <- @implementations, | ||
Code.ensure_loaded?(encoder), | ||
do: implementation | ||
|
||
defmodule User do | ||
use Ecto.Schema | ||
|
||
@derive Jason.Encoder | ||
@derive Keyword.values(loaded_implementations) | ||
schema "users" do | ||
has_many :comments, Ecto.Comment | ||
end | ||
end | ||
|
||
test "encodes decimal" do | ||
decimal = Decimal.new("1.0") | ||
assert Jason.encode!(decimal) == ~s("1.0") | ||
end | ||
for {json_library, _encoder} <- loaded_implementations do | ||
describe to_string(json_library) do | ||
test "encodes decimal" do | ||
decimal = Decimal.new("1.0") | ||
assert unquote(json_library).encode!(decimal) == ~s("1.0") | ||
end | ||
|
||
test "fails on association not loaded" do | ||
assert_raise RuntimeError, | ||
~r/cannot encode association :comments from Ecto.JsonTest.User to JSON/, | ||
fn -> Jason.encode!(%User{}.comments) end | ||
end | ||
test "fails on association not loaded" do | ||
assert_raise RuntimeError, | ||
~r/cannot encode association :comments from Ecto.JsonTest.User to JSON/, | ||
fn -> unquote(json_library).encode!(%User{}.comments) end | ||
end | ||
|
||
test "fails when encoding __meta__" do | ||
assert_raise RuntimeError, | ||
~r/cannot encode metadata from the :__meta__ field for Ecto.JsonTest.User to JSON/, | ||
fn -> Jason.encode!(%User{comments: []}) end | ||
test "fails when encoding __meta__" do | ||
assert_raise RuntimeError, | ||
~r/cannot encode metadata from the :__meta__ field for Ecto.JsonTest.User to JSON/, | ||
fn -> unquote(json_library).encode!(%User{comments: []}) end | ||
end | ||
end | ||
end | ||
end |