davorg-cpan / moox-role-json_ld

Moo role to easily add JSON-LD generation into a class
1 stars 5 forks source link

Unable to handle multivalued properties that are also do JSON_LD #22

Closed jwrightecs closed 4 years ago

jwrightecs commented 5 years ago

Many (most) of the properties of schema.org schemata are multivalued, including nested ones, for example Organization.employees, Person.children, MusicComposition.composer (https://schema.org/Person#music-2), but such properties seem to not be supported by this role:

use Data::Dumper;
{
        package LD::Employee;
        use Moo;
        use Types::Standard qw/Str ArrayRef InstanceOf/;
        use MooX::JSON_LD 'Person';

        has name => (
                is => 'ro',
                json_ld => 'name',
        );
        no Moo;
        no MooX::JSON_LD;
}

{
        package LD::Org;
        use Moo;
        use MooX::JSON_LD 'Organization';
        use Types::Standard qw/ArrayRef InstanceOf/;

        has 'org_name' => (
                is => 'ro',
                json_ld => 'name',
        );
        has employees => (
                is => 'rw',
                isa => ArrayRef->of(InstanceOf->of("LD::Employee")),
                json_ld => 'employee',
        );
}

my $e1 = LD::Employee->new( name => 'Joe Soap' );
my $e2 = LD::Employee->new( name => 'John Brown' );
my $employees = [$e1, $e2];
print Dumper( map $_->json_ld, @$employees );
my $o = new LD::Org( org_name => 'Foo Inc' );
$o->employees([ $e1, $e2 ]);
print Dumper( $o->json_ld );

Returns the error encountered object 'LD::Employee=HASH(0x27ab040)', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) at /home/perl/perl5/lib/perl5/MooX/Role/JSON_LD.pm line 233

davorg commented 5 years ago

Interesting bug. Thanks. I think the fix has two parts:

  1. Add convert_blessed() to the JSON encoder attribute
  2. Alias TO_JSON() to json_ld_data() in the class

I'll look at getting a fix out in the next couple of days.

davorg commented 4 years ago

It took more than a "couple of days", I'm afraid, but I've just released version 0.0.17 which fixes this bug. It'll be on CPAN soon. Sorry for the delay and thanks for the report.