Repository: jeregrine/jsonapi
Pull request #114 (2018-06-04 21:18:26)

Handle dashed include for top-level relationship
Dashed includes were supported for nested relationships, but not for top-level ones. Also removes a stray `String.to_atom`


Patch:
diff --git a/lib/jsonapi/query_parser.ex b/lib/jsonapi/query_parser.ex
index c8fe705..f1ddcc5 100644
--- a/lib/jsonapi/query_parser.ex
+++ b/lib/jsonapi/query_parser.ex
@@ -181,7 +181,7 @@ defmodule JSONAPI.QueryParser do
       if inc =~ ~r/\w+\.\w+/ do
         acc ++ handle_nested_include(inc, valid_includes, config)
       else
-        inc = String.to_atom(inc)
+        inc = inc |> dash() |> String.to_existing_atom()
 
         if Enum.any?(valid_includes, fn {key, _val} -> key == inc end) do
           acc ++ [inc]
diff --git a/lib/jsonapi/serializer.ex b/lib/jsonapi/serializer.ex
index 338e827..04574df 100644
--- a/lib/jsonapi/serializer.ex
+++ b/lib/jsonapi/serializer.ex
@@ -125,7 +125,7 @@ defmodule JSONAPI.Serializer do
   defp generate_view_tuple(view) when is_atom(view), do: {view, :include}
 
   def is_data_loaded?(rel_data) do
-    assoc_loaded?(rel_data) && (is_map(rel_data) || (is_list(rel_data) && !Enum.empty?(rel_data)))
+    assoc_loaded?(rel_data) && (is_map(rel_data) || is_list(rel_data))
   end
 
   def encode_relation({rel_view, rel_data, _rel_url, _conn} = info) do


Test patch:
diff --git a/test/jsonapi_query_parser_test.exs b/test/jsonapi_query_parser_test.exs
index d637833..8980ddc 100644
--- a/test/jsonapi_query_parser_test.exs
+++ b/test/jsonapi_query_parser_test.exs
@@ -11,7 +11,11 @@ defmodule JSONAPI.QueryParserTest do
     def type, do: "mytype"
 
     def relationships do
-      [author: JSONAPI.QueryParserTest.UserView, comments: JSONAPI.QueryParserTest.CommentView]
+      [
+        author: JSONAPI.QueryParserTest.UserView,
+        comments: JSONAPI.QueryParserTest.CommentView,
+        best_friends: JSONAPI.QueryParsertTest.UserView
+      ]
     end
   end
 
@@ -67,6 +71,7 @@ defmodule JSONAPI.QueryParserTest do
     assert parse_include(config, "author").includes == [:author]
     assert parse_include(config, "comments,author").includes == [:comments, :author]
     assert parse_include(config, "comments.user").includes == [comments: :user]
+    assert parse_include(config, "best_friends").includes == [:best_friends]
 
     assert_raise ArgumentError, "argument error", fn ->
       assert parse_include(config, "author.top-posts")
@@ -75,6 +80,7 @@ defmodule JSONAPI.QueryParserTest do
     Application.put_env(:jsonapi, :underscore_to_dash, true)
 
     assert parse_include(config, "author.top-posts").includes == [author: :top_posts]
+    assert parse_include(config, "best-friends").includes == [:best_friends]
 
     Application.delete_env(:jsonapi, :underscore_to_dash)
   end
diff --git a/test/serializer_test.exs b/test/serializer_test.exs
index 449a7da..195b2f1 100644
--- a/test/serializer_test.exs
+++ b/test/serializer_test.exs
@@ -181,7 +181,8 @@ defmodule JSONAPISerializerTest do
     assert attributes[:body] == data[:body]
 
     assert encoded_data[:links][:self] == PostView.url_for(data, nil)
-    assert map_size(encoded_data[:relationships]) == 1
+    assert map_size(encoded_data[:relationships]) == 2
+    assert encoded_data[:relationships][:best_comments][:data] == []
 
     assert Enum.count(encoded[:included]) == 1
   end