ja_serializer icon indicating copy to clipboard operation
ja_serializer copied to clipboard

infinite loop when using "format" as field name

Open mikeni opened this issue 8 years ago • 3 comments

In dsl.ex, if you have :format as attribute

def default_attributes(serializer, struct, conn) do serializer.__attributes |> Enum.map(&({&1, apply(serializer, &1, [struct, conn])}))

you get

warning: Elixir.MyApp.V1.PhotoView.format/3 is deprecated. Please use JaSerializer.format/4 instead, eg: JaSerializer.format(Elixir.MyApp.V1.PhotoView, data, conn, opts)

and it seems to infinitely loop

mikeni avatar Dec 10 '17 09:12 mikeni

This will be fixed with 1.0.0 when we remove the deprecated function.

beerlington avatar Dec 22 '18 12:12 beerlington

In the meantime, how can you workaround it to use format as an attribute name?

coneybeare avatar Aug 24 '20 19:08 coneybeare

@coneybeare you can use the attributes/2 callback to serialize any conflicting keywords like format:

defmodule MySerializer do
  def attributes(record, _conn) do
    Map.take(record, [:format])
  end
end

beerlington avatar Aug 24 '20 19:08 beerlington