Skip to content
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

Poison.decode\2 not responding with error when the result is not fit to the 'as' parameter #145

Open
griffhun opened this issue Sep 21, 2017 · 1 comment

Comments

@griffhun
Copy link

griffhun commented Sep 21, 2017

defmodule Event do
  @type t :: %__MODULE__{}
  defstruct [:type, :created, :payload]
end

Possible Incorrect behaviour:
iex(4)> Poison.decode("1", as: %Event{})
{:ok, 1}

iex(8)> Poison.decode("\"apple\"", as: %Event{})
{:ok, "apple"}

Working as expected:
iex(6)> Poison.decode("alma", as: %Event{})
{:error, {:invalid, "a", 0}}

iex(12)> Poison.decode("{\"type\":\"t1\"}", as: %Event{})
{:ok, %Event{created: nil, payload: nil, type: "t1"}}
@griffhun griffhun changed the title Poison.decode\2 not raising error when the result is not fitting to the as parameter Poison.decode\2 not responding with error when the result is not fit to the 'as' parameter Sep 21, 2017
@Sunspar
Copy link

Sunspar commented Dec 30, 2017

I'd also be interested in hearing whether this is planned functionality, a bug, etc. I'm working on writing a library to wrap an API and for some calls I can get either an appropriate document for that endpoint or an "error" response with a message as to why. I can't find a way to ask it to fail out when the document doesn't match the strut passed in via as:, so right now I have to do the conversion twice, poke around to see which case I'm dealing with and then return appropriately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants