Skip to content

fix: Add missing group_id to RecipeTag and TagBase schemas #5342

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

Open
wants to merge 1 commit into
base: mealie-next
Choose a base branch
from

Conversation

robertdanahome
Copy link

@robertdanahome robertdanahome commented Apr 13, 2025

Problem:

When updating recipe tags via the API (PATCH /api/recipes/{recipe_slug} or using bulk actions like /api/recipes/bulk-actions/tag), requests can fail with either a TypeError: __init__() missing 1 required positional argument: 'group_id' or a SQL Integrity Error (violating the unique constraint on tags.slug + tags.group_id).

Root Cause:

There is an inconsistency between the Pydantic schemas used for API validation and the underlying SQLAlchemy database model for Tags:

  • The Tag database model (mealie/db/models/recipe/tag.py) correctly requires group_id during initialization (__init__).
  • However, the Pydantic schemas RecipeTag (mealie/schema/recipe/recipe.py) and TagBase (mealie/schema/recipe/recipe_category.py), which are used to validate the tags array in API request payloads, do not define a group_id field.
  • Consequently, when a payload containing group_id within the tag objects is sent, Pydantic strips this field during validation.
  • The backend processing logic then receives tag data missing the required group_id, leading to errors when attempting to interact with the Tag database model.

Solution:

This PR adds the missing group_id: UUID4 | None = None field to the RecipeTag and CategoryBase (which TagBase inherits from) Pydantic schemas.

  • mealie/schema/recipe/recipe.py (modified RecipeTag)
  • mealie/schema/recipe/recipe_category.py (modified CategoryBase)

This ensures that group_id, if provided in an API request for associating tags, is correctly validated and passed through to the backend database logic, resolving the TypeError and SQL Integrity Error. The field is made optional (| None = None) to minimize potential side effects in other parts of the code that might use these schemas without providing a group ID.

However, this change broke tests. Fixing it required adding the required group_id field to the RecipeToolOut schema. This ensures group_id is present when validating Recipe objects during creation in tests, resolving Pydantic validation errors and downstream IntegrityErrors that occurred during test setup fixtures.

Testing:

Tested locally by applying these schema changes and using an external script to PATCH recipes with tags including id, name, slug, and group_id. The previous TypeError and SQL Integrity Error were resolved, and tags were associated successfully.

Ran the python test suite locally, all tests are now passing.

Fixes #5286

@robertdanahome robertdanahome mentioned this pull request Apr 13, 2025
@robertdanahome
Copy link
Author

Full disclosure: I diagnosed and fixed this using AI. The fix works in my environment, but it's been years since I've slung code and I don't know this project well enough to confirm whether or not there are other things that might need to be changed along with this fix.

@robertdanahome
Copy link
Author

looking into the test failures

@robertdanahome robertdanahome force-pushed the fix/api-tag-groupid-schema branch from 6486e46 to 1f4bd7e Compare April 13, 2025 19:42
@robertdanahome
Copy link
Author

My first try at this broke tests. I made the fix for that, then squashed that fix into the original fix. This is now passing the python tests locally.

@robertdanahome robertdanahome force-pushed the fix/api-tag-groupid-schema branch from 1f4bd7e to b258447 Compare April 14, 2025 16:11
Adds the optional group_id field to RecipeTag and CategoryBase Pydantic
schemas to resolve API validation errors (TypeError/IntegrityError) when
updating recipe tags/categories via PATCH or bulk actions. This ensures
group_id provided in payloads is not stripped during validation.

Also adds the required group_id field to the RecipeToolOut schema.
This ensures group_id is present when validating Recipe objects during
creation in tests, resolving Pydantic validation errors and downstream
IntegrityErrors that occurred during test setup fixtures.
@robertdanahome robertdanahome force-pushed the fix/api-tag-groupid-schema branch from b258447 to 7f76bcd Compare April 14, 2025 16:14
@robertdanahome
Copy link
Author

fixed the formatting issue from the last test run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recipe Tag
1 participant