AF
Ash Framework•2mo ago
minib

AshPhoenix - Union: "is not a map" error

I can't figure out why these params don't work:
%{
"access" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,_union_type,_unused_api_key,_unused_secret_key,api_key,secret_key",
"_union_type" => "xyz",
"api_key" => "dsf",
"secret_key" => "sdf"
},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
}
%{
"access" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,_union_type,_unused_api_key,_unused_secret_key,api_key,secret_key",
"_union_type" => "xyz",
"api_key" => "dsf",
"secret_key" => "sdf"
},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
}
This is the failed form:
source: #AshPhoenix.Form<
action: :create,
params: %{
"access" => %Ash.Union{value: nil, type: :xyz},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
raw_params: %{
"access" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,_union_type,_unused_api_key,_unused_secret_key,api_key,secret_key",
"_union_type" => "xyz",
"api_key" => "dsf",
"secret_key" => "sdf"
},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
params: %{
"access" => %Ash.Union{value: nil, type: :xyz},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
errors: [access: {"is not a map", []}],
source: #AshPhoenix.Form<
action: :create,
params: %{
"access" => %Ash.Union{value: nil, type: :xyz},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
raw_params: %{
"access" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,_union_type,_unused_api_key,_unused_secret_key,api_key,secret_key",
"_union_type" => "xyz",
"api_key" => "dsf",
"secret_key" => "sdf"
},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
params: %{
"access" => %Ash.Union{value: nil, type: :xyz},
"account_id" => "0198dd77-e6cb-7d4c-a039-9e128da2ea8f",
"label" => "asd"
},
errors: [access: {"is not a map", []}],
Can anyone please help me to debug?
7 Replies
minib
minibOP•2mo ago
defmodule MyApp.Services.Credentials do
use Ash.Resource,
otp_app: :my_app,
domain: MyApp.Services,
a_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer]

postgres do
table "credentials"
repo Ober.Repo
end

actions do
defaults [:read, :destroy, create: [:account_id, :label, :access]]
end

policies do
policy do
authorize_if relates_to_actor_via([:account, :user])
end
end

attributes do
uuid_v7_primary_key :id

attribute :label, :string do
allow_nil? false
public? true
end

attribute :access, :credentials_type do
allow_nil? false
public? true
end

timestamps()
end

relationships do
belongs_to :account, MyApp.Services.Account do
allow_nil? false
end
end
end


defmodule MyApp.Services.Credentials.CredentialsType do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
xyz: [
type: :struct,
constraints: [instance_of: MyApp.Services.Credentials.XyzCredentials],
tag: :type,
tag_value: :xyz
]
]
]
end

defmodule MyApp.Services.Credentials.XyzCredentials do
use Ash.Resource, data_layer: :embedded

actions do
defaults [:read, create: [:api_key, :secret_key], update: [:api_key, :secret_key]]
end

attributes do
attribute :api_key, :string, allow_nil?: false, public?: true
attribute :secret_key, :string, allow_nil?: false, public?: true
end
end
defmodule MyApp.Services.Credentials do
use Ash.Resource,
otp_app: :my_app,
domain: MyApp.Services,
a_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer]

postgres do
table "credentials"
repo Ober.Repo
end

actions do
defaults [:read, :destroy, create: [:account_id, :label, :access]]
end

policies do
policy do
authorize_if relates_to_actor_via([:account, :user])
end
end

attributes do
uuid_v7_primary_key :id

attribute :label, :string do
allow_nil? false
public? true
end

attribute :access, :credentials_type do
allow_nil? false
public? true
end

timestamps()
end

relationships do
belongs_to :account, MyApp.Services.Account do
allow_nil? false
end
end
end


defmodule MyApp.Services.Credentials.CredentialsType do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
xyz: [
type: :struct,
constraints: [instance_of: MyApp.Services.Credentials.XyzCredentials],
tag: :type,
tag_value: :xyz
]
]
]
end

defmodule MyApp.Services.Credentials.XyzCredentials do
use Ash.Resource, data_layer: :embedded

actions do
defaults [:read, create: [:api_key, :secret_key], update: [:api_key, :secret_key]]
end

attributes do
attribute :api_key, :string, allow_nil?: false, public?: true
attribute :secret_key, :string, allow_nil?: false, public?: true
end
end
ZachDaniel
ZachDaniel•2mo ago
Hm...something is strange there Might need a repro/issue as it doesn't jump out at me You can try submitting the form with a ! to see if you get a better error Or using the new raw_errors tool to get the errors
minib
minibOP•2mo ago
Hi @ZachDaniel, thanks so much for your reply and the hints! Unfortunately, I'm still running into the same issue. I've created a project reproducing the issue: https://github.com/minibikini/union_form_repro Full disclosure, I haven't been getting much sleep lately, so it's totally possible I'm just overlooking something obvious. Apologies in advance if it's something silly! 🙌
ZachDaniel
ZachDaniel•2mo ago
Did you open an issue as well? I lost track of this and issues is the best way for me to track things that need fixing
munksgaard
munksgaard•2mo ago
@minib Did you try with type: MyApp.Services.Credentials.XyzCredentials and without the constraints: [instance_of ...]? Also, shouldn't this:
attribute :access, :credentials_type do
allow_nil? false
public? true
end
attribute :access, :credentials_type do
allow_nil? false
public? true
end
be
attribute :access, CredentialsType do
allow_nil? false
public? true
end
attribute :access, CredentialsType do
allow_nil? false
public? true
end
minib
minibOP•2mo ago
yes, tried both I've listed CredentialsType in the config as cutom type credentials_type. But in the reproducing repo I've used the module name instead. Nope, thought this belongs to here, support forum. Will open an issue.
minib
minibOP•2mo ago
GitHub
AshPhoenix.Form: "is not a map" error for a union field · Issue #4...
Code of Conduct I agree to follow this project&#39;s Code of Conduct AI Policy I agree to follow this project&#39;s AI Policy, or I agree that AI was not used while creating this issue. Versions Na...

Did you find this page helpful?