-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
@@ -38,7 +38,7 @@ defmodule Example17xAppWeb do | |||
def controller do | |||
quote do | |||
use Phoenix.Controller, | |||
formats: [:html, :json], | |||
formats: [:html, :json, :"json-api"], |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use JaSerializer.PhoenixView | ||
|
||
attributes([:volume_level]) | ||
|
||
def id(_data, _conn) do | ||
"singular" | ||
end | ||
|
||
def type do | ||
"things" |
There was a problem hiding this comment.
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 ✅
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 🥳