Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #581 from metacpan/tsibley/backwards-compat-when-f…
…ields-provided

Provide backwards compatibility for clients which request only specific fields
  • Loading branch information
ranguard committed Nov 27, 2016
2 parents 8a8a808 + f7850d3 commit c567b1c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
11 changes: 8 additions & 3 deletions lib/MetaCPAN/Server/Controller.pm
Expand Up @@ -82,7 +82,8 @@ sub get : Path('') : Args(1) {
if ( !defined $file ) {
$c->detach( '/not_found', ['Not found'] );
}
$c->stash( $file->{_source} || $file->{fields} )
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}
Expand Down Expand Up @@ -149,8 +150,12 @@ sub join : ActionClass('Deserialize') {
my $data
= $is_get
? [ $c->stash ]
: [ map { $_->{_source} || $_->{fields} }
@{ $c->stash->{hits}->{hits} } ];
: [
map {
$_->{_source}
|| single_valued_arrayref_to_scalar( $_->{fields} )
} @{ $c->stash->{hits}->{hits} }
];
my @ids = List::MoreUtils::uniq grep {defined}
map { ref $cself eq 'CODE' ? $cself->($_) : $_->{$cself} } @$data;
my $filter = { terms => { $config->{foreign} => [@ids] } };
Expand Down
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand Down Expand Up @@ -35,7 +36,8 @@ sub get : Path('') : Args(1) {
if ( !defined $file ) {
$c->detach( '/not_found', ['Not found'] );
}
my $st = $file->{_source} || $file->{fields};
my $st = $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} );
if ( $st and $st->{pauseid} ) {
$st->{release_count}
= $c->model('CPAN::Release')
Expand Down
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand All @@ -22,7 +23,8 @@ sub find : Path('') : Args(2) {
distribution => $distribution
}
);
$c->stash( $favorite->{_source} || $favorite->{fields} );
$c->stash( $favorite->{_source}
|| single_valued_arrayref_to_scalar( $favorite->{fields} ) );
} or $c->detach( '/not_found', [$@] );
}

Expand Down
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Server/Controller/File.pm
Expand Up @@ -5,6 +5,7 @@ use warnings;

use ElasticSearchX::Model::Util;
use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand Down Expand Up @@ -42,7 +43,8 @@ sub find : Path('') {
}
);
if ( $file->{_source} || $file->{fields} ) {
$c->stash( $file->{_source} || $file->{fields} );
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) );
}
} or $c->detach( '/not_found', [$@] );
}
Expand Down
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Server/Controller/Module.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller::File' }

Expand All @@ -15,7 +16,8 @@ sub get : Path('') : Args(1) {
if ( !defined $file ) {
$c->detach( '/not_found', [] );
}
$c->stash( $file->{_source} || $file->{fields} )
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}
Expand Down
7 changes: 5 additions & 2 deletions lib/MetaCPAN/Server/Controller/Release.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand All @@ -24,7 +25,8 @@ sub find : Path('') : Args(1) {
if ( !defined $file ) {
$c->detach( '/not_found', [] );
}
$c->stash( $file->{_source} || $file->{fields} )
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}
Expand All @@ -44,7 +46,8 @@ sub get : Path('') : Args(2) {
if ( !defined $file ) {
$c->detach( '/not_found', [] );
}
$c->stash( $file->{_source} || $file->{fields} )
$c->stash( $file->{_source}
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
|| $c->detach( '/not_found',
['The requested field(s) could not be found'] );
}
Expand Down
3 changes: 2 additions & 1 deletion lib/MetaCPAN/Util.pm
Expand Up @@ -8,7 +8,7 @@ use version;

use Digest::SHA1;
use Encode;
use Ref::Util qw( is_arrayref );
use Ref::Util qw( is_arrayref is_hashref );
use Sub::Exporter -setup => {
exports => [
'author_dir', 'digest',
Expand Down Expand Up @@ -137,6 +137,7 @@ sub single_valued_arrayref_to_scalar {
$fields ||= [];
my %fields_to_extract = map { $_ => 1 } @{$fields};
foreach my $hash ( @{$array} ) {
next unless is_hashref($hash);
foreach my $field ( %{$hash} ) {
next if ( $has_fields and not $fields_to_extract{$field} );
my $value = $hash->{$field};
Expand Down
2 changes: 1 addition & 1 deletion t/server/controller/module.t
Expand Up @@ -73,7 +73,7 @@ test_psgi app, sub {
elsif ( $k =~ /fields/ ) {
is_deeply(
$json,
{ documentation => ['Moose'], name => ['Moose.pm'] },
{ documentation => 'Moose', name => 'Moose.pm' },
'controller proxies field query parameter to ES'
);
}
Expand Down

0 comments on commit c567b1c

Please sign in to comment.