mathieuprog / polymorphic_embed

Polymorphic embeds in Ecto
Apache License 2.0
341 stars 63 forks source link

Missing/Not found type raising error when passed as string #64

Closed JGrishey closed 2 years ago

JGrishey commented 2 years ago

Hello!

As of 3.0.3, if I pass in

%{ "channel": "an_unknown_channel", ...}

to a schema with:

polymorphic_embeds_one(:channel,
    types: [
        known_channel: ... # some atom
    ]
)

You will receive this error:

** (ArgumentError) errors were found at the given arguments:

       * 1st argument: not an already existing atom

     code: SomeSchema.load(invalid_payload)
     stacktrace:
       :erlang.binary_to_existing_atom("an_unknown_channel", :utf8)
       (polymorphic_embed 3.0.3) lib/polymorphic_embed.ex:389: PolymorphicEmbed.maybe_to_existing_atom/1
       (polymorphic_embed 3.0.3) lib/polymorphic_embed.ex:383: PolymorphicEmbed.get_metadata_for_type/2
       (polymorphic_embed 3.0.3) lib/polymorphic_embed.ex:349: PolymorphicEmbed.do_get_polymorphic_module_for_type/2
       (polymorphic_embed 3.0.3) lib/polymorphic_embed.ex:156: PolymorphicEmbed.cast_polymorphic_embeds_one/5

I believe this is due to the changes in 3.0.3 that introduce String.to_existing_atom(...). This raises on no found atom, but this library shouldn't raise an error unless you have on_not_found: :raise set.

mathieuprog commented 2 years ago

Fixed in 3.0.4 Will never happen again because: