From fae91fa2f6f2ab68fc4f3019661378d4edaf0592 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:58:29 -0500 Subject: [PATCH] Fix tests --- src/fastmcp/resources/types.py | 11 ++++++++++- tests/resources/test_resource_template.py | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/fastmcp/resources/types.py b/src/fastmcp/resources/types.py index c5363a6d3..0b13816ec 100644 --- a/src/fastmcp/resources/types.py +++ b/src/fastmcp/resources/types.py @@ -8,7 +8,7 @@ from typing import Any, Callable, Union import httpx import pydantic.json import pydantic_core -from pydantic import Field +from pydantic import Field, ValidationInfo from fastmcp.resources.base import Resource @@ -91,6 +91,15 @@ class FileResource(Resource): raise ValueError("Path must be absolute") return path + @pydantic.field_validator("is_binary") + @classmethod + def set_binary_from_mime_type(cls, is_binary: bool, info: ValidationInfo) -> bool: + """Set is_binary based on mime_type if not explicitly set.""" + if is_binary: + return True + mime_type = info.data.get("mime_type", "text/plain") + return not mime_type.startswith("text/") + async def read(self) -> Union[str, bytes]: """Read the file content.""" try: diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.py index af45e6bb3..9b459d7b1 100644 --- a/tests/resources/test_resource_template.py +++ b/tests/resources/test_resource_template.py @@ -22,7 +22,8 @@ class TestResourceTemplate: assert template.uri_template == "test://{key}/{value}" assert template.name == "test" assert template.mime_type == "text/plain" # default - assert template.fn == my_func + test_input = {"key": "test", "value": 42} + assert template.fn(**test_input) == my_func(**test_input) def test_template_matches(self): """Test matching URIs against a template."""