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: avoid crash if ingredients services called without ingredients_lc #11055

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

Conversation

stephanegigandet
Copy link
Contributor

fixes #11047

In the future, it would be a good thing to enable the services to add error messages that could be returned to the caller, when there is missing data for instance.

@stephanegigandet stephanegigandet requested a review from a team as a code owner November 25, 2024 15:12
@github-actions github-actions bot added 🥗🔍 Ingredients analysis https://wiki.openfoodfacts.org/Ingredients_Extraction_and_Analysis 🥗 Ingredients labels Nov 25, 2024
@@ -1604,7 +1604,7 @@ Array of specific ingredients.

sub parse_origins_from_text ($product_ref, $text) {

my $ingredients_lc = $product_ref->{ingredients_lc} || $product_ref->{lc};
my $ingredients_lc = $product_ref->{ingredients_lc} || $product_ref->{lc} || $product_ref->{lang};
Copy link
Member

Choose a reason for hiding this comment

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

This is where, if we add an Object approach, we could have a method for that on product. (would also work to define a sub get_product_lang)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm refactoring it

@@ -1766,15 +1766,24 @@ sub parse_ingredients_text_service ($product_ref, $updated_product_fields_ref) {
# and indicate that the service is creating the "ingredients" structure
$updated_product_fields_ref->{ingredients} = 1;

return if ((not defined $product_ref->{ingredients_text}) or ($product_ref->{ingredients_text} eq ""));
# $product_ref->{ingredients_lc} is defined in extract_ingredients_from_text()
my $ingredients_lc = $product_ref->{ingredients_lc} || $product_ref->{lc} || $product_ref->{lang};
Copy link
Member

Choose a reason for hiding this comment

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

can we document this ingredients_lc property (I wasn't aware of it).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is in the OpenAPI spec:

  ingredients_lc:
    type: string
    description: |
      Language that was used to parse the ingredient list. If `ingredients_text` is available
      for the product main language (`lang`), `ingredients_lc=lang`, otherwise we look at
      `ingredients_text` fields for other languages and set `ingredients_lc` to the first
      non-empty `ingredient_text`.

Copy link
Member

Choose a reason for hiding this comment

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

Didn't you add a test for this scenario ?

@github-actions github-actions bot added API Issues related to the Open Food Facts API. More specific labels exist & should be used (API WRITE…) 🖼️ Images 🧪 tests 📍 Origins Origins are used for Eco-Score computation. We want to have structured origins. Products labels Nov 26, 2024
Copy link

sonarcloud bot commented Nov 26, 2024

@stephanegigandet
Copy link
Contributor Author

@alexgarel Thank you for the feedback.

I refactored some things, and then that led to refactoring more things:

  • get_or_select_ingredients_lc() : new function called by services that expect ingredients_lc to be set
  • select_ingredients_lc() : @language_codes may not be computed is called from a service, so finding existing fields differently
    also sets ingredients_text if needed
  • APIProductServices: added a way for services to pass back error messages that can be reported in the API response
  • added tests for the parse_ingredients_text service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Issues related to the Open Food Facts API. More specific labels exist & should be used (API WRITE…) 🖼️ Images 🥗🔍 Ingredients analysis https://wiki.openfoodfacts.org/Ingredients_Extraction_and_Analysis 🥗 Ingredients 📍 Origins Origins are used for Eco-Score computation. We want to have structured origins. Products 🧪 tests
Projects
Status: Reviewer approved
Development

Successfully merging this pull request may close these issues.

crash in ingredient analysis service: Undef $lc in call to get_string_id_for_lang
2 participants