From 8a035a82d994ac60929cab2a0a924d7fd364162b Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 16 Sep 2026 10:34:45 -0500 Subject: [PATCH] fix: fall back to servings for recipe yield display (#7881) Co-authored-by: Claude Opus 4.8 Co-authored-by: Michael Genson --- mealie/schema/recipe/recipe.py | 6 ++- tests/unit_tests/schema_tests/test_recipe.py | 39 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/mealie/schema/recipe/recipe.py b/mealie/schema/recipe/recipe.py index 4011d71f3..1b4d78065 100644 --- a/mealie/schema/recipe/recipe.py +++ b/mealie/schema/recipe/recipe.py @@ -166,7 +166,11 @@ class RecipeSummary(MealieModel): @property def recipe_yield_display(self) -> str: - return f"{self.recipe_yield_quantity} {self.recipe_yield}".strip() + # Fall back to recipe_servings when no yield is set at all, otherwise + # a servings-only recipe emits a bare "0.0" (or "0.0 None") into schema.org. + quantity = self.recipe_yield_quantity or (self.recipe_servings if not self.recipe_yield else 0) + number = f"{quantity:g}" if quantity else "" + return f"{number} {self.recipe_yield or ''}".strip() @classmethod def loader_options(cls) -> list[LoaderOption]: diff --git a/tests/unit_tests/schema_tests/test_recipe.py b/tests/unit_tests/schema_tests/test_recipe.py index 3e78a2e24..9bf47c907 100644 --- a/tests/unit_tests/schema_tests/test_recipe.py +++ b/tests/unit_tests/schema_tests/test_recipe.py @@ -63,6 +63,45 @@ def test_recipe_string_sanitation(field: str, val: Any, expected: Any): assert getattr(recipe, field) == expected +@pytest.mark.parametrize( + ["recipe_yield_quantity", "recipe_yield", "recipe_servings", "expected"], + [ + # Explicit yield quantity + text + (8, "slices", 0, "8 slices"), + # Whole-number quantities render without a trailing ".0" + (8, "servings", 0, "8 servings"), + # Fractional quantities are preserved + (2.25, "loaves", 0, "2.25 loaves"), + # Yield text only, no quantity + (0, "a dozen cookies", 0, "a dozen cookies"), + # Yield text only, with servings set: servings don't get prefixed onto the text + (0, "a dozen cookies", 4, "a dozen cookies"), + # Servings-only recipe: falls back to recipe_servings instead of "0.0" + (0, None, 4, "4"), + (0, "", 4, "4"), + # Nothing set at all + (0, None, 0, ""), + ], +) +def test_recipe_yield_display( + recipe_yield_quantity: float, + recipe_yield: str | None, + recipe_servings: float, + expected: str, +): + recipe = RecipeSummary( + id=uuid4(), + user_id=uuid4(), + household_id=uuid4(), + group_id=uuid4(), + recipe_yield_quantity=recipe_yield_quantity, + recipe_yield=recipe_yield, + recipe_servings=recipe_servings, + ) + + assert recipe.recipe_yield_display == expected + + def test_recipe_preserves_existing_slug(): recipe = RecipeSummary( id=uuid4(),