Skip to content

Sparse field param for function based attribute breaks #337

@elvanja

Description

@elvanja

I have a serializer defined like this:

  attributes([
    :name,
    :custom_property
  ])

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:

  def safe_atom_list(field_str) do
    field_str
    |> String.split(",")
    |> Enum.map(&JaSerializer.ParamParser.Utils.format_key/1) # <---- this is the fix
    |> Enum.map(&String.to_existing_atom/1)
  end

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:

* (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.binary_to_existing_atom("custom-property", :utf8)
        (elixir 1.10.3) lib/string.ex:2303: String.to_existing_atom/1
        (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
        (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
        (ja_serializer 0.16.0) lib/ja_serializer/builder/attribute.ex:37: JaSerializer.Builder.Attribute.do_filter/2
        (ja_serializer 0.16.0) lib/ja_serializer/builder/attribute.ex:11: JaSerializer.Builder.Attribute.build/1
        (ja_serializer 0.16.0) lib/ja_serializer/builder/resource_object.ex:23: JaSerializer.Builder.ResourceObject.build/1
        (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
        (ja_serializer 0.16.0) lib/ja_serializer/builder/top_level.ex:34: JaSerializer.Builder.TopLevel.build/1
        (ja_serializer 0.16.0) lib/ja_serializer.ex:62: JaSerializer.format/4
        (phoenix 1.4.17) lib/phoenix/view.ex:410: Phoenix.View.render_to_iodata/3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions