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: Pydantic Serialization Issues #3157

Conversation

michael-genson
Copy link
Collaborator

@michael-genson michael-genson commented Feb 11, 2024

Well, it couldn't be that easy to upgrade, could it?

What type of PR is this?

(REQUIRED)

  • bug

What this PR does / why we need it:

(REQUIRED)

In certain instances, Pydantic will bypass its __init__ (mostly related to ORM). In a few instances we were overriding Pydantic's __init__ to do some post-init validation. Thankfully, Pydantic V2 supports this out of the box with its model_validator when set to mode="after".

I also noticed when adding recipes to the shopping list we were losing units for some reason. I'm not quite sure what happened, but somewhere we were relying on Pydantic choosing the correct sub-model (IngredientUnit vs CreateIngredientUnit), and now it isn't anymore. So I fixed that by adding the id as an optional field so it always gets serialized when present.

Which issue(s) this PR fixes:

(REQUIRED)

N/A

Special notes for your reviewer:

(fill-in or delete this section)

🙃

Testing

(fill-in or delete this section)

Manually

# if we're missing a label, but the food has a label, use that as the label
if (not self.label) and (self.food and self.food.label):
self.label = self.food.label
self.label_id = self.label.id

return self
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pydantic reassigns the instance for some reason, so yeah, we do actually have to return self here

@michael-genson michael-genson merged commit df75cb4 into mealie-recipes:mealie-next Feb 11, 2024
9 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