Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 1 addition & 48 deletions lib/MetaCPAN/Web/Model/API.pm
Original file line number Diff line number Diff line change
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