add allowed_attributes

This commit is contained in:
Yanick Champoux 2023-11-01 16:03:28 -04:00
parent fb5cd7ce3a
commit 63ead8b937
2 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,8 @@ use Moo;
use experimental qw/ signatures /; use experimental qw/ signatures /;
use List::AllUtils qw/ pairmap pairgrep /; use List::AllUtils qw/ pairmap pairgrep /;
use Set::Object qw/set/;
=head1 ATTRIBUTES =head1 ATTRIBUTES
=head2 type =head2 type
@ -38,6 +40,7 @@ has top_level_meta => (is => 'ro');
has relationships => (is => 'ro', default => sub { +{} }); has relationships => (is => 'ro', default => sub { +{} });
has registry => ( is => 'ro' ); has registry => ( is => 'ro' );
has allowed_attributes => ( is => 'ro');
=head1 METHODS =head1 METHODS
@ -116,6 +119,11 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
delete $s->{attributes} unless $s->{attributes}->%*; delete $s->{attributes} unless $s->{attributes}->%*;
if( $self->allowed_attributes ) {
delete $s->{attributes}{$_} for (
set( keys $s->{attributes}->%* ) - set($self->allowed_attributes->@* ) )->@*;
}
return $s; return $s;
} }

View File

@ -77,5 +77,16 @@ subtest 'a single scalar == id', sub {
}; };
}; };
subtest 'allowed_attributes', sub {
my $serialized = Dancer2::Plugin::JsonApi::Registry::Schema->new(
type => 'thing',
allowed_attributes => ['foo'],
)->serialize( { id => 1, foo => 2, bar => 3 } );
is $serialized->{data} => {
type => 'thing',
id => 1,
attributes => { foo => 2, } };
};
done_testing(); done_testing();