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

(-)a/Koha/Object.pm (-9 / +28 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
                items => {
380
                    children => {
381
                        holds => {,
382
                            children => {
383
                              ...
384
                            }
385
                        }
386
                    }
387
                },
388
                library => {
389
                    ...
390
                }
391
            },
392
            ...
393
         ]
394
        }
395
    );
377
396
378
Returns a representation of the object, suitable for API output.
397
Returns a representation of the object, suitable for API output.
379
398
380
=cut
399
=cut
381
400
382
sub to_api {
401
sub to_api {
383
    my ( $self, $embeds ) = @_;
402
    my ( $self, $params ) = @_;
384
    my $json_object = $self->TO_JSON;
403
    my $json_object = $self->TO_JSON;
385
404
405
    my $embeds = $params->{embed};
406
386
    # Rename attributes if there's a mapping
407
    # Rename attributes if there's a mapping
387
    if ( $self->can('to_api_mapping') ) {
408
    if ( $self->can('to_api_mapping') ) {
388
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
409
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
Lines 403-420 sub to_api { Link Here
403
    }
424
    }
404
425
405
    if ($embeds) {
426
    if ($embeds) {
406
        foreach my $embed (@$embeds) {
427
        foreach my $embed ( keys %{ $embeds } ) {
407
            my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2;
428
            my $curr = $embed;
408
            my @nxembeds;
429
            my $next = $embeds->{$curr}->{children};
409
410
            @nxembeds = ($next) if $next;
411
430
412
            my $children = $self->$curr;
431
            my $children = $self->$curr;
413
            if ( ref $children eq 'ARRAY' ) {
432
            if ( ref $children eq 'ARRAY' ) {
414
                my @list;
433
                my @list;
415
                my $pos = 0;
434
                my $pos = 0;
416
                foreach my $child (@$children) {
435
                foreach my $child (@$children) {
417
                    my $res = $child->to_api( \@nxembeds );
436
                    my $res = $child->to_api({ embed => $next });
418
                    $res = { $json_object->{$curr}->[$pos], $res }
437
                    $res = { $json_object->{$curr}->[$pos], $res }
419
                      if defined $json_object->{$curr}
438
                      if defined $json_object->{$curr}
420
                      && defined $json_object->{$curr}->[$pos];
439
                      && defined $json_object->{$curr}->[$pos];
Lines 424-430 sub to_api { Link Here
424
                $json_object->{$curr} = \@list;
443
                $json_object->{$curr} = \@list;
425
            }
444
            }
426
            else {
445
            else {
427
                my $res = $children->to_api( \@nxembeds );
446
                my $res = $children->to_api({ embed => $next });
428
                $res = { $json_object->{$curr}, $res }
447
                $res = { $json_object->{$curr}, $res }
429
                  if defined $json_object->{$curr};
448
                  if defined $json_object->{$curr};
430
                $json_object->{$curr} = $res;
449
                $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 / +24 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 => 21;
219
219
220
    $schema->storage->txn_begin;
220
    $schema->storage->txn_begin;
221
221
Lines 262-304 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 = { 'items' => {} };
288
270
289
    my $biblio_api = $biblio->to_api(\@embeds);
271
    my $biblio_api = $biblio->to_api({ embed => $embeds });
290
272
291
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
273
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
292
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
274
    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');
275
    ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
294
276
295
    @embeds = ('items.holds');
277
    $embeds = (
296
    $biblio_api = $biblio->to_api(\@embeds);
278
        {
279
            'items' => {
280
                'children' => {
281
                    'holds' => {}
282
                }
283
            },
284
            'biblioitem' => {}
285
        }
286
    );
287
    $biblio_api = $biblio->to_api({ embed => $embeds });
297
288
298
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
289
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
299
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
290
    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');
291
    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');
292
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
293
    is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
294
295
    my $hold_api = $hold->to_api(
296
        {
297
            embed => { 'item' => {} }
298
        }
299
    );
300
301
    is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' );
302
    is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
302
303
303
    $schema->storage->txn_rollback;
304
    $schema->storage->txn_rollback;
304
};
305
};
(-)a/t/db_dependent/Koha/Objects.t (-2 / +64 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 = { 'items' => {} };
325
326
    my $i = 0;
327
    my @items = ( $item_1, $item_2 );
328
    my @holds = ( $hold_1, $hold_2 );
329
330
    my $biblios_api = Koha::Biblios->search(
331
        {
332
            biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
333
        }
334
    )->to_api( { embed => $embed } );
335
336
    foreach my $biblio_api ( @{ $biblios_api } ) {
337
        ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
338
        is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
339
        ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
340
341
        $i++;
342
    }
343
344
    # One more level
345
    $embed = {
346
        'items' => {
347
            children => { 'holds' => {} }
348
        }
349
    };
350
351
    $i = 0;
352
353
    $biblios_api = Koha::Biblios->search(
354
        {
355
            biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
356
        }
357
    )->to_api( { embed => $embed } );
358
359
    foreach my $biblio_api ( @{ $biblios_api } ) {
360
361
        ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
362
        is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
363
        ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
364
        is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
365
366
        $i++;
367
    }
368
306
    $schema->storage->txn_rollback;
369
    $schema->storage->txn_rollback;
307
};
370
};
308
371
309
- 

Return to bug 24228