Skip to content

Commit

Permalink
Merge branch 'master' into mickey/remove_single_valued_arrayref_to_sc…
Browse files Browse the repository at this point in the history
…alar
  • Loading branch information
oalders committed Jul 5, 2017
2 parents 9660c9d + 76d7e8e commit 23becd0
Showing 1 changed file with 1 addition and 48 deletions.
49 changes: 1 addition & 48 deletions lib/MetaCPAN/Web/Model/API.pm
Expand Up @@ -104,7 +104,7 @@ sub request {

if ( $content_type =~ /^application\/json/ ) {
my $out;
eval { $out = $self->process_json_response($data); };
eval { $out = decode_json($data); };
return $out
if $out;
}
Expand All @@ -115,53 +115,6 @@ sub request {
);
}

sub process_json_response {
my ( $self, $data ) = @_;

# Let json error propagate.
my $json = decode_json($data);

$self->_strip_source_prefix_from_fields($json);

return $json;
}

sub _strip_source_prefix_from_fields {
my ( $self, $json ) = @_;

# There appears to be a bug in older (than 0.90) ES versions where
# "A stored boolean field is being returned as a string, not as a boolean"
# when requested via "fields". To work around this we can specify
# "_source.blah" in "fields", then we strip the "_source." prefix here.
# https://github.com/metacpan/metacpan-web/issues/881
# https://github.com/elasticsearch/elasticsearch/issues/2551
# See .../API/Release.pm for examples of this.

# Don't autovivify.
if ( exists( $json->{hits} ) && exists( $json->{hits}->{hits} ) ) {
foreach my $hit ( @{ $json->{hits}->{hits} } ) {

next unless exists $hit->{fields};

my $fields = $hit->{fields};
foreach my $orig ( keys %$fields ) {
my $key = $orig; # copy

# Strip '_source.' prefix from keys.
if ( $key =~ s/^_source\.// ) {
warn "Field $key overwritten with $orig in ${\ref $self}"
if exists $fields->{$key};

# Update original reference.
$fields->{$key} = delete $fields->{$orig};
}
}
}
}

# No return, reference is modified.
}

# cache these
my $encoding = Encode::find_encoding('utf-8-strict')
or warn 'UTF-8 Encoding object not found';
Expand Down

0 comments on commit 23becd0

Please sign in to comment.