-
-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect nesting of nutrients and sub-nutrients in /cgi/nutrients.pl API #5997
Comments
Not 100% related, but there seems to be a little difference between
|
It is the expected behaviour. If you use a subdomain like [country code].openfoodfacts.org, it will set a default country cc and a default language lc. For instance ch.openfoodfacts.org will set the country to Switzerland, and the language to German. If you use ?cc= or ?lc= to override the country or language, it will work, but setting ?cc will only override the country, it will not set a default language. You can do this: https://world.openfoodfacts.org/cgi/nutrients.pl?cc=fr&lc=fr One thing about the nutrients.pl API is that you will get different results per country (which nutrients and in which order), and by language (translation of the name of the nutrient). |
@stephanegigandet Understood. |
I don't code in
|
This is my suggestion (worked on "europe" data). The fix is about:
my @table = ();
my %parent_level0;
my %parent_level1;
foreach (@{$nutriments_tables{$nutriment_table}}) {
my $nid = $_; # Copy instead of alias
$nid =~/^#/ and next;
my $important = ($nid =~ /^!/) ? JSON::PP::true : JSON::PP::false;
$nid =~ s/!//g;
my $default_edit_form = $nid =~ /-$/ ? JSON::PP::false : JSON::PP::true;
$nid =~ s/-$//g;
my $onid = $nid =~ s/^(\-+)//gr;
my $prefix_length = 0;
if ($nid =~ s/^--//g) {
$prefix_length = 2;
} elsif($nid =~ s/^-//g) {
$prefix_length = 1;
}
my %current = ( id => $onid, important => $important, display_in_edit_form => $default_edit_form );
my $current_ref = \%current;
my $name = get_nutrient_label($onid, $lc);
if (defined $name) {
$current_ref->{name} = $name;
}
if ($prefix_length eq 1) {
@{$parent_level0->{nutrients}} = () unless defined $parent_level0->{nutrients};
push @{$parent_level0->{nutrients}}, $current_ref unless not defined $current_ref;
}
elsif ($prefix_length eq 2) {
@{$parent_level1->{nutrients}} = () unless defined $parent_level1->{nutrients};
push @{$parent_level1->{nutrients}}, $current_ref unless not defined $current_ref;
}
elsif ($prefix_length eq 0) {
push @table, $current_ref unless not defined $current_ref;
}
else {
print "unexpected case";
}
if ($prefix_length eq 0) {
$parent_level0 = $current_ref;
} elsif ($prefix_length eq 1) {
$parent_level1 = $current_ref;
}
}
my %result = ( nutrients => \@table );
my $data = encode_json(\%result);
print header( -type => 'application/json', -content_language => $lc, -charset => 'utf-8', -access_control_allow_origin => '*', -cache_control => 'public, max-age: 86400' ) . $data; In addition to that, there's a funny hierarchy in 'fiber', # should probably be '-fiber'
'--soluble-fiber-',
'--insoluble-fiber-', |
@monsieurtanuki Thank you very much for the fix, I made a corresponding PR here: #6031 |
Describe the bug
https://world.openfoodfacts.org/cgi/nutrients.pl returns a nested structure with nutrients so that apps can know how to display nutrition table, in which order etc. (the result varies based on the country code cc parameter).
The nutrients are not nested correctly though.
e.g.
To Reproduce
https://world.openfoodfacts.org/cgi/nutrients.pl
Expected behavior
Nutrients should be in the order defined in Food.pm
Screenshots
No response
Additional context
Reported by @monsieurtanuki : openfoodfacts/openfoodfacts-dart#210 (comment)
Type of device
REST-API
Browser version
No response
Number of products impacted
No response
Time per product
No response
The text was updated successfully, but these errors were encountered: