Skip to content

Fix nutrition amounts to match the displayed food serving - #135

Merged
xrpbanks merged 1 commit into
mainfrom
fix/food-serving-basis-20260908
Sep 8, 2026
Merged

Fix nutrition amounts to match the displayed food serving#135
xrpbanks merged 1 commit into
mainfrom
fix/food-serving-basis-20260908

Conversation

@xrpbanks

@xrpbanks xrpbanks commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Food search used Open Food Facts values per 100 g/ml while displaying the packaging serving size. As a result, selecting 50% of a displayed 250 ml serving could save half of the reference values instead of half of the serving values.

This PR makes the displayed amount and all four nutrient values use the same basis:

  • Prefer the provider's per-serving values when calories, protein, fat and carbohydrates are complete and the serving label is usable.
  • Otherwise use the complete 100 g/ml values and explicitly label them as the source reference amount.
  • Never combine nutrient bases or infer weight, volume or density from packaging text. Real zero values remain valid.

The existing portion picker scales this consistent amount. Existing food logs remain as recorded; this change does not retrospectively correct them. The deployment is backend-only and requires no WordPress plugin upload or database migration.

Validation: 160 targeted backend tests passed, including 18 new nutrition-basis cases. The new cases cover the reported serving/reference mismatch using synthetic representative data, missing and invalid values, missing or oversized labels, and zero macros. Whitespace, legal-boundary and tracked-secret checks passed.

Reference: Open Food Facts nutrition schema.

@xrpbanks
xrpbanks marked this pull request as ready for review September 8, 2026 04:33
Copilot AI lite review requested due to automatic review settings September 8, 2026 04:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The normalization logic matches the stated requirements and is covered by focused new tests for the key mismatch and edge cases.

Pull request overview

This PR fixes a mismatch in Open Food Facts search results where nutrient values were taken per 100 g/ml while the UI displayed a per-serving amount, ensuring the displayed serving/reference amount and all four macro fields (calories, protein, fat, carbohydrates) always share the same basis.

Changes:

  • Normalize Open Food Facts products to use per-serving macros only when the serving label is present/usable and all four per-serving values are complete; otherwise fall back to a clearly labeled 100 g/ml reference basis.
  • Add targeted backend tests covering serving-vs-reference selection rules, invalid/missing data, oversized labels, and zero-value macros.
  • Document the normalization and the non-retrospective behavior for existing food logs.
File summaries
File Description
docs/public/data-safety.md Documents the new “single nutrition basis” rule and clarifies that existing logs are not rescaled.
backend/tests/test_food_nutrition_basis.py Adds regression tests for basis selection and edge cases (missing/invalid values, unusable labels, zero macros).
backend/app/services/open_food_facts.py Implements basis-consistent normalization (serving when complete + usable label, else explicit 100 g/ml reference).
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@xrpbanks
xrpbanks merged commit ddb75bd into main Sep 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants