mathieuprog/polymorphic_embed

Missing/Not found type raising error when passed as string

Closed this issue · 1 comments

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.

Fixed in 3.0.4
Will never happen again because:

  • got completely rid of any to_existing_atom calls
  • added tests for this case