Skip to content

Commit 0629702

Browse files
committed
fix: ensure id is always coming back as a string
per jsonapi spec and generated spec
1 parent 8d3bb3a commit 0629702

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/ash_json_api/resource/resource.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,11 @@ defmodule AshJsonApi.Resource do
548548
[] ->
549549
# Expect resource to have only 1 primary key if :primary_key section is not used
550550
[key] = Ash.Resource.Info.primary_key(resource)
551-
Map.get(record, key)
551+
552+
case Map.get(record, key) do
553+
nil -> nil
554+
value -> to_string(value)
555+
end
552556

553557
keys ->
554558
delimiter = primary_key_delimiter(resource)

lib/ash_json_api/serializer.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ defmodule AshJsonApi.Serializer do
151151

152152
defp serialize_relationship_data(record, source_record, relationship) do
153153
%{
154-
id: record.id,
154+
id: AshJsonApi.Resource.encode_primary_key(record),
155155
type: AshJsonApi.Resource.Info.type(relationship.destination)
156156
}
157157
|> add_relationship_meta(record, source_record, relationship)
@@ -569,8 +569,11 @@ defmodule AshJsonApi.Serializer do
569569

570570
defp add_linkage(payload, record, %{destination: destination, cardinality: :one, name: name}) do
571571
case record do
572-
%{__linkage__: %{^name => [%{id: id}]}} ->
573-
Map.put(payload, :data, %{id: id, type: AshJsonApi.Resource.Info.type(destination)})
572+
%{__linkage__: %{^name => [record]}} ->
573+
Map.put(payload, :data, %{
574+
id: AshJsonApi.Resource.encode_primary_key(record),
575+
type: AshJsonApi.Resource.Info.type(destination)
576+
})
574577

575578
# There could be another case here if a bug in the system gave us a list
576579
# of more than one shouldn't happen though
@@ -594,7 +597,8 @@ defmodule AshJsonApi.Serializer do
594597
:data,
595598
Enum.map(
596599
linkage,
597-
&(%{id: &1.id, type: type} |> add_relationship_meta(&1, record, relationship))
600+
&(%{id: AshJsonApi.Resource.encode_primary_key(record), type: type}
601+
|> add_relationship_meta(&1, record, relationship))
598602
)
599603
)
600604

0 commit comments

Comments
 (0)