Skip to content

Commit

Permalink
fix: use allow_non_ref in JSON decoding of off-query (openfoodfacts#1…
Browse files Browse the repository at this point in the history
…0587)

We recently switched from JSON::PP to Cpanel::JSON::XS, and it looks
like JSON::PP decode_json() was accepting to decode raw numbers, without
having to specify allow_non_ref.

We had an issue when trying to deploy to prod, because on count
requests, the return value we get from off-query is a raw number, which
could not get decoded:
  • Loading branch information
stephanegigandet authored Jul 23, 2024
1 parent 7f1d4ba commit 1a8e82b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/ProductOpener/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ sub execute_count_tags_query ($query) {
return execute_tags_query('count', $query);
}

# $json has utf8 disabled: it encodes to Perl Unicode strings
my $json = JSON::MaybeXS->new->utf8(0)->allow_nonref->canonical;

sub execute_tags_query ($type, $query) {
if ((defined $query_url) and (length($query_url) > 0)) {
$query_url =~ s/^\s+|\s+$//g;
Expand All @@ -137,7 +140,7 @@ sub execute_tags_query ($type, $query) {
'Content-Type' => 'application/json; charset=utf-8'
);
if ($resp->is_success) {
return decode_json($resp->decoded_content);
return $json->decode($resp->decoded_content);
}
else {
$log->warn(
Expand Down

0 comments on commit 1a8e82b

Please sign in to comment.