From 5dd5e664e90dc1e494c5d511aaf8632e79f7e24e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 1 Nov 2023 15:21:06 -0400 Subject: [PATCH] scalar data are ids --- lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm | 3 +++ t/registry-schema.t | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm index 07183d3..281cb0b 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm @@ -85,6 +85,9 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) { return [ map { $self->serialize_data($_,$extra_data, $included) } @$data ] if ref $data eq 'ARRAY'; + # it's a scalar? it's the id + return { id => $data, type => $self->type } unless ref $data; + my $s = { type => $self->type, id => $self->gen_id($data) }; diff --git a/t/registry-schema.t b/t/registry-schema.t index d0aa8cb..a7036f7 100644 --- a/t/registry-schema.t +++ b/t/registry-schema.t @@ -66,6 +66,16 @@ subtest 'attributes' => sub { }; +subtest 'a single scalar == id', sub { + my $serialized = + Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' ) + ->serialize('blah'); + + is $serialized->{data} => { + type => 'thing', + id => 'blah', + }; +}; done_testing();