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

Experimental attempt to use ja_serializer with phoenix 1.7.7 without using the fallback phoenix_view package #3

Closed

Conversation

theirishpenguin
Copy link
Owner

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

It takes a few strange changes to make a basic ja_serializer scenario work (there may be more reasonable changes to could be made that I am unaware of - especially if some small supporting changes were made in ja_serializer). See inline comments below.

With these changes in place the simple test suite passes 🥳

image

@@ -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).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Note: I should also change the filename for this file to something like thing_json_api.ex (to match the ugly module name).

# (ArgumentError) no "show" json-api template defined for
# :"Elixir.Example17xAppWeb.ThingJSON-API" (the module does not exist)
#
defmodule :"Elixir.Example17xAppWeb.ThingJSON-API" do
Copy link
Owner Author

Choose a reason for hiding this comment

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

So (I suspect that) because of the mime type / format "json-api" changes I had to make in this PR, along with the way that phoenix 1.7.7 renders views, I would get the following error if I used the pre-phoenix 1.7.7 module name style Example17xAppWeb.ThingView...

(ArgumentError) no "show" json-api template defined for :"Elixir.Example17xAppWeb.ThingJSON-API"  (the module does not exist)

If I did not define the type() function below then, when I run the test suite, I would get...

image

I am speculating there is code in JA serializer that...

  • Takes the view name ThingView
  • Removes "View" from the end
  • And uses this as the default "type"
  • But this now longer works since the module name ends in "JSON-API"
  • (Reminder: This is speculation. I could be wrong. But something funny is happening anyway)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Including beerlington's reply (from the previous version of this PR)

image

use JaSerializer.PhoenixView

attributes([:volume_level])

def id(_data, _conn) do
"singular"
end

def type do
"things"
Copy link
Owner Author

Choose a reason for hiding this comment

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

But when I add the explicit type the tests pass ✅

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