From c7c19b8ed61ccf6204fb9b315af2977127f623ed Mon Sep 17 00:00:00 2001 From: Tseluiko Aleksandr Date: Mon, 16 Nov 2026 10:20:22 +0300 Subject: [PATCH] Export MsgspecRoute, add classifiers and Dependabot --- .github/dependabot.yml | 6 +++ README.md | 1 + pyproject.toml | 10 +++++ src/msgspec_fastapi/__init__.py | 4 +- tests/test_error_responses.py | 3 +- tests/test_openapi.py | 3 +- tests/test_router.py | 70 ++++++++++++++++++++++++++++++++- 7 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..ca79ca5 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly diff --git a/README.md b/README.md index a342834..e0c3f6b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,7 @@ validators or serializers. FastAPI still depends on Pydantic for its own interna the JSON body on the route endpoint. `Form()` and `File()` parameters are left to FastAPI. - On ordinary non-streaming routes, when both the parameters and body are invalid, only the parameter errors are returned; FastAPI returns both. +- A custom `route_class` or `route_class_override` must extend `MsgspecRoute`. - Structs, alone or inside containers, are not supported as path, query, header or cookie parameters, or as `Form()` and `File()` parameters. - Defaults and copied metadata do not support container subclasses, custom state/reduction diff --git a/pyproject.toml b/pyproject.toml index fa44690..3ecfdf7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,17 @@ authors = [{ name = "Aleksandr Tseluiko" }] classifiers = [ "Development Status :: 2 - Pre-Alpha", "Framework :: FastAPI", + "Intended Audience :: Developers", + "Programming Language :: C", "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: Implementation :: CPython", + "Topic :: Internet :: WWW/HTTP :: HTTP Servers", + "Typing :: Typed", ] dependencies = [ "fastapi>=0.142.2,<0.148", diff --git a/src/msgspec_fastapi/__init__.py b/src/msgspec_fastapi/__init__.py index a8637dc..692c125 100644 --- a/src/msgspec_fastapi/__init__.py +++ b/src/msgspec_fastapi/__init__.py @@ -1,4 +1,4 @@ from .responses import MsgspecJSONResponse -from .router import MsgspecRouter, install_openapi +from .router import MsgspecRoute, MsgspecRouter, install_openapi -__all__ = ['MsgspecJSONResponse', 'MsgspecRouter', 'install_openapi'] +__all__ = ['MsgspecJSONResponse', 'MsgspecRoute', 'MsgspecRouter', 'install_openapi'] diff --git a/tests/test_error_responses.py b/tests/test_error_responses.py index 92fa6b5..fe61ae8 100644 --- a/tests/test_error_responses.py +++ b/tests/test_error_responses.py @@ -12,8 +12,7 @@ from fastapi.testclient import TestClient from pydantic import Field -from msgspec_fastapi import MsgspecRouter -from msgspec_fastapi.router import MsgspecRoute +from msgspec_fastapi import MsgspecRoute, MsgspecRouter class Item(msgspec.Struct): diff --git a/tests/test_openapi.py b/tests/test_openapi.py index 08695b2..1c53cf3 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -9,8 +9,7 @@ from fastapi.testclient import TestClient from pydantic import BaseModel, create_model -from msgspec_fastapi import MsgspecRouter, install_openapi -from msgspec_fastapi.router import MsgspecRoute +from msgspec_fastapi import MsgspecRoute, MsgspecRouter, install_openapi class Choice(str, Enum): diff --git a/tests/test_router.py b/tests/test_router.py index ec30f44..1d745d3 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -7,7 +7,18 @@ import httpx2 import msgspec import pytest -from fastapi import APIRouter, Depends, FastAPI, File, Form, Header, HTTPException, Query, Response +from fastapi import ( + APIRouter, + Body, + Depends, + FastAPI, + File, + Form, + Header, + HTTPException, + Query, + Response, +) from fastapi.exceptions import ResponseValidationError from fastapi.responses import HTMLResponse, PlainTextResponse from fastapi.testclient import TestClient @@ -358,6 +369,63 @@ async def endpoint( # noqa: ANN202 - Test an endpoint without a return annotati assert next(p for p in parameters if p['name'] == 'max')['schema']['minimum'] == 1 +def routes_for_request_shapes(router_class: type[APIRouter], model: Any) -> TestClient: + router = router_class() + + @router.post('/sync') + def sync(city: model) -> dict[str, Any]: + try: + asyncio.get_running_loop() + except RuntimeError: + in_event_loop = False + else: + in_event_loop = True + return {'population': city.population, 'in_event_loop': in_event_loop} + + @router.post('/embedded') + async def embedded(city: Annotated[model, Body(embed=True)]) -> model: + return city + + @router.post('/optional') + async def optional(city: model | None = None) -> dict[str, int | None]: + return {'population': None if city is None else city.population} + + @router.get('/list') + async def listing(q: Annotated[list[int], Query()] = []) -> list[int]: + return q + + app = FastAPI() + app.include_router(router) + return TestClient(app, raise_server_exceptions=False) + + +JSON = {'Content-Type': 'application/json'} + + +@pytest.mark.parametrize( + 'method,path,options', + [ + ('post', '/sync', {'json': {'population': 1}}), + ('post', '/sync', {}), + ('post', '/sync', {'content': b'', 'headers': JSON}), + ('post', '/embedded', {'json': {'city': {'population': 1}}}), + ('post', '/embedded', {'json': {'population': 1}}), + ('post', '/optional', {}), + ('post', '/optional', {'content': b'', 'headers': JSON}), + ('post', '/optional', {'json': {'population': 1}}), + ('get', '/list?q=1&q=2', {}), + ('get', '/list', {}), + ('get', '/list?q=1&q=x', {}), + ], +) +def test_request_shapes_match_fastapi(method: str, path: str, options: dict[str, Any]) -> None: + expected = getattr(routes_for_request_shapes(APIRouter, PydCity), method)(path, **options) + actual = getattr(routes_for_request_shapes(MsgspecRouter, City), method)(path, **options) + assert actual.status_code == expected.status_code + if expected.status_code == 200: + assert actual.json() == expected.json() + + def test_install_openapi_accepts_no_router_arguments() -> None: app = FastAPI() install_openapi(app)