no data field when there is no data

This commit is contained in:
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',
'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";
@ -47,21 +48,18 @@ like(
}
}
)->serialize( {}, { bar => 'yup' } ),
{ meta => { foo => 1, 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'});
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing', )
->serialize( { id => 1, foo => 'bar' } );
is $serialized->{data} => {
type => 'thing',
id => 1,
attributes => {
foo => 'bar',
}
attributes => { foo => 'bar', }
};
};
@ -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();