jsonapi field in the ::Schema

This commit is contained in:
Yanick Champoux 2023-10-31 15:54:28 -04:00
parent e679ab442d
commit d51ee14eac
2 changed files with 16 additions and 14 deletions

View File

@ -1,7 +1,7 @@
package Dancer2::Plugin::JsonApi::Registry; package Dancer2::Plugin::JsonApi::Registry;
use 5.32.0; use 5.32.0;
use Dancer2::Plugin::JsonApi::Registry::Type; use Dancer2::Plugin::JsonApi::Registry::Schema;
use Carp; use Carp;
@ -15,14 +15,14 @@ The registry for the different types of data managed by the plugin.
=head1 METHODS =head1 METHODS
=head2 serialize($type,$data,$meta={}) =head2 serialize($type,$data,$extra_data={})
Returns the serialized form of C<$data>. Returns the serialized form of C<$data>.
=cut =cut
sub serialize($self,$type,$data,$meta={}) { sub serialize($self,$type,$data,$extra_data={}) {
return {}; return $self->type($type)->top_level_serialize($data,$extra_data);
} }
has types => ( has types => (
@ -37,7 +37,7 @@ Adds a data type to the registry.
=cut =cut
sub add_type($self,$type,$definition={}) { sub add_type($self,$type,$definition={}) {
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Type->new( $self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Schema->new(
type => $type, type => $type,
%$definition %$definition
); );

View File

@ -10,14 +10,16 @@ $registry->add_type(
people => { people => {
id => 'id', id => 'id',
links => { links => {
self => sub($data) { self => sub ( $data, @ ) {
return "/peoples/$data->{id}" return "/peoples/$data->{id}";
} }
} } } );
}
);
isa_ok $registry->type('people') => 'Dancer2::Plugin::JsonApi::Registry::Type'; isa_ok $registry->type('people') =>
'Dancer2::Plugin::JsonApi::Registry::Schema';
like(
$registry->serialize( people => {} ),
{ jsonapi => { version => '1.0' } } );
done_testing(); done_testing();