Skip to content

Commit

Permalink
fix: parsing of facets in URL (#9816)
Browse files Browse the repository at this point in the history
Fixes #9708 (category url not working) and add a test
  • Loading branch information
stephanegigandet authored Feb 19, 2024
1 parent 59bc324 commit 3989825
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
34 changes: 9 additions & 25 deletions lib/ProductOpener/Routing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -454,23 +454,18 @@ sub analyze_request ($request_ref) {
$request_ref->{canon_rel_url} = '';
my $canon_rel_url_suffix = '';

#check if last field is number
if (($#components >= 1) and ($components[-1] =~ /^\d+$/)) {
#if first field or third field is tags (plural) then last field is page number
if ( defined $tag_type_from_plural{$lc}{$components[0]}
or defined $tag_type_from_plural{"en"}{$components[0]}
or defined $tag_type_from_plural{$lc}{$components[2]}
or defined $tag_type_from_plural{"en"}{$components[2]})
{
# We may have a page number
if ($#components >= 0) {
# The last component can be a page number
if (($components[-1] =~ /^\d+$/) and ($components[-1] <= 1000)) {
$request_ref->{page} = pop @components;
$log->debug("get page number", {$request_ref->{page}}) if $log->is_debug();
$log->debug("got a page number", {$request_ref->{page}}) if $log->is_debug();
}
}
# list of tags? (plural of tagtype must be the last field)

$log->debug("checking last component",
{last_component => $components[-1], is_plural => $tag_type_from_plural{$lc}{$components[-1]}})
if $log->is_debug();
# Extract tag type / tag value pairs and store them in an array $request_ref->{tags}
# e.g. /category/breakfast-cereals/label/organic/brand/monoprix
extract_tagtype_and_tag_value_pairs_from_components($request_ref, \@components);

# list of (categories) tags with stats for a nutriment
if ( ($#components == 1)
Expand All @@ -490,6 +485,7 @@ sub analyze_request ($request_ref) {
if $log->is_debug();
}

# list of tags? (plural of tagtype must be the last field)
if (defined $tag_type_from_plural{$lc}{$components[-1]}) {

$request_ref->{groupby_tagtype} = $tag_type_from_plural{$lc}{pop @components};
Expand All @@ -507,24 +503,12 @@ sub analyze_request ($request_ref) {
if $log->is_debug();
}

# Extract tag type / tag value pairs and store them in an array $request_ref->{tags}
# e.g. /category/breakfast-cereals/label/organic/brand/monoprix
extract_tagtype_and_tag_value_pairs_from_components($request_ref, \@components);

# Old Open Food Hunt points
if ((defined $components[0]) and ($components[0] eq 'points')) {
$request_ref->{points} = 1;
$request_ref->{canon_rel_url} .= "/points";
}

# We may have a page number
if ($#components >= 0) {
# The last component can be a page number
if (($components[-1] =~ /^\d+$/) and ($components[-1] <= 1000)) {
$request_ref->{page} = pop @components;
}
}

if ($#components >= 0) {
# We have a component left, but we don't know what it is
$log->warn("invalid address, confused by number of components left", {left_components => \@components})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"count" : 1,
"page" : 1,
"page_count" : 1,
"page_size" : 24,
"products" : [
{
"labels_tags" : [
"en:organic"
],
"product_name" : "Apples - Organic - France, Belgium, Canada"
}
],
"skip" : 0
}
13 changes: 12 additions & 1 deletion tests/integration/facets.t
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ my @products = (
(
code => '200000000011',
product_name => "Apples - Organic - France, Belgium, Canada",
categories => "en:apples",
categories => "en:apples,en:vitamins",
labels => "en:organic",
origins => "en:france,en:belgium,en:canada",
),
Expand All @@ -160,6 +160,8 @@ my @products = (
categories => "en:chocolate",
labels => "en:organic,en:fair-trade",
origins => "en:martinique",
lc => "en",
ingredients_text_en => "Cocoa, sugar, vanilla, vitamiun C",
),
},
# German product with accents in labels
Expand Down Expand Up @@ -339,6 +341,15 @@ my $tests_ref = [
expected_status_code => 200,
sort_products_by => 'product_name',
},
# facet name that is the same as a facet type
# https://github.com/openfoodfacts/openfoodfacts-server/issues/9708
{
test_case => 'category-vitamins',
method => 'GET',
path => '/category/vitamins.json?fields=product_name,labels_tags',
expected_status_code => 200,
sort_products_by => 'product_name',
},
];

# note: we need to execute the tests with bob, because we need authentication
Expand Down

0 comments on commit 3989825

Please sign in to comment.