From 05e13e607877086f04d844e20c1261a5fe6eb4d0 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 11 Mar 2024 00:13:57 +0000 Subject: [PATCH] account for slugs or recipes when constructing user favorites --- mealie/schema/user/user.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mealie/schema/user/user.py b/mealie/schema/user/user.py index 270e38fd55f2..f0a456bf713e 100644 --- a/mealie/schema/user/user.py +++ b/mealie/schema/user/user.py @@ -107,7 +107,7 @@ class UserOut(UserBase): group_slug: str tokens: list[LongLiveTokenOut] | None = None cache_key: str - favorite_recipes: Annotated[list[str] | None, Field(validate_default=True)] = [] + favorite_recipes: Annotated[list[str], Field(validate_default=True)] = [] model_config = ConfigDict(from_attributes=True) @property @@ -119,8 +119,18 @@ class UserOut(UserBase): return [joinedload(User.group), joinedload(User.favorite_recipes), joinedload(User.tokens)] @field_validator("favorite_recipes", mode="before") - def convert_favorite_recipes_to_slugs(cls, v): - return [recipe.slug for recipe in v] if v else v + def convert_favorite_recipes_to_slugs(cls, v: list[str | RecipeSummary] | None): + if not v: + return [] + + slugs: list[str] = [] + for recipe in v: + if isinstance(recipe, str): + slugs.append(recipe) + else: + slugs.append(recipe.slug) + + return slugs class UserPagination(PaginationBase):