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,28 +7,29 @@ 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',
|
||||||
id => 'foo'
|
id => 'foo'
|
||||||
)->serialize( { foo => '123' } )->{data}{id} => '123',
|
)->serialize( { foo => '123' } )->{data}{id} => '123',
|
||||||
'custom id'
|
'custom id'
|
||||||
);
|
);
|
||||||
|
|
||||||
my $serialized = schema_serialize(
|
my $serialized = schema_serialize(
|
||||||
{ 'type' => 'thing',
|
{ 'type' => 'thing',
|
||||||
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'
|
||||||
);
|
);
|
||||||
|
|
||||||
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
|
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
|
||||||
|
|
||||||
@ -39,29 +40,26 @@ sub schema_serialize ( $schema, $data ) {
|
|||||||
|
|
||||||
like(
|
like(
|
||||||
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
Dancer2::Plugin::JsonApi::Registry::Schema->new(
|
||||||
type => 'thing',
|
type => 'thing',
|
||||||
top_level_meta => {
|
top_level_meta => {
|
||||||
foo => 1,
|
foo => 1,
|
||||||
bar => sub ( $data, $xtra ) {
|
bar => sub ( $data, $xtra ) {
|
||||||
$xtra->{bar};
|
$xtra->{bar};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)->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',
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -69,11 +67,11 @@ subtest 'attributes' => sub {
|
|||||||
subtest 'a single scalar == id', sub {
|
subtest 'a single scalar == id', sub {
|
||||||
my $serialized =
|
my $serialized =
|
||||||
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
|
Dancer2::Plugin::JsonApi::Registry::Schema->new( type => 'thing' )
|
||||||
->serialize('blah');
|
->serialize('blah');
|
||||||
|
|
||||||
is $serialized->{data} => {
|
is $serialized->{data} => {
|
||||||
type => 'thing',
|
type => 'thing',
|
||||||
id => 'blah',
|
id => 'blah',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -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