You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The default blueprint json file generated from mix.
defmoduleMyApp.PostJSONdo@doc""" Renders a list of posts. """defindex(%{posts: posts})do%{data: for(post<-posts,do: data(post))}end@doc""" Renders a single post. """defshow(%{post: post})do%{data: data(post)}enddefpdata(%Post{}=post)do%{id: post,text: post.text}endend
The text was updated successfully, but these errors were encountered:
snewcomer
changed the title
New docs for
New docs for json views in Phoenix
Oct 16, 2024
To support a view that also supports JSONAPI, we need to create a separate object_json_api.ex that serializes the data passed from the controller and puts the appropriate view based on the headers.
Something like this so that render dispatches to the appropriate json serialization module.
def get_request_format(conn) do
case get_req_header(conn, "content-type") do
["application/vnd.api+json" | _] -> "json-api"
_ -> "json"
end
end
.....
def put_format_view(conn) do
case get_request_format(conn) do
"json-api" ->
conn
|> put_resp_content_type("application/vnd.api+json")
|> put_view(module.json_api_view())
_ ->
conn
|> put_resp_content_type("application/json")
|> put_view(module.json_view())
end
end
Do we need new docs? Sry its been a while since I've looked at Phoenix, but with this new paradigm, it seems like we would make use of the Serializer?
https://hexdocs.pm/phoenix/json_and_apis.html
The usage docs
The default blueprint json file generated from
mix
.The text was updated successfully, but these errors were encountered: