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

(-)a/Koha/Object.pm (-7 / +27 lines)
Lines 373-388 sub _numeric_column_type { Link Here
373
373
374
=head3 to_api
374
=head3 to_api
375
375
376
    my $object_for_api = $object->to_api;
376
    my $object_for_api = $object->to_api(
377
        {
378
          [ embed => [
379
              {
380
                  accessor => 'items',
381
                  children => [
382
                      {
383
                          accessor => 'holds,
384
                          children => [
385
                              ...
386
                          ]
387
                      }
388
                  ]
389
              },
390
              ...
391
          ] ]
392
        }
393
    );
377
394
378
Returns a representation of the object, suitable for API output.
395
Returns a representation of the object, suitable for API output.
379
396
380
=cut
397
=cut
381
398
382
sub to_api {
399
sub to_api {
383
    my ( $self, $embeds ) = @_;
400
    my ( $self, $params ) = @_;
384
    my $json_object = $self->TO_JSON;
401
    my $json_object = $self->TO_JSON;
385
402
403
    my $embeds = $params->{embed};
404
386
    # Rename attributes if there's a mapping
405
    # Rename attributes if there's a mapping
387
    if ( $self->can('to_api_mapping') ) {
406
    if ( $self->can('to_api_mapping') ) {
388
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
407
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
Lines 404-420 sub to_api { Link Here
404
423
405
    if ($embeds) {
424
    if ($embeds) {
406
        foreach my $embed (@$embeds) {
425
        foreach my $embed (@$embeds) {
407
            my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2;
426
            my $curr = $embed->{accessor};
408
            my @nxembeds;
427
            my $next = $embed->{children};
409
428
410
            @nxembeds = ($next) if $next;
429
            my @nxembeds;
430
            @nxembeds = @{ $next } if $next;
411
431
412
            my $children = $self->$curr;
432
            my $children = $self->$curr;
413
            if ( ref $children eq 'ARRAY' ) {
433
            if ( ref $children eq 'ARRAY' ) {
414
                my @list;
434
                my @list;
415
                my $pos = 0;
435
                my $pos = 0;
416
                foreach my $child (@$children) {
436
                foreach my $child (@$children) {
417
                    my $res = $child->to_api( \@nxembeds );
437
                    my $res = $child->to_api({ embed => \@nxembeds });
418
                    $res = { $json_object->{$curr}->[$pos], $res }
438
                    $res = { $json_object->{$curr}->[$pos], $res }
419
                      if defined $json_object->{$curr}
439
                      if defined $json_object->{$curr}
420
                      && defined $json_object->{$curr}->[$pos];
440
                      && defined $json_object->{$curr}->[$pos];
Lines 424-430 sub to_api { Link Here
424
                $json_object->{$curr} = \@list;
444
                $json_object->{$curr} = \@list;
425
            }
445
            }
426
            else {
446
            else {
427
                my $res = $children->to_api( \@nxembeds );
447
                my $res = $children->to_api({ embed => \@nxembeds });
428
                $res = { $json_object->{$curr}, $res }
448
                $res = { $json_object->{$curr}, $res }
429
                  if defined $json_object->{$curr};
449
                  if defined $json_object->{$curr};
430
                $json_object->{$curr} = $res;
450
                $json_object->{$curr} = $res;
(-)a/Koha/Objects.pm (-2 / +2 lines)
Lines 315-323 Returns a representation of the objects, suitable for API output . Link Here
315
=cut
315
=cut
316
316
317
sub to_api {
317
sub to_api {
318
    my ($self, $embeds) = @_;
318
    my ($self, $params) = @_;
319
319
320
    return [ map { $_->to_api($embeds) } $self->as_list ];
320
    return [ map { $_->to_api($params) } $self->as_list ];
321
}
321
}
322
322
323
=head3 Koha::Objects->_wrap
323
=head3 Koha::Objects->_wrap
(-)a/t/db_dependent/Koha/Object.t (-23 / +31 lines)
Lines 215-221 subtest 'TO_JSON tests' => sub { Link Here
215
215
216
subtest "to_api() tests" => sub {
216
subtest "to_api() tests" => sub {
217
217
218
    plan tests => 18;
218
    plan tests => 20;
219
219
220
    $schema->storage->txn_begin;
220
    $schema->storage->txn_begin;
221
221
Lines 262-305 subtest "to_api() tests" => sub { Link Here
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
264
264
265
    my $item_class = Test::MockModule->new('Koha::Item');
266
    $item_class->mock( 'to_api_mapping',
267
        sub {
268
            return {
269
                itemnumber       => 'item_id'
270
            };
271
        }
272
    );
273
274
    my $hold_class = Test::MockModule->new('Koha::Hold');
275
    $hold_class->mock( 'to_api_mapping',
276
        sub {
277
            return {
278
                reserve_id       => 'hold_id'
279
            };
280
        }
281
    );
282
283
    my $biblio = $builder->build_sample_biblio();
265
    my $biblio = $builder->build_sample_biblio();
284
    my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
266
    my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
285
    my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
267
    my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
286
268
287
    my @embeds = ('items');
269
    my @embeds = (
270
        {
271
            accessor => 'items'
272
        }
273
    );
288
274
289
    my $biblio_api = $biblio->to_api(\@embeds);
275
    my $biblio_api = $biblio->to_api({ embed => \@embeds });
290
276
291
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
277
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
292
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
278
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
293
    ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
279
    ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
294
280
295
    @embeds = ('items.holds');
281
    @embeds = (
296
    $biblio_api = $biblio->to_api(\@embeds);
282
        {
283
            accessor => 'items',
284
            children => [
285
                {
286
                    accessor => 'holds'
287
                }
288
            ]
289
        }
290
    );
291
    $biblio_api = $biblio->to_api({ embed => \@embeds });
297
292
298
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
293
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
299
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
294
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
300
    ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
295
    ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
301
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
296
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
302
297
298
    my $hold_api = $hold->to_api(
299
        {
300
            embed => [
301
                {
302
                    accessor => 'item'
303
                }
304
            ]
305
        }
306
    );
307
308
    is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' );
309
    is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
310
303
    $schema->storage->txn_rollback;
311
    $schema->storage->txn_rollback;
304
};
312
};
305
313
(-)a/t/db_dependent/Koha/Objects.t (-2 / +72 lines)
Lines 283-289 subtest '->search() tests' => sub { Link Here
283
283
284
subtest "to_api() tests" => sub {
284
subtest "to_api() tests" => sub {
285
285
286
    plan tests => 4;
286
    plan tests => 18;
287
287
288
    $schema->storage->txn_begin;
288
    $schema->storage->txn_begin;
289
289
Lines 303-308 subtest "to_api() tests" => sub { Link Here
303
    is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
303
    is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
304
    is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
304
    is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
305
305
306
    my $biblio_1 = $builder->build_sample_biblio();
307
    my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
308
    my $hold_1   = $builder->build_object(
309
        {
310
            class => 'Koha::Holds',
311
            value => { itemnumber => $item_1->itemnumber }
312
        }
313
    );
314
315
    my $biblio_2 = $builder->build_sample_biblio();
316
    my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
317
    my $hold_2   = $builder->build_object(
318
        {
319
            class => 'Koha::Holds',
320
            value => { itemnumber => $item_2->itemnumber }
321
        }
322
    );
323
324
    my @embed = (
325
        {
326
            accessor => 'items'
327
        }
328
    );
329
330
    my $i = 0;
331
    my @items = ( $item_1, $item_2 );
332
    my @holds = ( $hold_1, $hold_2 );
333
334
    my $biblios_api = Koha::Biblios->search(
335
        {
336
            biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
337
        }
338
    )->to_api( { embed => \@embed } );
339
340
    foreach my $biblio_api ( @{ $biblios_api } ) {
341
        ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
342
        is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
343
        ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
344
345
        $i++;
346
    }
347
348
    # One more level
349
    @embed = (
350
        {
351
            accessor => 'items',
352
            children => [
353
                {
354
                    accessor => 'holds'
355
                }
356
            ]
357
        }
358
    );
359
    $i = 0;
360
361
    $biblios_api = Koha::Biblios->search(
362
        {
363
            biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
364
        }
365
    )->to_api( { embed => \@embed } );
366
367
    foreach my $biblio_api ( @{ $biblios_api } ) {
368
369
        ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
370
        is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
371
        ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
372
        is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
373
374
        $i++;
375
    }
376
306
    $schema->storage->txn_rollback;
377
    $schema->storage->txn_rollback;
307
};
378
};
308
379
309
- 

Return to bug 24228