diff --git a/lib/ja_serializer/builder/included.ex b/lib/ja_serializer/builder/included.ex index 934fce6..6075fe5 100644 --- a/lib/ja_serializer/builder/included.ex +++ b/lib/ja_serializer/builder/included.ex @@ -41,6 +41,7 @@ defmodule JaSerializer.Builder.Included do end defp resource_objects_for(structs, conn, serializer, opts) do + structs = Enum.filter(structs, &is_map/1) %{data: structs, conn: conn, serializer: serializer, opts: opts} |> ResourceObject.build() |> List.wrap() @@ -48,9 +49,12 @@ defmodule JaSerializer.Builder.Included do # Find relationships that should be included. defp relationships_with_include(context) do - context.data - |> context.serializer.relationships(context.conn) - |> Enum.filter(fn {rel_name, rel_definition} -> + context + |> case do + %{relationships: relationships} -> relationships + %{data: data} -> context.serializer.relationships(data, context.conn) + end + |> Enum.filter(fn({rel_name, rel_definition}) -> case context[:opts][:include] do # if `include` param is not present only return 'default' includes nil -> diff --git a/test/ja_serializer/builder/included_test.exs b/test/ja_serializer/builder/included_test.exs index b4b5dfd..db8f667 100644 --- a/test/ja_serializer/builder/included_test.exs +++ b/test/ja_serializer/builder/included_test.exs @@ -477,4 +477,13 @@ defmodule JaSerializer.Builder.IncludedTest do ids = Enum.map(json["included"], &Map.get(&1, "id")) assert "p1" in ids end + + test "non-existent include that is serialized into resource identifier results in no includes being added" do + a1 = %TestModel.Article{id: "a1", title: "a1", author: "p1"} + + json = JaSerializer.format(ArticleSerializer, a1, %{}, include: "author") + keys = Map.keys(json) + assert not "included" in keys + assert json["data"]["relationships"]["author"]["data"]["id"] == "p1" + end end