Merge branch 'relationship-links'

This commit is contained in:
Yanick Champoux 2023-11-01 17:39:20 -04:00
commit 3736a8ba36
2 changed files with 30 additions and 1 deletions

View File

@ -114,6 +114,10 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
$s->{relationships}{ $key }{data} = obj_ref($t->{data},\@inc); $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; push @$included, @inc if $included;
} }

View File

@ -69,14 +69,39 @@ $registry->add_type(
}, },
relationships => { relationships => {
'tags' => { type => 'tag' }, '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('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 } ); 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 => { like $output => {
"jsonapi"=> { "jsonapi"=> {
"version"=> "1.0" "version"=> "1.0"
@ -110,4 +135,4 @@ like $output => {
}], }],
}; };
done_testing(); done_testing;