phoenixframework / phoenix

Peace of mind from prototype to production
https://www.phoenixframework.org
MIT License
21.38k stars 2.87k forks source link

Incorrect documentation :json_libary vs :json_encoding #3341

Closed lucas-nelson closed 5 years ago

lucas-nelson commented 5 years ago

Environment

Behavior

We had thought we changed over to jason for JSON encoding during our upgrade to Phoenix 1.4. But, we still have poison in our deps because of other libraries. Things have been working fine. Today an unrelated dep upgrade bumped poison to 4.x and we saw an error out of phoenix:

     ** (UndefinedFunctionError) function Poison.encode_to_iodata!/1 is undefined or private
     code: capture_log(fn ->
     stacktrace:
       (poison) Poison.encode_to_iodata!(%{error: "{:error, #Ecto.Changeset<action: nil, changes: %{}, errors: [], data: nil, valid?: false>}", params: %Progress.Student.Context{canonical_student_id: 1, grid: nil, journey: nil, precinct: :learning, product: :rejr}})
       (phoenix) lib/phoenix/controller.ex:279: Phoenix.Controller.json/2
       (piper) web/controllers/api/v3/event_controller.ex:1: Piper.Api.V3.EventController.action/2
       (piper) web/controllers/api/v3/event_controller.ex:1: Piper.Api.V3.EventController.phoenix_controller_pipeline/2
       (piper) lib/piper/endpoint.ex:1: Piper.Endpoint.instrument/4
       (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
       (piper) lib/piper/endpoint.ex:1: Piper.Endpoint.plug_builder_call/2
       (piper) lib/piper/endpoint.ex:51: Piper.Endpoint."call (overridable 3)"/2
       (piper) lib/piper/endpoint.ex:1: Piper.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:235: Phoenix.ConnTest.dispatch/5
       test/controllers/api/v3/event_controller_test.exs:163: anonymous fn/2 in Piper.Api.V3.EventControllerTest."test Deprecreated Protobufs POST /events - with valid protobuf message and invalid semantics"/1
       (ex_unit) lib/ex_unit/capture_log.ex:78: ExUnit.CaptureLog.capture_log/2
       test/controllers/api/v3/event_controller_test.exs:158: (test)

We have this configured:

config :phoenix, :json_encoding, Jason

Which matches the recommendation at:

https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix.ex#L103-L117

IO.warn """
Phoenix now requires you to explicitly list which engine to use
for Phoenix JSON encoding. We recommend everyone to upgrade to
Jason by setting in your config/config.exs:

    config :phoenix, :json_encoding, Jason

And then adding {:jason, "~> 1.0"} as a dependency.

If instead you would rather continue using Poison, then add to
your config/config.exs:

    config :phoenix, :json_encoding, Poison
"""

But the variable the case is operating on is configured_lib = Application.get_env(:phoenix, :json_library)

If I change our configuration to:

config :phoenix, :json_library, Jason

The error goes away.

I think the documentation is wrong? :json_encoding instead of :json_library? The change happened in e34c8b6e3bc3a57191097d723c5c9795c5118bf5 .

lucas-nelson commented 5 years ago

fwiw, also in the 1.4 upgrade blog post I can see ":json_library" - https://phoenixframework.org/blog/upgrading-from-13x-to-140