View | Details | Raw Unified | Return to bug 26635
Collapse All | Expand All

(-)a/Koha/Object.pm (-8 / +8 lines)
Lines 558-570 sub to_api { Link Here
558
    $params = defined $params ? {%$params} : {};
558
    $params = defined $params ? {%$params} : {};
559
559
560
    # children should be able to handle without
560
    # children should be able to handle without
561
    my $embeds    = delete $params->{embed};
561
    my $embeds  = delete $params->{embed};
562
    my $av_expand = delete $params->{av_expand};
562
    my $strings = delete $params->{strings};
563
563
564
    # coded values handling
564
    # coded values handling
565
    my $avs = {};
565
    my $avs = {};
566
    if ( $av_expand and $self->can('api_av_mapping') ) {
566
    if ( $strings and $self->can('api_strings_mapping') ) {
567
        $avs = $self->api_av_mapping($params);
567
        $avs = $self->api_strings_mapping($params);
568
    }
568
    }
569
569
570
    # Remove forbidden attributes if required (including their coded values)
570
    # Remove forbidden attributes if required (including their coded values)
Lines 573-579 sub to_api { Link Here
573
            delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list };
573
            delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list };
574
        }
574
        }
575
575
576
        if ( $av_expand ) {
576
        if ( $strings ) {
577
            foreach my $field (keys %{$avs}) {
577
            foreach my $field (keys %{$avs}) {
578
                delete $avs->{$field}
578
                delete $avs->{$field}
579
                    unless any { $_ eq $field } @{ $self->public_read_list };
579
                    unless any { $_ eq $field } @{ $self->public_read_list };
Lines 606-612 sub to_api { Link Here
606
    }
606
    }
607
607
608
    $json_object->{_strings} = $avs
608
    $json_object->{_strings} = $avs
609
      if $av_expand;
609
      if $strings;
610
610
611
    if ($embeds) {
611
    if ($embeds) {
612
        foreach my $embed ( keys %{$embeds} ) {
612
        foreach my $embed ( keys %{$embeds} ) {
Lines 620-627 sub to_api { Link Here
620
                my $curr = $embed;
620
                my $curr = $embed;
621
                my $next = $embeds->{$curr}->{children};
621
                my $next = $embeds->{$curr}->{children};
622
622
623
                $params->{av_expand} = 1
623
                $params->{strings} = 1
624
                  if $embeds->{$embed}->{av_expand};
624
                  if $embeds->{$embed}->{strings};
625
625
626
                my $children = $self->$curr;
626
                my $children = $self->$curr;
627
627
(-)a/Koha/REST/Plugin/Objects.pm (-6 / +6 lines)
Lines 53-60 the requested object. It passes through any embeds if specified. Link Here
53
            my $attributes = {};
53
            my $attributes = {};
54
54
55
            # Look for embeds
55
            # Look for embeds
56
            my $embed     = $c->stash('koha.embed');
56
            my $embed   = $c->stash('koha.embed');
57
            my $av_expand = $c->stash('koha.av_expand');
57
            my $strings = $c->stash('koha.strings');
58
58
59
            # Generate prefetches for embedded stuff
59
            # Generate prefetches for embedded stuff
60
            $c->dbic_merge_prefetch(
60
            $c->dbic_merge_prefetch(
Lines 68-74 the requested object. It passes through any embeds if specified. Link Here
68
68
69
            return unless $object;
69
            return unless $object;
70
70
71
            return $object->to_api({ embed => $embed, av_expand => $av_expand });
71
            return $object->to_api({ embed => $embed, strings => $strings });
72
        }
72
        }
73
    );
73
    );
74
74
Lines 99-106 shouldn't be called twice in it. Link Here
99
            # Privileged reques?
99
            # Privileged reques?
100
            my $is_public = $c->stash('is_public');
100
            my $is_public = $c->stash('is_public');
101
            # Look for embeds
101
            # Look for embeds
102
            my $embed     = $c->stash('koha.embed');
102
            my $embed   = $c->stash('koha.embed');
103
            my $av_expand = $c->stash('koha.av_expand');
103
            my $strings = $c->stash('koha.strings');
104
104
105
            # Merge sorting into query attributes
105
            # Merge sorting into query attributes
106
            $c->dbic_merge_sorting(
106
            $c->dbic_merge_sorting(
Lines 205-211 shouldn't be called twice in it. Link Here
205
                }
205
                }
206
            );
206
            );
207
207
208
            return $objects->to_api({ embed => $embed, public => $is_public, av_expand => $av_expand });
208
            return $objects->to_api({ embed => $embed, public => $is_public, strings => $strings });
209
        }
209
        }
210
    );
210
    );
211
}
211
}
(-)a/Koha/REST/Plugin/Query.pm (-4 / +4 lines)
Lines 242-249 Unwraps and stashes the x-koha-embed headers for use later query construction Link Here
242
            if ($embed_header) {
242
            if ($embed_header) {
243
                my $THE_embed = {};
243
                my $THE_embed = {};
244
                foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
244
                foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
245
                    if ( $embed_req eq '+av_expand' ) {    # special case
245
                    if ( $embed_req eq '+strings' ) {    # special case
246
                        $c->stash( 'koha.av_expand' => 1 );
246
                        $c->stash( 'koha.strings' => 1 );
247
                    } else {
247
                    } else {
248
                        _merge_embed( _parse_embed($embed_req), $THE_embed );
248
                        _merge_embed( _parse_embed($embed_req), $THE_embed );
249
                    }
249
                    }
Lines 368-376 sub _parse_embed { Link Here
368
            my $key = $+{relation} . "_count";
368
            my $key = $+{relation} . "_count";
369
            $result->{$key} = { is_count => 1 };
369
            $result->{$key} = { is_count => 1 };
370
        }
370
        }
371
        elsif ( $curr =~ m/^(?<relation>.*)\+av_expand/ ) {
371
        elsif ( $curr =~ m/^(?<relation>.*)\+strings/ ) {
372
            my $key = $+{relation};
372
            my $key = $+{relation};
373
            $result->{$key} = { av_expand => 1 };
373
            $result->{$key} = { strings => 1 };
374
        }
374
        }
375
        else {
375
        else {
376
            $result->{$curr} = {};
376
            $result->{$curr} = {};
(-)a/t/Koha/REST/Plugin/Query.t (-11 / +11 lines)
Lines 210-223 get '/stash_embed' => sub { Link Here
210
    my $c = shift;
210
    my $c = shift;
211
211
212
    $c->stash_embed();
212
    $c->stash_embed();
213
    my $embed     = $c->stash('koha.embed');
213
    my $embed   = $c->stash('koha.embed');
214
    my $av_expand = $c->stash('koha.av_expand');
214
    my $strings = $c->stash('koha.strings');
215
215
216
    $c->render(
216
    $c->render(
217
        status => 200,
217
        status => 200,
218
        json   => {
218
        json   => {
219
            av_expand => $av_expand,
219
            strings => $strings,
220
            embed     => $embed,
220
            embed   => $embed,
221
        }
221
        }
222
    );
222
    );
223
};
223
};
Lines 452-470 subtest 'stash_embed() tests' => sub { Link Here
452
            patron    => {}
452
            patron    => {}
453
        });
453
        });
454
454
455
    $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item+av_expand,patron+av_expand' } )
455
    $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item+strings,patron+strings' } )
456
      ->json_is( '/embed' => {
456
      ->json_is( '/embed' => {
457
            checkouts => { children => { item => { av_expand => 1 } } },
457
            checkouts => { children => { item => { strings => 1 } } },
458
            patron    => { av_expand => 1 }
458
            patron    => { strings => 1 }
459
        })
459
        })
460
      ->json_is( '/av_expand' => undef );
460
      ->json_is( '/strings' => undef );
461
461
462
    $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts+av_expand,checkouts.item,patron,+av_expand' } )
462
    $t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts+strings,checkouts.item,patron,+strings' } )
463
      ->json_is( '/embed' => {
463
      ->json_is( '/embed' => {
464
            checkouts => { children => { item => { } }, av_expand => 1 },
464
            checkouts => { children => { item => { } }, strings => 1 },
465
            patron    => { }
465
            patron    => { }
466
        })
466
        })
467
      ->json_is( '/av_expand' => 1 );
467
      ->json_is( '/strings' => 1 );
468
};
468
};
469
469
470
subtest 'stash_overrides() tests' => sub {
470
subtest 'stash_overrides() tests' => sub {
(-)a/t/db_dependent/Koha/Object.t (-12 / +12 lines)
Lines 303-309 subtest "to_api() tests" => sub { Link Here
303
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
303
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
304
    is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
304
    is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
305
305
306
    my $_str = {
306
    my $_strings = {
307
        location => {
307
        location => {
308
            category => 'ASD',
308
            category => 'ASD',
309
            str      => 'Estante alto',
309
            str      => 'Estante alto',
Lines 311-328 subtest "to_api() tests" => sub { Link Here
311
        }
311
        }
312
    };
312
    };
313
313
314
    # mock Koha::Item so it implements 'api_av_mapping'
314
    # mock Koha::Item so it implements 'api_strings_mapping'
315
    my $item_mock = Test::MockModule->new('Koha::Item');
315
    my $item_mock = Test::MockModule->new('Koha::Item');
316
    $item_mock->mock(
316
    $item_mock->mock(
317
        'api_av_mapping',
317
        'api_strings_mapping',
318
        sub {
318
        sub {
319
            return $_str;
319
            return $_strings;
320
        }
320
        }
321
    );
321
    );
322
322
323
    my $hold_api = $hold->to_api(
323
    my $hold_api = $hold->to_api(
324
        {
324
        {
325
            embed => { 'item' => { av_expand => 1 } }
325
            embed => { 'item' => { strings => 1 } }
326
        }
326
        }
327
    );
327
    );
328
328
Lines 330-337 subtest "to_api() tests" => sub { Link Here
330
    is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
330
    is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
331
    is_deeply(
331
    is_deeply(
332
        $hold_api->{item}->{_strings},
332
        $hold_api->{item}->{_strings},
333
        $_str,
333
        $_strings,
334
        '_str correctly added to nested embed'
334
        '_strings correctly added to nested embed'
335
    );
335
    );
336
336
337
    # biblio with no items
337
    # biblio with no items
Lines 475-481 subtest "to_api() tests" => sub { Link Here
475
475
476
        my $city_mock = Test::MockModule->new('Koha::City');
476
        my $city_mock = Test::MockModule->new('Koha::City');
477
        $city_mock->mock(
477
        $city_mock->mock(
478
            'api_av_mapping',
478
            'api_strings_mapping',
479
            sub {
479
            sub {
480
                my ( $self, $params ) = @_;
480
                my ( $self, $params ) = @_;
481
481
Lines 508-518 subtest "to_api() tests" => sub { Link Here
508
            }
508
            }
509
        );
509
        );
510
510
511
        my $mobj = $marseille->to_api( { av_expand => 1, public => 1 } );
511
        my $mobj = $marseille->to_api( { strings => 1, public => 1 } );
512
        my $cobj = $cordoba->to_api( { av_expand => 1, public => 0 } );
512
        my $cobj = $cordoba->to_api( { strings => 1, public => 0 } );
513
513
514
        ok( exists $mobj->{_strings}, '_str exists for Marseille' );
514
        ok( exists $mobj->{_strings}, '_strings exists for Marseille' );
515
        ok( exists $cobj->{_strings}, '_str exists for Córdoba' );
515
        ok( exists $cobj->{_strings}, '_strings exists for Córdoba' );
516
516
517
        is_deeply(
517
        is_deeply(
518
            $mobj->{_strings}->{country},
518
            $mobj->{_strings}->{country},
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-6 / +5 lines)
Lines 673-679 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
673
673
674
    my $city_class = Test::MockModule->new('Koha::City');
674
    my $city_class = Test::MockModule->new('Koha::City');
675
    $city_class->mock(
675
    $city_class->mock(
676
        'api_av_mapping',
676
        'api_strings_mapping',
677
        sub {
677
        sub {
678
            my ($self, $params) = @_;
678
            my ($self, $params) = @_;
679
            use Koha::AuthorisedValues;
679
            use Koha::AuthorisedValues;
Lines 714-720 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
714
        }
714
        }
715
    );
715
    );
716
716
717
    $t->get_ok( '/cities/' . $manuel->id => { 'x-koha-embed' => '+av_expand' } )
717
    $t->get_ok( '/cities/' . $manuel->id => { 'x-koha-embed' => '+strings' } )
718
      ->status_is(200)->json_is( '/name' => 'Manuel' )
718
      ->status_is(200)->json_is( '/name' => 'Manuel' )
719
      ->json_has('/_strings')
719
      ->json_has('/_strings')
720
      ->json_is( '/_strings/country/type'     => 'av' )
720
      ->json_is( '/_strings/country/type'     => 'av' )
Lines 725-731 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
725
      ->status_is(200)->json_is( '/name' => 'Manuel' )
725
      ->status_is(200)->json_is( '/name' => 'Manuel' )
726
      ->json_hasnt('/_strings');
726
      ->json_hasnt('/_strings');
727
727
728
    $t->get_ok( '/cities/' . $manuela->id => { 'x-koha-embed' => '+av_expand' } )
728
    $t->get_ok( '/cities/' . $manuela->id => { 'x-koha-embed' => '+strings' } )
729
      ->status_is(200)->json_is( '/name' => 'Manuela' )
729
      ->status_is(200)->json_is( '/name' => 'Manuela' )
730
      ->json_has('/_strings')
730
      ->json_has('/_strings')
731
      ->json_is( '/_strings/country/type'     => 'av' )
731
      ->json_is( '/_strings/country/type'     => 'av' )
Lines 786-792 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
786
786
787
    my $city_class = Test::MockModule->new('Koha::City');
787
    my $city_class = Test::MockModule->new('Koha::City');
788
    $city_class->mock(
788
    $city_class->mock(
789
        'api_av_mapping',
789
        'api_strings_mapping',
790
        sub {
790
        sub {
791
            my ($self, $params) = @_;
791
            my ($self, $params) = @_;
792
            use Koha::AuthorisedValues;
792
            use Koha::AuthorisedValues;
Lines 829-835 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
829
    );
829
    );
830
830
831
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
831
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
832
          { 'x-koha-embed' => '+av_expand' } )->status_is(200)
832
          { 'x-koha-embed' => '+strings' } )->status_is(200)
833
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
833
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
834
      ->json_is( '/0/name' => 'Manuel' )
834
      ->json_is( '/0/name' => 'Manuel' )
835
      ->json_has('/0/_strings')
835
      ->json_has('/0/_strings')
836
- 

Return to bug 26635