Repository: jeregrine/jsonapi (elixir)

Issue:
Problems with `self` pagination link
Hi. We're trying to upgrade to a version that includes https://github.com/jeregrine/jsonapi/pull/189, but have run into some problems.

Here's an example of some test regressions we have:
```elixir
  9) test index respects the page number 7 and size 3 (OurAppWeb.FooControllerTest)
     test/our_app_web/controllers/foo_controller_test.exs:342
     Assertion with == failed
     code:  assert resp["links"] == expected_pagination_links("foos", page_params)
     left:  %{"first" => "/api/v1/foos?page[page]=1&page[page-size]=3", "last" => "/api/v1/foos?page[page]=10&page[page-size]=3", "next" => "/api/v1/foos?page[page]=8&page[page-size]=3", "prev" => "/api/v1/foos?page[page]=6&page[page-size]=3", "self" => "http://localhost/foos"}
     right: %{"first" => "/api/v1/foos?page[page]=1&page[page-size]=3", "last" => "/api/v1/foos?page[page]=10&page[page-size]=3", "next" => "/api/v1/foos?page[page]=8&page[page-size]=3", "prev" => "/api/v1/foos?page[page]=6&page[page-size]=3", "self" => "/api/v1/foos?page[page]=7&page[page-size]=3"}
     stacktrace:
       test/our_app_web/controllers/foo_controller_test.exs:361: (test)
```

You see that the `self` value lacks the pagination params, pointing instead to a raw top-level `/foos` endpoint. I presume this comes from [view.url_for/2 here](https://github.com/jeregrine/jsonapi/pull/189/files?file-filters%5B%5D=.ex&file-filters%5B%5D=.exs&hide-deleted-files=true#diff-c6eda26dd75d06ea8b08bf45cb6f3f33R167)

In addition, I notice that [`@type links` in `paginator.ex` lacks the `self` key](https://github.com/jeregrine/jsonapi/pull/189/files?file-filters%5B%5D=.ex&file-filters%5B%5D=.exs&hide-deleted-files=true#diff-0e2f0db679e38d3135ee38439ff91b52R12). This seems at least tangentially related.

Pull request:
Fix #207 problems with 'self' pagination link
I was working with @kbaird when we came across the issue with #207.  This combines the version of `merge_base_links/4` previous to #189 and the implementation it introduced. This keeps all tests green while fixing our issue with `self` pagination links.

Patch:
diff --git a/lib/jsonapi/serializer.ex b/lib/jsonapi/serializer.ex
index 68de804..0f1d973 100644
--- a/lib/jsonapi/serializer.ex
+++ b/lib/jsonapi/serializer.ex
@@ -161,10 +161,9 @@ defmodule JSONAPI.Serializer do
 
   defp merge_base_links(%{links: links} = doc, data, view, conn) do
     view_links =
-      data
-      |> view.links(conn)
+      %{self: view.url_for(data, conn)}
+      |> Map.merge(view.links(data, conn))
       |> Map.merge(links)
-      |> Map.merge(%{self: view.url_for(data, conn)})
 
     Map.merge(doc, %{links: view_links})
   end


Test patch:
diff --git a/test/jsonapi/serializer_test.exs b/test/jsonapi/serializer_test.exs
index 14f13c8..7a9b460 100644
--- a/test/jsonapi/serializer_test.exs
+++ b/test/jsonapi/serializer_test.exs
@@ -45,7 +45,8 @@ defmodule JSONAPI.SerializerTest do
         first: view.url_for_pagination(data, conn, %{page | "page" => "1"}),
         last: view.url_for_pagination(data, conn, %{page | "page" => total_pages}),
         next: next_link(data, view, conn, number, size, total_pages),
-        prev: previous_link(data, view, conn, number, size)
+        prev: previous_link(data, view, conn, number, size),
+        self: view.url_for_pagination(data, conn, %{size: size, page: number})
       }
     end
 
@@ -596,11 +597,13 @@ defmodule JSONAPI.SerializerTest do
     page = conn.assigns.jsonapi_query.page
     first = view.url_for_pagination(data, conn, %{page | "page" => 1})
     last = view.url_for_pagination(data, conn, %{page | "page" => 3})
+    self = view.url_for_pagination(data, conn, page)
 
     assert encoded[:links][:first] == first
     assert encoded[:links][:last] == last
     assert encoded[:links][:next] == last
     assert encoded[:links][:prev] == first
+    assert encoded[:links][:self] == self
   end
 
   test "serialize does not include pagination links if they are not defined" do