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

[Take 3 - Simplified Complete Solution] A way to use ja_serializer with a vanilla phoenix 1.7.X app - without using the fallback phoenix_view package #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

theirishpenguin
Copy link
Owner

@theirishpenguin theirishpenguin commented Aug 31, 2023

Overview

This PR follows on from vt-elixir/ja_serializer#346 (comment) and is an attempt to document a reasonable way of using ja_serializer with phoenix 1.7.7 without using the fallback phoenix_view package.

With these changes in place the simple test suite passes 🥳

image

Resources useful for understanding the move from phoenix 1.6.X to 1.7.X

@@ -38,7 +38,7 @@ defmodule Example17xAppWeb do
def controller do
quote do
use Phoenix.Controller,
formats: [:html, :json],
formats: [:html, :json, :"json-api"],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the mime type in config/config.exs, I appear to be forced to add "json-api" here (because of the dash I need to define it as a stringy atom).

# in the function (as shown below in the show() function). For more details on put_view() usage look under
# the render() documenation at https://hexdocs.pm/phoenix/0.10.0/Phoenix.Controller.html#render/3
#
plug :put_view, Example17xAppWeb.ThingView
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use phoenix 1.7.X directly (as part of the approach in this PR), we need to use put_view() as otherwise phoenix 1.7.X will expect a name like Example17xAppWeb.ThingJSON-API for the view layer module.

(This is because phoenix will try to initially base the name on the :"json-api" format configured earlier).

@@ -1,5 +1,4 @@
defmodule Example17xAppWeb.ThingView do
use Example17xAppWeb, :view
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use use Example17xAppWeb, :view (well, not without creating it) as phoenix has moved away from this idea with the move to phoenix 1.7.X.

@theirishpenguin theirishpenguin changed the title [Take 3 - Simplified Complete Solution] A way to use ja_serializer with a vanilla phoenix 1.7.7 app - without using the fallback phoenix_view package [Take 3 - Simplified Complete Solution] A way to use ja_serializer with a vanilla phoenix 1.7.X app - without using the fallback phoenix_view package Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant