Skip to content

Add @schema_redact: :all_except_primary_keys module attribute to Ecto Schema #4599

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

Merged
merged 4 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ defmodule Ecto.Schema do
inspect on the schema unless the schema module is tagged with
the option `@derive_inspect_for_redacted_fields false`.

A schema module tagged with `@schema_redact :all_except_primary_keys` will
redact all fields except primary keys.

## Schema attributes

Supported attributes for configuring the defined schema. They must
Expand All @@ -155,6 +158,10 @@ defmodule Ecto.Schema do
which generates structs and queries without context. Context are not used
by the built-in SQL adapters.

* `@schema_redact` - If set to `:all_except_primary_keys`, Ecto will
treat all non-primary key fields as if they were individually marked
as redacted.

* `@foreign_key_type` - configures the default foreign key type
used by `belongs_to` associations. It must be set in the same
module that defines the `belongs_to`. Defaults to `:id`;
Expand Down Expand Up @@ -1992,7 +1999,14 @@ defmodule Ecto.Schema do
writable = opts[:writable] || :always
put_struct_field(mod, name, Keyword.get(opts, :default))

if Keyword.get(opts, :redact, false) do
redact_field? = Keyword.get_lazy(opts, :redact, fn ->
case Module.get_attribute(mod, :schema_redact) do
:all_except_primary_keys -> not pk?
_ -> false
end
end)

if redact_field? do
Module.put_attribute(mod, :ecto_redact_fields, name)
end

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

defmodule RedactAllExceptPrimaryKeysSchema do
use Ecto.Schema

@schema_redact :all_except_primary_keys
schema "redacted_schema" do
field :password, :string
field :username, :string
field :virtual_pass, :string, virtual: true
end
end

defmodule RedactedEmbeddedSchema do
use Ecto.Schema

Expand Down Expand Up @@ -3683,5 +3694,12 @@ defmodule Ecto.ChangesetTest do
assert inspect(changeset) =~ "hunter2"
refute inspect(changeset) =~ "**redacted**"
end

test "redacts all non-primary-key fields when schema sets @schema_redact :all_except_primary_keys" do
changeset = Ecto.Changeset.cast(%RedactAllExceptPrimaryKeysSchema{}, %{username: "Hunter", password: "hunter2"}, [:username, :password])
assert inspect(changeset) =~ "id"
refute inspect(changeset) =~ "hunter2"
assert inspect(changeset) =~ "**redacted**"
end
end
end
31 changes: 31 additions & 0 deletions test/ecto/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,37 @@ defmodule Ecto.SchemaTest do
refute inspect(%Schema{temp: "hunter2"}) =~ "hunter2"
end

defmodule SchemaWithRedactAllExceptPrimaryKeys do
use Ecto.Schema

@schema_redact :all_except_primary_keys
schema "my_schema" do
field :password, :string
field :temp, :any, default: "temp", virtual: true
end
end

test "schema with @schema_redact: :all_except_primary_keys derives inspect for all non-primary-key fields" do
inspected_schema = inspect(%SchemaWithRedactAllExceptPrimaryKeys{password: "hunter2"})

assert inspected_schema =~ "id"
refute inspected_schema =~ "hunter2"
refute inspected_schema =~ "temp"
end

defmodule SchemaWithRedactFalse do
use Ecto.Schema

@schema_redact false
schema "my_schema" do
field :name, :string
end
end

test "schema with @schema_redact: false doesn't derive inspect" do
assert inspect(%SchemaWithRedactFalse{name: "Hunter"}) =~ "Hunter"
end

defmodule SchemaWithoutDeriveInspect do
use Ecto.Schema

Expand Down