Open elvanja opened 3 years ago
P.S. If it helps, I also have a workaround. In controller, I apply this transformation to the sparse fields params:
defp underscore(nil), do: nil
defp underscore(string) when is_binary(string), do: String.replace(string, "-", "_")
defp underscore(%{} = map) do
map
|> Enum.map(fn {key, value} ->
{key, underscore(value)}
end)
|> Enum.into(%{})
end
Used like this:
render(conn, "index.json",
data: some_data,
opts: [
fields: underscore(params["fields"])
]
)
@elvanja sorry I haven't had a chance to look at this yet. It seems like this should affect any sparse field name that needs to get converted from dashes to underscore. If the regular (non-custom) property name has an underscore, does that break too?
Hi! Sure, no problemo, it is not an emergency 😄 And yes, the non-custom properties also break, on main record or on relations.
I have a serializer defined like this:
Where
:name
exists in the original struct, but:custom_property
is calculated in same named function.If I try to use sparse fields that include the custom property via e.g.
fields[record]=custom-property
, it breaks when trying to convert from dashed to underscored key value while building the attributes (error details below).A fix for this is to just apply formatting while doing the conversion. E.g. in JaSerializer.Builder.Utils.safe_atom_list/1:
After that, it all seems to be working fine. I can turn this into a PR, but I am not sure if this is the right place to fix the issue.
And, here is the stack trace: