From 43670163d2f277469a2163daccf2db98e758d4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Wed, 27 Oct 2021 15:40:05 +0000 Subject: [PATCH 1/4] fix: correct nesting of cgi/nutrient.pl API response #5997 --- cgi/nutrients.pl | 64 +++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/cgi/nutrients.pl b/cgi/nutrients.pl index 7495738b30bf2..aea642a512e7a 100644 --- a/cgi/nutrients.pl +++ b/cgi/nutrients.pl @@ -35,24 +35,16 @@ ProductOpener::Display::init(); -# Recursively remove parent association to avoid redundant JSON data. -sub _remove_parent { - my $current_ref = shift; - - if (defined $current_ref->{nutrients}) { - foreach my $nutrient (@{$current_ref->{nutrients}}) { - _remove_parent($nutrient); - } - } - - delete $current_ref->{parent}; - - return; -} +# Turn the flat nutriments table array into a nested array of nutrients +# The level of each nutrient is indicated by leading dashes before its id: +# nutrient +# -sub-nutrient +# --sub-sub-nutrient my @table = (); -my $previous_ref; -my $previous_prefix_length = 0; +my $parent_level0; +my $parent_level1; + foreach (@{$nutriments_tables{$nutriment_table}}) { my $nid = $_; # Copy instead of alias @@ -62,33 +54,37 @@ sub _remove_parent { my $default_edit_form = $nid =~ /-$/ ? JSON::PP::false : JSON::PP::true; $nid =~ s/-$//g; - my $onid = $nid =~ s/^(-+)//gr; - my $prefix_length = defined $1 ? length($1) : 0; - my %current = ( id => $onid, important => $important, display_in_edit_form => $default_edit_form ); - my $current_ref = \%current; + my $onid = $nid =~ s/^(\-+)//gr; + + my $current_ref = { id => $onid, important => $important, display_in_edit_form => $default_edit_form }; my $name = get_nutrient_label($onid, $lc); if (defined $name) { $current_ref->{name} = $name; } - if (($prefix_length gt 0) or ($prefix_length gt $previous_prefix_length)) { - @{$previous_ref->{nutrients}} = () unless defined $previous_ref->{nutrients}; - push @{$previous_ref->{nutrients}}, $current_ref unless not defined $current_ref; - $current_ref->{parent} = $previous_ref; + my $prefix_length = 0; + if ($nid =~ s/^--//g) { + $prefix_length = 2; + } elsif($nid =~ s/^-//g) { + $prefix_length = 1; } - else { + + if ($prefix_length == 0) { + # I'm on level 2, my parent is the latest level 1 parent push @table, $current_ref unless not defined $current_ref; + $parent_level0 = $current_ref; } - - if (($prefix_length gt $previous_prefix_length) or ($prefix_length eq 0)) { - $previous_ref = $current_ref; - $previous_prefix_length = $prefix_length; + elsif ($prefix_length == 1) { + # I'm on level 0, I have no parent, and I'm the latest level 0 parent + @{$parent_level0->{nutrients}} = () unless defined $parent_level0->{nutrients}; + push @{$parent_level0->{nutrients}}, $current_ref unless not defined $current_ref; + $parent_level1 = $current_ref; + } + elsif ($prefix_length == 2) { + # I'm on level 1, my parent is the latest level 0 parent, and I'm the latest level 1 parent + @{$parent_level1->{nutrients}} = () unless defined $parent_level1->{nutrients}; + push @{$parent_level1->{nutrients}}, $current_ref unless not defined $current_ref; } -} - -# The parent attribute is only used to build up the structure. Just remove it here to avoid circular dependency in JSON. -foreach my $nutrient (@table) { - _remove_parent($nutrient); } my %result = ( nutrients => \@table ); From 6387e11f6806ae0166b5f56c138ea5fa824c2c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Thu, 28 Oct 2021 08:13:05 +0000 Subject: [PATCH 2/4] fix level of soluble/insoluble fibers --- lib/ProductOpener/Food.pm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/ProductOpener/Food.pm b/lib/ProductOpener/Food.pm index 2b51c4c13f685..88dda1d35aaaa 100644 --- a/lib/ProductOpener/Food.pm +++ b/lib/ProductOpener/Food.pm @@ -530,8 +530,8 @@ sub mmoll_to_unit { '-starch-', '-polyols-', 'fiber', - '--soluble-fiber-', - '--insoluble-fiber-', + '-soluble-fiber-', + '-insoluble-fiber-', '!proteins', '-casein-', '-serum-proteins-', @@ -634,8 +634,8 @@ sub mmoll_to_unit { 'cholesterol', '!carbohydrates', '-fiber', - '--soluble-fiber-', - '--insoluble-fiber-', + '-soluble-fiber-', + '-insoluble-fiber-', '-sugars', '--sucrose-', '--glucose-', @@ -863,8 +863,8 @@ sub mmoll_to_unit { 'sodium', '!carbohydrates', '-fiber', - '--soluble-fiber-', - '--insoluble-fiber-', + '-soluble-fiber-', + '-insoluble-fiber-', '-sugars', '-added-sugars', '--sucrose-', @@ -972,8 +972,8 @@ sub mmoll_to_unit { 'sodium', '!carbohydrates', '-fiber', - '--soluble-fiber-', - '--insoluble-fiber-', + '-soluble-fiber-', + '-insoluble-fiber-', '-sugars', '--sucrose-', '--glucose-', From 9814e5af76e321cc0361ef24f85be11742fb4991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Thu, 28 Oct 2021 08:13:15 +0000 Subject: [PATCH 3/4] small fixes --- cgi/nutrients.pl | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/cgi/nutrients.pl b/cgi/nutrients.pl index aea642a512e7a..8e3a9aa35b98f 100644 --- a/cgi/nutrients.pl +++ b/cgi/nutrients.pl @@ -3,7 +3,7 @@ # This file is part of Product Opener. # # Product Opener -# Copyright (C) 2011-2019 Association Open Food Facts +# Copyright (C) 2011-2021 Association Open Food Facts # Contact: contact@openfoodfacts.org # Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France # @@ -30,6 +30,7 @@ use ProductOpener::Display qw/:all/; use ProductOpener::Food qw/:all/; +use Log::Any qw($log); use CGI qw/:cgi :form escapeHTML/; use JSON::PP; @@ -70,20 +71,24 @@ } if ($prefix_length == 0) { - # I'm on level 2, my parent is the latest level 1 parent - push @table, $current_ref unless not defined $current_ref; + # I'm on level 0, I have no parent, and I'm the new level 0 parent + push @table, $current_ref; $parent_level0 = $current_ref; + $parent_level1 = undef; } - elsif ($prefix_length == 1) { - # I'm on level 0, I have no parent, and I'm the latest level 0 parent - @{$parent_level0->{nutrients}} = () unless defined $parent_level0->{nutrients}; + elsif (($prefix_length == 1) and (defined $parent_level0)) { + # I'm on level 1, my parent is the latest level 0 parent, and I'm the new level 1 parent + defined $parent_level0->{nutrients} or $parent_level0->{nutrients} = []; push @{$parent_level0->{nutrients}}, $current_ref unless not defined $current_ref; $parent_level1 = $current_ref; } - elsif ($prefix_length == 2) { - # I'm on level 1, my parent is the latest level 0 parent, and I'm the latest level 1 parent - @{$parent_level1->{nutrients}} = () unless defined $parent_level1->{nutrients}; - push @{$parent_level1->{nutrients}}, $current_ref unless not defined $current_ref; + elsif (($prefix_length == 2) and (defined $parent_level1)) { + # I'm on level 2, my parent is the latest level 1 parent + defined $parent_level1->{nutrients} or $parent_level1->{nutrients} = []; + push @{$parent_level1->{nutrients}}, $current_ref; + } + else { + $log->error("invalid nesting of nutrients", { nutriment_table => $nutriment_table, nid => $nid, prefix_length => $prefix_length, current_ref => $current_ref, parent_level0 => $parent_level0, parent_level1 => $parent_level1 }) if $log->is_error(); } } From d77e094872e7a103a42723f1202ad4680ca75dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Fri, 29 Oct 2021 13:18:44 +0000 Subject: [PATCH 4/4] fix soluble/insoluble fiber --- lib/ProductOpener/Food.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ProductOpener/Food.pm b/lib/ProductOpener/Food.pm index 88dda1d35aaaa..c73cfc5e298bc 100644 --- a/lib/ProductOpener/Food.pm +++ b/lib/ProductOpener/Food.pm @@ -634,8 +634,8 @@ sub mmoll_to_unit { 'cholesterol', '!carbohydrates', '-fiber', - '-soluble-fiber-', - '-insoluble-fiber-', + '--soluble-fiber-', + '--insoluble-fiber-', '-sugars', '--sucrose-', '--glucose-', @@ -863,8 +863,8 @@ sub mmoll_to_unit { 'sodium', '!carbohydrates', '-fiber', - '-soluble-fiber-', - '-insoluble-fiber-', + '--soluble-fiber-', + '--insoluble-fiber-', '-sugars', '-added-sugars', '--sucrose-', @@ -972,8 +972,8 @@ sub mmoll_to_unit { 'sodium', '!carbohydrates', '-fiber', - '-soluble-fiber-', - '-insoluble-fiber-', + '--soluble-fiber-', + '--insoluble-fiber-', '-sugars', '--sucrose-', '--glucose-',