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: Allow UserOut to accept list of slugs for recipe favorites #3283

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions mealie/schema/user/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from pathlib import Path
from typing import Annotated
from typing import Annotated, Any
from uuid import UUID

from pydantic import UUID4, ConfigDict, Field, StringConstraints, field_validator
Expand Down Expand Up @@ -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
Expand All @@ -119,8 +119,24 @@ def loader_options(cls) -> list[LoaderOption]:
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: Any):
if not v:
return []
if not isinstance(v, list):
return v

slugs: list[str] = []
for recipe in v:
if isinstance(recipe, str):
slugs.append(recipe)
else:
try:
slugs.append(recipe.slug)
except AttributeError:
# this isn't a list of recipes, so we quit early and let Pydantic's typical validation handle it
return v

return slugs


class UserPagination(PaginationBase):
Expand Down