vt-elixir / ja_serializer

JSONAPI.org Serialization in Elixir.
Other
640 stars 148 forks source link

Inconsistency in attribute and relationship functions #298

Closed lasseebert closed 5 years ago

lasseebert commented 5 years ago

In a view, one can specify a custom attribute in two ways:

attributes [:foo]

def foo(model, _conn), do: model.bar
# OR:
def foo(model), do: model.bar

For relationships only the first way works:

has_one(:foo, include: true, serializer: SomeView)

def foo(model, _conn), do: model.bar

This inconsistency caught me and I spend some time figuring out what was wrong. (Especially because the default behaviour just defaults to nil if a function or attribute is not found).

Is this intended? Otherwise I will happily make a PR making it consistent.

beerlington commented 5 years ago

I don't believe that's intentional and I wasn't even aware that the def foo(model), do: model.bar version worked. It looks like that's happening here https://github.com/vt-elixir/ja_serializer/blob/master/lib/ja_serializer/dsl.ex#L242-L243

If possible, I think it makes sense to support the single argument relationship so it's consistent. I would be happy to accept a PR for that. Thanks!