misc Schema changes

This commit is contained in:
Yanick Champoux 2023-11-13 13:12:48 -05:00
parent e93942cb61
commit 2e125c1771
2 changed files with 10 additions and 7 deletions

View File

@ -115,7 +115,7 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
my $s = { my $s = {
type => $self->type, type => $self->type,
id => $self->gen_id($data) id => $self->gen_id( $data, $extra_data )
}; };
if ( $self->links ) { if ( $self->links ) {
@ -134,12 +134,13 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
my $t = $self->registry->serialize( $relationships{$key}{type}, my $t = $self->registry->serialize( $relationships{$key}{type},
$attr, \@inc ); $attr, \@inc );
$s->{relationships}{$key}{data} = obj_ref( $t->{data}, \@inc ); if ( my $data = obj_ref( $t->{data}, \@inc ) ) {
$s->{relationships}{$key}{data} = $data;
}
if ( my $links = $relationships{$key}{links} ) { if ( my $links = $relationships{$key}{links} ) {
$s->{relationships}{$key}{links} = $s->{relationships}{$key}{links} =
gen_links( $links, $s->{relationships}{$key}{data}, gen_links( $links, $data, $extra_data );
$extra_data );
} }
push @$included, @inc if $included; push @$included, @inc if $included;
@ -163,15 +164,17 @@ sub obj_ref ( $data, $included ) {
return $data if keys %$data == 2; return $data if keys %$data == 2;
return unless keys %$data;
push @$included, $data; push @$included, $data;
return +{ $data->%{qw/ id type/} }; return +{ $data->%{qw/ id type/} };
} }
sub gen_id ( $self, $data ) { sub gen_id ( $self, $data, $xtra ) {
my $id = $self->id; my $id = $self->id;
return ref $id ? $id->($data) : $data->{$id}; return ref $id ? $id->( $data, $xtra ) : $data->{$id};
} }
sub gen_links ( $links, $data, $extra_data = {} ) { sub gen_links ( $links, $data, $extra_data = {} ) {

View File

@ -22,7 +22,7 @@ is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
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' }