Skip to content
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

fix: sort_by param issue in the url with "?" instead of "&" #10024

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

himanshisrestha
Copy link
Contributor

What

After searching for a product, and then applying a sort_by filter, the resulting url contained 2 sort_by params and "?" instead of "&"

Related issue(s) and discussion

@himanshisrestha himanshisrestha marked this pull request as ready for review March 26, 2024 11:29
@himanshisrestha himanshisrestha requested a review from a team as a code owner March 26, 2024 11:29
cgi/search.pl Outdated
Comment on lines 668 to 672
# if (defined $sort_by) {
# $current_link .= "&sort_by=$sort_by";
# }

$current_link .= "\&page_size=$limit";
# $current_link .= "\&page_size=$limit";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to remove it because we may let user sort on whatever they want through the API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In search.pl we also have different sort possibilities.

So I would say: use add_param here also, and maybe verify first that sort_by or page_size is not already in current_link.

@codecov-commenter
Copy link

codecov-commenter commented Mar 26, 2024

Codecov Report

Attention: Patch coverage is 0% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 48.59%. Comparing base (dc04d18) to head (ab2072d).
Report is 259 commits behind head on main.

Files Patch % Lines
lib/ProductOpener/Display.pm 0.00% 5 Missing ⚠️
lib/ProductOpener/Web.pm 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10024      +/-   ##
==========================================
- Coverage   49.54%   48.59%   -0.96%     
==========================================
  Files          67       71       +4     
  Lines       20650    20987     +337     
  Branches     4980     5029      +49     
==========================================
- Hits        10231    10198      -33     
- Misses       9131     9514     +383     
+ Partials     1288     1275      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alexgarel
Copy link
Member

@himanshisrestha the code looks good per se, but there seems to be an error.

Try http://world.openfoodfacts.localhost/cgi/search.pl and look at the permalink
Capture d’écran du 2024-03-26 15-53-26

It should contains the sort_by, it does not have it (while it's working on staging

@github-actions github-actions bot added the 💥 Merge Conflicts 💥 Merge Conflicts label Mar 26, 2024
@himanshisrestha
Copy link
Contributor Author

@alexgarel I've tried to replicate but I'vent seen what you mentioned.
The Permanent link from staging is here:
https://world.openfoodfacts.net/cgi/search.pl?action=process&search_terms=sugar&sort_by=unique_scans_n&page_size=20

and the local host is here:
http://world.openfoodfacts.localhost/cgi/search.pl?action=process&search_terms=sugar&sort_by=created_t&page_size=20
(Here sort_by =created_t because in search.pl url I added a filter for that)


=cut

sub add_params ($url, $params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put a more specific name, like add_params_to_link?


sub add_params ($url, $params) {

# Check if the URL already contains parameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to check if the URL already contains the same parameter we want to add, in which case we want to remove the old one before adding the new one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add unit tests for this function with various cases: param already there, other params, no other params etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to check if the URL already contains the same parameter we want to add, in which case we want to remove the old one before adding the new one.

Do we always explicitly limit it to one value when reading the parameters? cgi can do arrays

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanegigandet where should the test cases be written .. Is it in a file in tests/unit or should I create a .t file there for the same ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a tests/unit/web.t file.

cgi/search.pl Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the 💥 Merge Conflicts 💥 Merge Conflicts label Apr 1, 2024

=cut

sub add_params ($url, $params) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@himanshisrestha we can even consider having

sub add_params_to_link ($url, @params) {

@params is a list of parameters you want to add.

This enables calling the function using:

add_params_to_link($my_url, "test=foo", "othe=bar"); with as many parameters as we want.

@alexgarel
Copy link
Member

@himanshisrestha we would be glad if you can finish this PR !

@himanshisrestha
Copy link
Contributor Author

@alexgarel Sure I'm working on it !! 😄

Copy link

sonarcloud bot commented May 14, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@github-actions github-actions bot added the 💥 Merge Conflicts 💥 Merge Conflicts label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

Sort by button in search results does not work
6 participants