1) test google_handler/2 show welcome (state=nil) > handler/3 (AuthWeb.PageControllerTest)
test/auth_web/controllers/page_controller_test.exs:30
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %{:__meta__ => #Ecto.Schema.Metadata<:loaded, "people">, :__struct__ => Auth.Person, :auth_provider => "google", :email => "[email protected]", :email_hash => "[email protected]", :familyName => "Correia", :givenName => "Nelson", :id => 2, :inserted_at => ~N[2020-04-28 14:56:26], :key_id => nil, :locale => "en", :password => nil, :password_hash => nil, :picture => "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7a", :status => 1, :statuses => #Ecto.Association.NotLoaded<association :statuses is not loaded>, :tag => nil, :updated_at => ~N[2020-04-28 14:56:26], :username => nil, :username_hash => nil, "aud" => "Joken", "exp" => 1619622786, "iat" => 1588085786, "iss" => "Joken", "jti" => "2o5076u24l10g5vt5g0000b6", "nbf" => 1588085786} of type Auth.Person (a struct), Jason.Encoder protocol must always be explicitly implemented.
If you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:
@derive {Jason.Encoder, only: [....]}
defstruct ...
It is also possible to encode all fields, although this should be used carefully to avoid accidentally leaking private information when new fields are added:
@derive Jason.Encoder
defstruct ...
Finally, if you don't own the struct you want to encode to JSON, you may use Protocol.derive/3 placed outside of any module:
Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])
Protocol.derive(Jason.Encoder, NameOfTheStruct)
. This protocol is implemented for the following type(s): Ecto.Association.NotLoaded, Ecto.Schema.Metadata, Date, BitString, Jason.Fragment, Any, Map, NaiveDateTime, List, Integer, Time, DateTime, Decimal, Atom, Float
code: conn = get(conn, "/auth/google/callback", %{code: "234", state: nil})
stacktrace:
(jason 1.2.0) lib/jason.ex:150: Jason.encode!/2
(jose 1.10.1) src/jwt/jose_jwt.erl:89: :jose_jwt.to_binary/1
(jose 1.10.1) src/jwt/jose_jwt.erl:172: :jose_jwt.sign/3
(joken 2.2.0) lib/joken/signer.ex:119: Joken.Signer.sign/2
(joken 2.2.0) lib/joken.ex:365: Joken.encode_and_sign/3
(auth_plug 0.12.0) lib/token.ex:70: AuthPlug.Token.generate_jwt!/2
(auth_plug 0.12.0) lib/auth_plug.ex:114: AuthPlug.create_jwt_session/2