misc Schema changes

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

View File

@ -22,7 +22,7 @@ is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
my $serialized = schema_serialize(
{ 'type' => 'thing',
id => sub ($data) { $data->{x} . $data->{y} },
id => sub ( $data, @ ) { $data->{x} . $data->{y} },
links => { self => '/some/url' },
},
{ x => '1', y => '2' }