Skip to content

Commit

Permalink
Fix "__schema__/1 is undefined or private" error while inspecting a s…
Browse files Browse the repository at this point in the history
…chemaless changeset (#3440)
  • Loading branch information
ruudk committed Oct 10, 2020
1 parent 0ec6688 commit 9537919
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2941,7 +2941,7 @@ defimpl Inspect, for: Ecto.Changeset do
end

redacted_fields = case data do
%type{} -> type.__schema__(:redact_fields)
%type{__meta__: _} -> type.__schema__(:redact_fields)
_ -> []
end

Expand Down
24 changes: 24 additions & 0 deletions test/ecto/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ defmodule Ecto.ChangesetTest do
end
end

defmodule NoSchemaPost do
defstruct [:title, :upvotes]
end

defp changeset(schema \\ %Post{}, params) do
cast(schema, params, ~w(id token title body upvotes decimal color topics virtual)a)
end
Expand Down Expand Up @@ -162,6 +166,19 @@ defmodule Ecto.ChangesetTest do
assert apply_changes(changeset) == %{title: "world", upvotes: 0}
end

test "cast/4: with data struct and types" do
data = {%NoSchemaPost{title: "hello"}, %{title: :string, upvotes: :integer}}
params = %{"title" => "world", "upvotes" => "0"}

changeset = cast(data, params, ~w(title upvotes)a)
assert changeset.params == params
assert changeset.data == %NoSchemaPost{title: "hello"}
assert changeset.changes == %{title: "world", upvotes: 0}
assert changeset.errors == []
assert changeset.valid?
assert apply_changes(changeset) == %NoSchemaPost{title: "world", upvotes: 0}
end

test "cast/4: with dynamic embed" do
data = {
%{
Expand Down Expand Up @@ -1776,6 +1793,13 @@ defmodule Ecto.ChangesetTest do
assert inspect(changeset(%{"title" => "title", "body" => "hi"})) ==
"#Ecto.Changeset<action: nil, changes: %{body: \"hi\", title: \"title\"}, " <>
"errors: [], data: #Ecto.ChangesetTest.Post<>, valid?: true>"

data = {%NoSchemaPost{title: "hello"}, %{title: :string, upvotes: :integer}}
params = %{"title" => "world", "upvotes" => "0"}

assert inspect(cast(data, params, ~w(title upvotes)a)) ==
"#Ecto.Changeset<action: nil, changes: %{title: \"world\", upvotes: 0}, " <>
"errors: [], data: #Ecto.ChangesetTest.NoSchemaPost<>, valid?: true>"
end

test "redacts fields marked redact: true" do
Expand Down

0 comments on commit 9537919

Please sign in to comment.