Skip to content

Commit bde243b

Browse files
committed
Fix CondClauseError when using use_parent_field_for_type with matching
types When using `use_parent_field_for_type` option in `polymorphic_embeds_one`, the `action_and_struct/4` function's cond block was missing a clause to handle the case when both `type_from_parent_field` and `type_from_map` are present and match. The existing cond only handled: 1. type_from_parent_field is nil -> :type_not_found 2. type_from_map is nil -> get module and return action/struct 3. types don't match -> raise error This caused a CondClauseError when form params included the `__type__` field matching the parent field type (e.g., during form validation). Added a `true` clause to handle the matching types case, which behaves the same as when type_from_map is nil - it uses the parent field type to determine the module and returns the appropriate action/struct tuple.
1 parent 18a506d commit bde243b

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

lib/polymorphic_embed.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@ defmodule PolymorphicEmbed do
360360

361361
to_string(type_from_parent_field) != to_string(type_from_map) ->
362362
raise "type specified in the parent field \"#{type_from_parent_field}\" does not match the type in the embedded map \"#{type_from_map}\""
363+
364+
true ->
365+
# type_from_parent_field and type_from_map match
366+
module = get_polymorphic_module_for_type(type_from_parent_field, types_metadata)
367+
368+
if is_nil(data_for_field) or data_for_field.__struct__ != module do
369+
{:insert, struct(module)}
370+
else
371+
{:update, data_for_field}
372+
end
363373
end
364374
else
365375
case get_polymorphic_module_from_map(params, type_field_name, types_metadata) do

0 commit comments

Comments
 (0)