Skip to content

Commit d598d20

Browse files
authored
fix: explicit linkage of empty relationships (#357)
1 parent 71dc1f0 commit d598d20

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/ash_json_api/serializer.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ defmodule AshJsonApi.Serializer do
577577
type: AshJsonApi.Resource.Info.type(destination)
578578
})
579579

580+
%{__linkage__: %{^name => []}} ->
581+
Map.put(payload, :data, nil)
582+
580583
# There could be another case here if a bug in the system gave us a list
581584
# of more than one shouldn't happen though
582585

test/spec_compliance/fetching_data/inclusion_of_related_resources_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ defmodule AshJsonApiTest.FetchingData.InclusionOfRelatedResources do
160160
# --------------------------
161161
describe "include request parameter" do
162162
@describetag :spec_may
163+
test "resource endpoint with include param of an empty to-one relationship (linkage)" do
164+
post =
165+
Post
166+
|> Ash.Changeset.for_create(:create, %{name: "foo"})
167+
|> Ash.create!()
168+
169+
assert %{
170+
resp_body: %{
171+
"data" => %{
172+
"relationships" => %{
173+
"author" => %{"data" => nil}
174+
}
175+
}
176+
}
177+
} = get(Domain, "/posts/#{post.id}/?include=author", status: 200)
178+
end
179+
163180
test "resource endpoint with include param of to-one relationship (linkage)" do
164181
# GET /posts/1?include=author
165182
author =
@@ -212,6 +229,24 @@ defmodule AshJsonApiTest.FetchingData.InclusionOfRelatedResources do
212229
end)
213230
end
214231

232+
test "resource endpoint with include param of empty to-many relationship" do
233+
# GET /posts/1?include=comments
234+
post =
235+
Post
236+
|> Ash.Changeset.for_create(:create, %{name: "foo"})
237+
|> Ash.create!()
238+
239+
assert %{
240+
resp_body: %{
241+
"data" => %{
242+
"relationships" => %{
243+
"comments" => %{"data" => []}
244+
}
245+
}
246+
}
247+
} = get(Domain, "/posts/#{post.id}/?include=comments", status: 200)
248+
end
249+
215250
test "resource endpoint with include param of to-many relationship" do
216251
# GET /posts/1?include=comments
217252
author =

0 commit comments

Comments
 (0)