no data field when there is no data

releases
Yanick Champoux 2023-11-13 10:31:24 -05:00
parent 67c4d58b9d
commit ebbb5f9466
2 changed files with 35 additions and 25 deletions

View File

@ -59,7 +59,10 @@ sub serialize ( $self, $data, $extra_data = {} ) {
my @included;
$serial->{data} = $self->serialize_data( $data, $extra_data, \@included );
if ( defined $data ) {
$serial->{data} =
$self->serialize_data( $data, $extra_data, \@included );
}
$serial->{links} = gen_links( $self->top_level_links, $data, $extra_data )
if $self->top_level_links;

View File

@ -7,28 +7,29 @@ use experimental qw/ signatures /;
my $type =
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
like $type->serialize( { attr1 => 'a', id => '123' },
{ foo => 1 } ) => {
like $type->serialize( { attr1 => 'a', id => '123' }, { foo => 1 } ) => {
jsonapi => { version => '1.0' },
data => { type => 'thing', id => '123' } };
data => { type => 'thing', id => '123' }
};
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
'type' => 'thing',
id => 'foo'
)->serialize( { foo => '123' } )->{data}{id} => '123',
)->serialize( { foo => '123' } )->{data}{id} => '123',
'custom id'
);
);
my $serialized = schema_serialize(
{ 'type' => 'thing',
id => sub ($data) { $data->{x} . $data->{y} },
links => { self => '/some/url' },
},
{ x => '1', y => '2' } );
{ x => '1', y => '2' }
);
is( $serialized->{data}{id} => '12',
'custom id, function'
);
);
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
@ -39,29 +40,26 @@ sub schema_serialize ( $schema, $data ) {
like(
Dancer2::Plugin::JsonApi::Registry::Schema->new(
type => 'thing',
type => 'thing',
top_level_meta => {
foo => 1,
bar => sub ( $data, $xtra ) {
$xtra->{bar};
}
}
)->serialize( {}, { bar => 'yup' } ),
{ meta => { foo => 1, bar => 'yup' } } );
)->serialize( {}, { bar => 'yup' } ),
{ meta => { foo => 1, bar => 'yup' } }
);
subtest 'attributes' => sub {
my $serialized =
Dancer2::Plugin::JsonApi::Registry::Schema->new(
type => 'thing',
)->serialize( { id => 1, foo => 'bar'});
my $serialized =
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing', )
->serialize( { id => 1, foo => 'bar' } );
is $serialized->{data} => {
type => 'thing',
id => 1,
attributes => {
foo => 'bar',
}
type => 'thing',
id => 1,
attributes => { foo => 'bar', }
};
};
@ -69,11 +67,11 @@ subtest 'attributes' => sub {
subtest 'a single scalar == id', sub {
my $serialized =
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
->serialize('blah');
->serialize('blah');
is $serialized->{data} => {
type => 'thing',
id => 'blah',
type => 'thing',
id => 'blah',
};
};
@ -86,7 +84,16 @@ subtest 'allowed_attributes', sub {
is $serialized->{data} => {
type => 'thing',
id => 1,
attributes => { foo => 2, } };
attributes => { foo => 2, }
};
};
subtest 'empty data', sub {
my $serialized =
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
->serialize(undef);
ok( !$serialized->{data}, "there is no data" );
};
done_testing();