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

Usage information for camelCase outgoing parameters #31

Open
nathany opened this issue Jun 4, 2020 · 4 comments
Open

Usage information for camelCase outgoing parameters #31

nathany opened this issue Jun 4, 2020 · 4 comments

Comments

@nathany
Copy link

nathany commented Jun 4, 2020

The Plug for incoming parameters is working fine, but I must be missing something to get Phoenix Controller's json to do the translation to camel case.

I've followed along with the readme, trying both option for format_encoders:

config :phoenix, :json_library, Jason

config :phoenix, :format_encoders, json: ProperCase.JSONEncoder.CamelCase
config :phoenix, :format_encoders, json: MyApp.CustomJSONEncoder

And have this in a controller:

conn
|> json(%{okay_awesome: true})

Output:

{
    "okay_awesome": true
}

But I would like to see okayAwesome.

Related: #5

@nathany nathany changed the title Usage information for outgoing parameters Usage information for camelCase/pascalCase outgoing parameters Jun 4, 2020
@nathany nathany changed the title Usage information for camelCase/pascalCase outgoing parameters Usage information for camelCase outgoing parameters Jun 4, 2020
@frfroes
Copy link

frfroes commented Jun 8, 2020

+1 for this one, I have the exact same issue and tried already both cases.

@frfroes
Copy link

frfroes commented Jun 8, 2020

Looks like the issue in my case was due to this rescue clause. I was trying to return an Ecto schema in my view and the ProperCase.to_camel_case/1 simply returns the original map when something when it raises Protocol.UndefinedError, which was happening due to the non-Enumerable Ecto fields. The solution for me was to drop such fields before returning:

def render("my_schema.json", %{my_schema: my_schema}) do
    %{
      ok: true,
      my_schema: my_schema |> Map.from_struct() |> Map.drop([:__meta__, :association_field])
    }
 end

That's probably an improvement point, at least to log the something in the rescue clause to let people know what's up.

@christhekeele
Copy link

Also experiencing the same problem, using plain maps for what it's worth.

@christhekeele
Copy link

christhekeele commented Oct 1, 2020

Ah I think I figured my (and likely @nathany's) problem out:

The json controller helper skips the template rendering pipeline entirely. However, Phoenix formatters are implicitly tied to templates: they are looked up based on root extension of a template file. So I imagine the formatter fires fine on render conn, "template.json") for some template.json.eex, but not raw data structures passed to the controller json helper.

For now I'm just calling json(conn, ProperCase.to_camel_case(data)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants