fix: coerce numeric nutrition values to strings in Cookbook model (fixes #708)
Agent-Logs-Url: https://github.com/dylanlangston/nextcloud-mcp-server/sessions/d7163bb6-ad5d-4406-8d11-145c5317ec19 Co-authored-by: dylanlangston <16236219+dylanlangston@users.noreply.github.com>
This commit is contained in:
co-authored by
dylanlangston
parent
fea84dd646
commit
cb88d2b062
@@ -2,7 +2,7 @@
|
||||
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from .base import BaseResponse, IdResponse, StatusResponse
|
||||
|
||||
@@ -40,6 +40,33 @@ class Nutrition(BaseModel):
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
@field_validator(
|
||||
"calories",
|
||||
"carbohydrateContent",
|
||||
"cholesterolContent",
|
||||
"fatContent",
|
||||
"fiberContent",
|
||||
"proteinContent",
|
||||
"saturatedFatContent",
|
||||
"servingSize",
|
||||
"sodiumContent",
|
||||
"sugarContent",
|
||||
"transFatContent",
|
||||
"unsaturatedFatContent",
|
||||
mode="before",
|
||||
)
|
||||
@classmethod
|
||||
def coerce_to_str(cls, v: object) -> object:
|
||||
"""Coerce numeric values to strings.
|
||||
|
||||
The schema.org/NutritionInformation spec allows values as either
|
||||
strings (e.g. '650 kcal') or numbers (e.g. 650). Nextcloud Cookbook
|
||||
stores whatever the source provided.
|
||||
"""
|
||||
if isinstance(v, (int, float)):
|
||||
return str(v)
|
||||
return v
|
||||
|
||||
|
||||
class RecipeStub(BaseModel):
|
||||
"""Stub of a recipe with basic information."""
|
||||
|
||||
Reference in New Issue
Block a user