Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/msgspec_fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -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']
3 changes: 1 addition & 2 deletions tests/test_error_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
70 changes: 69 additions & 1 deletion tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading