no data field when there is no data
This commit is contained in:
parent
67c4d58b9d
commit
ebbb5f9466
@ -59,7 +59,10 @@ sub serialize ( $self, $data, $extra_data = {} ) {
|
|||||||
|
|
||||||
my @included;
|
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 )
|
$serial->{links} = gen_links( $self->top_level_links, $data, $extra_data )
|
||||||
if $self->top_level_links;
|
if $self->top_level_links;
|
||||||
|
@ -7,10 +7,10 @@ use experimental qw/ signatures /;
|
|||||||
my $type =
|
my $type =
|
||||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
|
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
|
||||||
|
|
||||||
like $type->serialize( { attr1 => 'a', id => '123' },
|
like $type->serialize( { attr1 => 'a', id => '123' }, { foo => 1 } ) => {
|
||||||
{ foo => 1 } ) => {
|
|
||||||
jsonapi => { version => '1.0' },
|
jsonapi => { version => '1.0' },
|
||||||
data => { type => 'thing', id => '123' } };
|
data => { type => 'thing', id => '123' }
|
||||||
|
};
|
||||||
|
|
||||||
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||||
'type' => 'thing',
|
'type' => 'thing',
|
||||||
@ -24,7 +24,8 @@ my $serialized = schema_serialize(
|
|||||||
id => sub ($data) { $data->{x} . $data->{y} },
|
id => sub ($data) { $data->{x} . $data->{y} },
|
||||||
links => { self => '/some/url' },
|
links => { self => '/some/url' },
|
||||||
},
|
},
|
||||||
{ x => '1', y => '2' } );
|
{ x => '1', y => '2' }
|
||||||
|
);
|
||||||
|
|
||||||
is( $serialized->{data}{id} => '12',
|
is( $serialized->{data}{id} => '12',
|
||||||
'custom id, function'
|
'custom id, function'
|
||||||
@ -47,21 +48,18 @@ like(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)->serialize( {}, { bar => 'yup' } ),
|
)->serialize( {}, { bar => 'yup' } ),
|
||||||
{ meta => { foo => 1, bar => 'yup' } } );
|
{ meta => { foo => 1, bar => 'yup' } }
|
||||||
|
);
|
||||||
|
|
||||||
subtest 'attributes' => sub {
|
subtest 'attributes' => sub {
|
||||||
my $serialized =
|
my $serialized =
|
||||||
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing', )
|
||||||
type => 'thing',
|
->serialize( { id => 1, foo => 'bar' } );
|
||||||
)->serialize( { id => 1, foo => 'bar'});
|
|
||||||
|
|
||||||
is $serialized->{data} => {
|
is $serialized->{data} => {
|
||||||
type => 'thing',
|
type => 'thing',
|
||||||
id => 1,
|
id => 1,
|
||||||
attributes => {
|
attributes => { foo => 'bar', }
|
||||||
foo => 'bar',
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -86,7 +84,16 @@ subtest 'allowed_attributes', sub {
|
|||||||
is $serialized->{data} => {
|
is $serialized->{data} => {
|
||||||
type => 'thing',
|
type => 'thing',
|
||||||
id => 1,
|
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();
|
done_testing();
|
||||||
|
Loading…
Reference in New Issue
Block a user