relationship links
This commit is contained in:
parent
63ead8b937
commit
57eeb08419
@ -114,6 +114,10 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
|
||||
|
||||
$s->{relationships}{ $key }{data} = obj_ref($t->{data},\@inc);
|
||||
|
||||
if( my $links = $relationships{$key}{links} ) {
|
||||
$s->{relationships}{$key}{links} = gen_links($links,$s->{relationships}{ $key }{data}, $extra_data );
|
||||
}
|
||||
|
||||
push @$included, @inc if $included;
|
||||
}
|
||||
|
||||
|
27
t/example.t
27
t/example.t
@ -69,14 +69,39 @@ $registry->add_type(
|
||||
},
|
||||
relationships => {
|
||||
'tags' => { type => 'tag' },
|
||||
'comments' => { type => 'comment' },
|
||||
author => {
|
||||
type => "people",
|
||||
links => sub ( $data, @ ) {
|
||||
return +{
|
||||
self => "/articles/"
|
||||
. $data->{id}
|
||||
. "/relationships/author",
|
||||
related => "/articles/" . $data->{id} . "/author"
|
||||
};
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
$registry->add_type('tag');
|
||||
$registry->add_type( 'comment',
|
||||
{ id => '_id', allowed_attributes => ['body'] } );
|
||||
$registry->add_type(
|
||||
'people',
|
||||
{ links => sub ( $data, @ ) { '/peoples/' . $data->{id} }
|
||||
} );
|
||||
|
||||
my $output = $registry->serialize('article', $data, { count => 2 } );
|
||||
|
||||
like $output->{data}[0]{relationships}{author}, {
|
||||
links => {
|
||||
"self" => "/articles/1/relationships/author",
|
||||
"related" => "/articles/1/author"
|
||||
}
|
||||
};
|
||||
|
||||
like $output => {
|
||||
"jsonapi"=> {
|
||||
"version"=> "1.0"
|
||||
@ -110,4 +135,4 @@ like $output => {
|
||||
}],
|
||||
};
|
||||
|
||||
done_testing();
|
||||
done_testing;
|
||||
|
Loading…
Reference in New Issue
Block a user