diff --git a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm index c919575..4d7ca7c 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm @@ -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; } diff --git a/t/example.t b/t/example.t index 6127a05..eb47b65 100644 --- a/t/example.t +++ b/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;