docker icon indicating copy to clipboard operation
docker copied to clipboard

Bug: Ingredients with empty common_name field are not saved when fetching from Open Food Facts

Open FrnklyN opened this issue 2 months ago • 0 comments

Description

When scanning products with the barcode scanner, ingredients from Open Food Facts that have an empty common_name field are fetched successfully but fail to save to the local database without any error message.

Steps to Reproduce

  1. Set up wger with Docker compose
  2. Configure DOWNLOAD_INGREDIENTS_FROM=WGER in prod.env
  3. Scan a product barcode that exists in Open Food Facts but has no common_name
  4. The app shows "not found" even though the product exists in OFF

Expected Behavior

  • Product should be fetched from Open Food Facts
  • Product should be saved to local database
  • Product should be available in the app after scanning

Actual Behavior

  • Product is fetched from Open Food Facts (✅)
  • API returns valid data including all required nutritional values (✅)
  • Product is NOT saved to database (❌)
  • No error is logged at INFO level (❌)
  • Function silently returns None

Root Cause

In wger/nutrition/models/ingredient.py, the fetch_ingredient_from_off() method has this check:

if not ingredient_data.name:
    return

if not ingredient_data.common_name:
    return  # ← THIS IS THE BUG

The problem is that common_name can be an empty string ('') for products that don't have a generic name in Open Food Facts.

In Python, not '' evaluates to True, so the function returns None without saving the ingredient.

Proposed Solution

The common_name field is optional and should not block saving.

Remove the check entirely

if not ingredient_data.name:
    return

# Remove the common_name check - it's optional

ingredient = cls(**ingredient_data.dict())
ingredient.save()

Testing

After removing the common_name check:

>>> from wger.nutrition.models import Ingredient
>>> result = Ingredient.fetch_ingredient_from_off('8720326078683')
>>> print(result)
Hagelslag Met Auto's
>>> print(result.pk)
689126

✅ Product is successfully saved!

Environment

  • wger version: latest Docker image
  • Docker compose setup
  • Products affected: Primarily Dutch/European products from Open Food Facts

Additional Notes

This bug particularly affects non-English speaking regions where products may not have a common_name translation. The error is silent (only logged at DEBUG level) which makes it very hard to diagnose.

FrnklyN avatar Nov 03 '25 21:11 FrnklyN