From 42ab346b8ef8a1739e3a6c8e0877d72bda91fccd Mon Sep 17 00:00:00 2001 From: Tseluiko Aleksandr Date: Sun, 15 Nov 2026 21:51:23 +0300 Subject: [PATCH 1/2] Improve request and response handling --- .github/workflows/build-wheels.yml | 151 +++ .github/workflows/ci.yml | 32 +- .github/workflows/publish.yml | 28 +- .gitignore | 4 + MANIFEST.in | 4 + README.md | 68 +- pyproject.toml | 22 +- setup.py | 16 + src/msgspec_fastapi/_defaults.c | 1102 ++++++++++++++++++++ src/msgspec_fastapi/_defaults.pyi | 50 + src/msgspec_fastapi/_defaults_copy.c | 809 +++++++++++++++ src/msgspec_fastapi/body_errors.py | 506 +++++++++ src/msgspec_fastapi/defaults.py | 342 +++++++ src/msgspec_fastapi/responses.py | 29 +- src/msgspec_fastapi/router.py | 561 ++++++++-- src/msgspec_fastapi/validation.py | 346 ++++++- tests/test_body_default_ownership.py | 169 +++ tests/test_body_errors.py | 345 +++++++ tests/test_constraint_carriers.py | 87 ++ tests/test_copy_callbacks.py | 72 ++ tests/test_copy_protocol_regressions.py | 300 ++++++ tests/test_copy_state_protocols.py | 358 +++++++ tests/test_copy_type_predicates.py | 138 +++ tests/test_copy_unpack_order.py | 96 ++ tests/test_default_accessors.py | 165 +++ tests/test_default_copy_protocols.py | 124 +++ tests/test_default_instance_protocols.py | 206 ++++ tests/test_default_slot_bindings.py | 274 +++++ tests/test_diagnostic_plans.py | 199 ++++ tests/test_enum_copy_protocols.py | 159 +++ tests/test_error_responses.py | 526 ++++++++++ tests/test_instance_builtin_codec.py | 41 + tests/test_metadata_copy.py | 118 +++ tests/test_native_copier.py | 561 ++++++++++ tests/test_native_guard.py | 224 ++++ tests/test_native_serialization.py | 135 +++ tests/test_native_type_contract.py | 232 +++++ tests/test_parameter_defaults.py | 138 +++ tests/test_parameter_getter_copy.py | 59 ++ tests/test_request_response.py | 1194 ++++++++++++++++++++++ tests/test_router.py | 35 +- tests/test_runtime_codec_boundary.py | 96 ++ tests/test_sse_boundary.py | 147 +++ tests/test_stream_adapter.py | 120 +++ 44 files changed, 10209 insertions(+), 179 deletions(-) create mode 100644 .github/workflows/build-wheels.yml create mode 100644 MANIFEST.in create mode 100644 setup.py create mode 100644 src/msgspec_fastapi/_defaults.c create mode 100644 src/msgspec_fastapi/_defaults.pyi create mode 100644 src/msgspec_fastapi/_defaults_copy.c create mode 100644 src/msgspec_fastapi/body_errors.py create mode 100644 src/msgspec_fastapi/defaults.py create mode 100644 tests/test_body_default_ownership.py create mode 100644 tests/test_body_errors.py create mode 100644 tests/test_constraint_carriers.py create mode 100644 tests/test_copy_callbacks.py create mode 100644 tests/test_copy_protocol_regressions.py create mode 100644 tests/test_copy_state_protocols.py create mode 100644 tests/test_copy_type_predicates.py create mode 100644 tests/test_copy_unpack_order.py create mode 100644 tests/test_default_accessors.py create mode 100644 tests/test_default_copy_protocols.py create mode 100644 tests/test_default_instance_protocols.py create mode 100644 tests/test_default_slot_bindings.py create mode 100644 tests/test_diagnostic_plans.py create mode 100644 tests/test_enum_copy_protocols.py create mode 100644 tests/test_error_responses.py create mode 100644 tests/test_instance_builtin_codec.py create mode 100644 tests/test_metadata_copy.py create mode 100644 tests/test_native_copier.py create mode 100644 tests/test_native_guard.py create mode 100644 tests/test_native_serialization.py create mode 100644 tests/test_native_type_contract.py create mode 100644 tests/test_parameter_defaults.py create mode 100644 tests/test_parameter_getter_copy.py create mode 100644 tests/test_request_response.py create mode 100644 tests/test_runtime_codec_boundary.py create mode 100644 tests/test_sse_boundary.py create mode 100644 tests/test_stream_adapter.py diff --git a/.github/workflows/build-wheels.yml b/.github/workflows/build-wheels.yml new file mode 100644 index 0000000..21b8807 --- /dev/null +++ b/.github/workflows/build-wheels.yml @@ -0,0 +1,151 @@ +name: Build distributions + +on: + workflow_call: + inputs: + check-version-tag: + type: boolean + default: false + +permissions: + contents: read + actions: read + +jobs: + sdist: + runs-on: ubuntu-latest + outputs: + filename: ${{ steps.sdist.outputs.filename }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + with: + enable-cache: false + - name: Check that the tag matches the version + if: inputs.check-version-tag + run: test "${GITHUB_REF_NAME#v}" = "$(uv version --short)" + - name: Build the source distribution + run: uv build --sdist --out-dir dist + - name: Check the source distribution + run: uvx twine@7.0.0 check --strict dist/*.tar.gz + - name: Record the source distribution filename + id: sdist + shell: bash + run: | + files=(dist/*.tar.gz) + test "${#files[@]}" -eq 1 + test -f "${files[0]}" + echo "filename=${files[0]#dist/}" >> "$GITHUB_OUTPUT" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: dist-sdist + path: dist/*.tar.gz + if-no-files-found: error + + wheels: + needs: sdist + name: Wheels (${{ matrix.id }}) + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - id: linux-x86_64 + os: ubuntu-latest + platform: linux + arch: x86_64 + - id: linux-aarch64 + os: ubuntu-24.04-arm + platform: linux + arch: aarch64 + - id: macos-x86_64 + os: macos-15-intel + platform: macos + arch: x86_64 + - id: macos-arm64 + os: macos-latest + platform: macos + arch: arm64 + - id: windows-amd64 + os: windows-latest + platform: windows + arch: AMD64 + - id: windows-arm64 + os: windows-11-arm + platform: windows + arch: ARM64 + env: + CIBW_ARCHS: ${{ matrix.arch }} + CIBW_PLATFORM: ${{ matrix.platform }} + PACKAGE_DIR: dist/${{ needs.sdist.outputs.filename }} + steps: + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + name: dist-sdist + path: dist/ + - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + with: + enable-cache: false + # The CLI avoids the composite action's unpinned nested actions. + - name: Build and test wheels + if: runner.os != 'Windows' + run: uvx --from cibuildwheel==4.2.0 cibuildwheel --output-dir wheelhouse "$PACKAGE_DIR" + - name: Build and test wheels (Windows) + if: runner.os == 'Windows' + run: uvx --from cibuildwheel==4.2.0 cibuildwheel --output-dir wheelhouse "$env:PACKAGE_DIR" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: dist-wheels-${{ matrix.id }} + path: wheelhouse/*.whl + if-no-files-found: error + + verify: + needs: [sdist, wheels] + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + pattern: dist-* + merge-multiple: true + path: dist/ + - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + with: + enable-cache: false + - name: Check the distributions + run: uvx twine@7.0.0 check --strict dist/* + - name: Check native wheel coverage + shell: python + run: | + from pathlib import Path + import zipfile + + assert len(list(Path("dist").glob("*.tar.gz"))) == 1 + wheels = list(Path("dist").glob("*.whl")) + assert wheels, "No wheels were built" + expected = { + ("manylinux", "x86_64"), ("manylinux", "aarch64"), + ("musllinux", "x86_64"), ("musllinux", "aarch64"), + ("macosx", "x86_64"), ("macosx", "arm64"), + ("win", "amd64"), ("win", "arm64"), + } + found = set() + for wheel in wheels: + _, python, abi, platforms = wheel.stem.rsplit("-", 3) + assert (python, abi) == ("cp311", "abi3"), wheel.name + with zipfile.ZipFile(wheel) as archive: + names = archive.namelist() + metadata = [name for name in names if name.endswith(".dist-info/WHEEL")] + assert len(metadata) == 1, wheel.name + assert b"Root-Is-Purelib: false" in archive.read(metadata[0]), wheel.name + assert any( + name.startswith("msgspec_fastapi/_defaults.") + and name.endswith((".so", ".pyd")) + for name in names + ), wheel.name + for platform in platforms.split("."): + for family, arch in expected: + if platform.startswith(family + "_") and platform.endswith("_" + arch): + found.add((family, arch)) + assert found == expected, f"Missing wheel platforms: {expected - found}" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8aa2a5b..847bcd8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,22 @@ jobs: - name: Check formatting run: uv run --no-sync ruff format --check . + typing: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + with: + enable-cache: false + - name: Install typing tools + run: uv sync --python 3.13 --group typing + - name: Check source types + run: uv run --no-sync mypy src/msgspec_fastapi --follow-imports=silent + - name: Check the native interface stubs + run: uv run --no-sync python -m mypy.stubtest msgspec_fastapi._defaults --concise + test: name: Python ${{ matrix.python }}, ${{ matrix.bounds }} dependency bounds runs-on: ubuntu-latest @@ -62,15 +78,7 @@ jobs: run: uv run --no-sync pytest build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 - with: - enable-cache: false - - name: Build - run: uv build - - name: Check the distributions - run: uvx twine@7.0.0 check --strict dist/* + permissions: + contents: read + actions: read + uses: ./.github/workflows/build-wheels.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3b641d1..659f924 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,24 +10,12 @@ permissions: jobs: build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 - with: - persist-credentials: false - - uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0 - with: - enable-cache: false - - name: Check that the tag matches the version - run: test "${GITHUB_REF_NAME#v}" = "$(uv version --short)" - - name: Run tests - run: uv run --python 3.13 --group test pytest - - name: Build - run: uv build - - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: dist - path: dist/ + permissions: + contents: read + actions: read + uses: ./.github/workflows/build-wheels.yml + with: + check-version-tag: true publish: needs: build @@ -37,9 +25,11 @@ jobs: url: https://pypi.org/project/msgspec-fastapi/ permissions: id-token: write + actions: read steps: - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: - name: dist + pattern: dist-* + merge-multiple: true path: dist/ - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2 diff --git a/.gitignore b/.gitignore index 1881a8d..133006d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,8 @@ __pycache__/ .ruff_cache/ .mypy_cache/ dist/ +build/ +*.egg-info/ +*.so +*.pyd uv.lock diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..877d3b3 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include LICENSE README.md pyproject.toml setup.py MANIFEST.in +recursive-include src/msgspec_fastapi *.py *.pyi *.c py.typed +recursive-include docs *.svg +recursive-include tests *.py diff --git a/README.md b/README.md index a865733..a342834 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ not part of msgspec; feedback is welcome in the issues. pip install msgspec-fastapi ``` +The package includes an internal C extension for checking copied defaults and +metadata. Building from source requires a C11 compiler and CPython headers. + ## Usage ```python @@ -120,6 +123,9 @@ Things to keep in mind: ## What it does +The package uses msgspec for its JSON validation and encoding, without calling Pydantic +validators or serializers. FastAPI still depends on Pydantic for its own internals. + - A msgspec body parameter (a Struct or another type msgspec can decode) is decoded from the raw request bytes by a `msgspec.json.Decoder` built once at route registration. A body with a non-JSON content type returns 415, malformed JSON or JSON nested too deeply @@ -138,15 +144,46 @@ Things to keep in mind: instance are not checked. A NaN or infinite value in a `float` field is encoded as `null` unless a constraint rejects it. In a `Decimal` field it is encoded as a string, while FastAPI with Pydantic rejects non-finite `Decimal` values. -- Responses of routes without a return annotation or `response_model`, or with - `-> None`, are also encoded with msgspec, so the output can differ from FastAPI's - `jsonable_encoder` (for example, `bytes` are base64-encoded and `Decimal` is encoded as - a string). +- With the default `MsgspecJSONResponse`, responses of routes without a return + annotation or `response_model`, or with `-> None`, are also encoded with msgspec. + In addition to msgspec's built-in types, `pathlib` paths and standard-library IP + addresses, interfaces and networks are encoded as strings. Other objects, including + `BaseModel` instances, `HttpUrl` and `SecretStr`, are unsupported and raise `TypeError` + during encoding. Values supported directly by msgspec retain its encoding: for example, + `bytes` are base64-encoded and `Decimal` is encoded as a string. - Path, query, header and cookie parameters keep FastAPI extraction, aliases and defaults, but are converted with `msgspec.convert(strict=False)` instead of Pydantic, so coercion differs for some inputs (for example, `" 1.5"` is rejected for `float` and `"nan"` is accepted for `Decimal`). Parameter types must be supported by msgspec; Pydantic-specific types such as `AnyUrl` raise `TypeError` at route registration. +- On the msgspec validation path, string `pattern` constraints from FastAPI parameters, + `Body` and supported nested `Field` annotations use Rust's `regex` engine through + `regexrs-py`, without catastrophic backtracking. Patterns are compiled once at route + registration. Compiled Python `re.Pattern` objects are rejected rather than falling + back to a backtracking engine. + Native `msgspec.Meta(pattern=...)` constraints retain msgspec's Python regex behavior + and are outside this protection. +- Generator endpoints support JSON Lines with the default response class and SSE with + `response_class=EventSourceResponse`. Plain yielded items with declared msgspec + types are validated with the same instance-trust rules as ordinary responses. Untyped + yielded values are also encoded with msgspec. Explicit instances of the exact + `ServerSentEvent` class support preformatted `raw_data` and metadata-only events; + subclasses are unsupported. A non-`None` `data` payload without + `raw_data` is rejected. For a Struct or Raw payload with event metadata, use + `ServerSentEvent(raw_data=msgspec.json.encode(payload).decode('utf-8'), id='...')`. + Explicit `StreamingResponse` keeps raw chunks. Streaming bodies retain dependency + validation and generator cleanup. +- Custom response classes are supported. Typed response validation and field projection + happen before response construction. A `JSONResponse` class that does not inherit + `MsgspecJSONResponse` receives JSON-compatible values converted with msgspec, including + for untyped returns. Returned `Response` instances pass through unchanged. +- Structural JSON body errors include field names, encoded aliases, keys and indices, + with multiple errors for supported Structs, regular dataclasses, TypedDicts and + built-in containers. Diagnostics never repeat model validation hooks or default + factories: types with hooks or unsupported diagnostic shapes retain the original + first error. The default 422 handler encodes error details with msgspec; custom + application exception handlers are preserved. Error details added by dependencies must + also be encodable by this encoder or handled by a custom exception handler. - `install_openapi(app)` adds the msgspec JSON schemas to the OpenAPI document. Call it once before serving. Routes, webhooks and callbacks are discovered from the application when the document is built; the optional router arguments remain accepted for @@ -154,33 +191,44 @@ Things to keep in mind: is cached: set `app.openapi_schema = None` to rebuild it after adding routes. - Body descriptions and examples, `openapi_extra` request metadata and response examples are preserved. JSON body routes document their 400, 415 and 422 responses. -- A Pydantic body or response model is left to FastAPI; the route parameters are still - validated with msgspec. +- JSON body and response types must be supported by msgspec. Pydantic models and + custom Pydantic types are rejected at route registration. Pydantic data objects are + also rejected before implicit copies of defaults and route metadata. ## Limitations - Only one msgspec body parameter per route, and it cannot be combined with other FastAPI body fields. Only JSON bodies are supported: `application/json`, `application/*+json`, or a request without a content type when `strict_content_type` - is `False`. `Form()` and `File()` parameters are left to FastAPI. -- When both the parameters and the body are invalid, only the parameter errors are - returned; FastAPI returns both. + is `False`. JSON body parameters in `Depends` functions are not supported; declare + 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. - 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 + methods, registered copy reducers, dynamic attribute access or custom copy descriptors. + Ordinary application-defined Python `__deepcopy__` methods and default factories remain + application code. - `response_model_include`, `response_model_exclude`, `response_model_exclude_*` and `response_model_by_alias=False` are rejected for typed msgspec responses. ## Development ```sh -uv sync --group test --group lint +uv sync --group test --group lint --group typing uv run --group test pytest uv run --group lint ruff check . uv run --group lint ruff format --check . +uv run --group typing mypy src/msgspec_fastapi --follow-imports=silent +uv run --group typing python -m mypy.stubtest msgspec_fastapi._defaults uv run --group lint pre-commit install uv run --group lint pre-commit run --all-files ``` +After changing C sources, rebuild the extension with +`uv sync --reinstall-package msgspec-fastapi --group test --group lint --group typing`. + Ruff checks imports, annotations and common Python errors, and formats the code. CI runs the same Ruff version as the pre-commit hooks. Test endpoints that exercise the absence of a response annotation intentionally omit it. diff --git a/pyproject.toml b/pyproject.toml index 0f712bd..666fb39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["hatchling>=1.27"] -build-backend = "hatchling.build" +requires = ["setuptools>=80"] +build-backend = "setuptools.build_meta" [project] name = "msgspec-fastapi" @@ -19,6 +19,7 @@ classifiers = [ dependencies = [ "fastapi>=0.142.2,<0.148", "msgspec>=0.22.0", + "regexrs-py>=0.2.0,<0.3", ] [project.urls] @@ -28,9 +29,22 @@ Issues = "https://github.com/Siyet/msgspec-fastapi/issues" [dependency-groups] test = ["pytest", "httpx", "httpx2", "python-multipart"] lint = ["ruff==0.17.0", "pre-commit>=4.5,<5"] +typing = ["mypy==2.4.1"] -[tool.hatch.build.targets.wheel] -packages = ["src/msgspec_fastapi"] +[tool.setuptools] +include-package-data = false + +[tool.setuptools.packages.find] +where = ["src"] + +[tool.setuptools.package-data] +msgspec_fastapi = ["*.pyi", "py.typed"] + +[tool.cibuildwheel] +build = "cp3{11,12,13,14}-*" +skip = "*-manylinux_i686 *-musllinux_i686 *-win32" +test-requires = ["pytest", "httpx", "httpx2", "python-multipart"] +test-command = "pytest {project}/tests" [tool.pytest.ini_options] testpaths = ["tests"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0a87a00 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +import sys + +from setuptools import Extension, setup + +setup( + ext_modules=[ + Extension( + 'msgspec_fastapi._defaults', + ['src/msgspec_fastapi/_defaults.c', 'src/msgspec_fastapi/_defaults_copy.c'], + define_macros=[('Py_LIMITED_API', '0x030b0000')], + py_limited_api=True, + extra_compile_args=['/std:c11'] if sys.platform == 'win32' else [], + ) + ], + options={'bdist_wheel': {'py_limited_api': 'cp311'}}, +) diff --git a/src/msgspec_fastapi/_defaults.c b/src/msgspec_fastapi/_defaults.c new file mode 100644 index 0000000..c8b5700 --- /dev/null +++ b/src/msgspec_fastapi/_defaults.c @@ -0,0 +1,1102 @@ +#ifndef Py_LIMITED_API +#define Py_LIMITED_API 0x030b0000 +#endif +#include +#include + +#define GUARD_POLICY_FIELDS(X) \ + X(fallback) \ + X(registry) \ + X(trusted_union_reducer) \ + X(union_type) \ + X(function_type) \ + X(builtin_function_type) \ + X(getset_descriptor_type) \ + X(member_descriptor_type) \ + X(method_descriptor_type) \ + X(wrapper_descriptor_type) \ + X(struct_type) \ + X(enum_type) \ + X(atomic_ids) \ + X(reference_ids) \ + X(model_brands) \ + X(native_state_methods) \ + X(state_names) \ + X(enum_namespace) \ + X(missing) \ + X(mro_descriptor) \ + X(dict_descriptor) \ + X(name_descriptor) + +#define GUARD_NAME_FIELDS(X) \ + X(copy_name) \ + X(name_name) \ + X(deepcopy_name) \ + X(dict_name) \ + X(dataclass_fields_name) \ + X(kind_atomic) \ + X(kind_state) \ + X(slots_name) \ + X(slotnames_name) + +typedef struct { + PyObject_HEAD +#define GUARD_REF(name) PyObject *name; + GUARD_POLICY_FIELDS(GUARD_REF) + GUARD_NAME_FIELDS(GUARD_REF) +#undef GUARD_REF + PyObject *object_namespace; + descrgetfunc get_mro; + descrgetfunc get_dict; + descrgetfunc get_name; + int initialized; +} Guard; + +static int guard_traverse(Guard *self, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE((PyObject *)self)); + Py_VISIT(self->object_namespace); +#define GUARD_VISIT(name) Py_VISIT(self->name); + GUARD_POLICY_FIELDS(GUARD_VISIT) + GUARD_NAME_FIELDS(GUARD_VISIT) +#undef GUARD_VISIT + return 0; +} + +static int guard_clear(Guard *self) { + Py_CLEAR(self->object_namespace); +#define GUARD_CLEAR(name) Py_CLEAR(self->name); + GUARD_POLICY_FIELDS(GUARD_CLEAR) + GUARD_NAME_FIELDS(GUARD_CLEAR) +#undef GUARD_CLEAR + self->get_mro = NULL; + self->get_dict = NULL; + self->get_name = NULL; + return 0; +} + +static void guard_dealloc(Guard *self) { + PyTypeObject *type = Py_TYPE((PyObject *)self); + PyObject_GC_UnTrack(self); + guard_clear(self); + PyObject_GC_Del(self); + Py_DECREF((PyObject *)type); +} + +static int guard_ready(Guard *self) { + if (!self->initialized || self->fallback == NULL) { + PyErr_SetString(PyExc_TypeError, "Uninitialized _Guard"); + return 0; + } + return 1; +} + +static int string_tuple(PyObject *value) { + if (!PyTuple_CheckExact(value)) + return 0; + Py_ssize_t size = PyTuple_Size(value); + for (Py_ssize_t i = 0; i < size; i++) { + if (!PyUnicode_CheckExact(PyTuple_GetItem(value, i))) + return 0; + } + return 1; +} + +static int id_collection(PyObject *value) { + if (!PySet_CheckExact(value) && !PyFrozenSet_CheckExact(value)) + return 0; + PyObject *iter = PyObject_GetIter(value); + if (iter == NULL) + return -1; + PyObject *item; + while ((item = PyIter_Next(iter)) != NULL) { + int valid = PyLong_CheckExact(item); + Py_DECREF(item); + if (!valid) { + Py_DECREF(iter); + return 0; + } + } + Py_DECREF(iter); + return PyErr_Occurred() ? -1 : 1; +} + +static int guard_init(Guard *self, PyObject *args, PyObject *kwargs) { + if (self->initialized) { + PyErr_SetString(PyExc_TypeError, "_Guard cannot be reinitialized"); + return -1; + } +#define GUARD_PARAM(name) PyObject *name; + GUARD_POLICY_FIELDS(GUARD_PARAM) +#undef GUARD_PARAM +#define GUARD_KEY(name) #name, + static char *keywords[] = {GUARD_POLICY_FIELDS(GUARD_KEY) NULL}; +#undef GUARD_KEY + if (!PyArg_ParseTupleAndKeywords( + args, kwargs, "OOOOOOOOOOOOOOOOOOOOOO:_Guard", keywords, &fallback, ®istry, + &trusted_union_reducer, &union_type, &function_type, &builtin_function_type, + &getset_descriptor_type, &member_descriptor_type, &method_descriptor_type, + &wrapper_descriptor_type, &struct_type, &enum_type, &atomic_ids, &reference_ids, + &model_brands, &native_state_methods, &state_names, &enum_namespace, &missing, + &mro_descriptor, &dict_descriptor, &name_descriptor)) + return -1; + if (!PyCallable_Check(fallback)) { + PyErr_SetString(PyExc_TypeError, "fallback must be callable"); + return -1; + } + if (!PyDict_CheckExact(registry)) { + PyErr_SetString(PyExc_TypeError, "registry must be an exact dict"); + return -1; + } + if (!PyType_Check(union_type)) { + PyErr_SetString(PyExc_TypeError, "union_type must be a type"); + return -1; + } + if (!PyType_Check(function_type)) { + PyErr_SetString(PyExc_TypeError, "function_type must be a type"); + return -1; + } + if (!PyType_Check(builtin_function_type)) { + PyErr_SetString(PyExc_TypeError, "builtin_function_type must be a type"); + return -1; + } + if (!PyType_Check(getset_descriptor_type)) { + PyErr_SetString(PyExc_TypeError, "getset_descriptor_type must be a type"); + return -1; + } + if (!PyType_Check(member_descriptor_type)) { + PyErr_SetString(PyExc_TypeError, "member_descriptor_type must be a type"); + return -1; + } + if (!PyType_Check(method_descriptor_type)) { + PyErr_SetString(PyExc_TypeError, "method_descriptor_type must be a type"); + return -1; + } + if (!PyType_Check(wrapper_descriptor_type)) { + PyErr_SetString(PyExc_TypeError, "wrapper_descriptor_type must be a type"); + return -1; + } + if (!PyType_Check(struct_type)) { + PyErr_SetString(PyExc_TypeError, "struct_type must be a type"); + return -1; + } + if (!PyType_Check(enum_type)) { + PyErr_SetString(PyExc_TypeError, "enum_type must be a type"); + return -1; + } + int atomic_valid = id_collection(atomic_ids); + if (atomic_valid < 0) + return -1; + int reference_valid = id_collection(reference_ids); + if (reference_valid < 0) + return -1; + if (!atomic_valid || !reference_valid) { + PyErr_SetString(PyExc_TypeError, "Policy ID collections must contain exact integer IDs"); + return -1; + } + if (!string_tuple(model_brands) || !string_tuple(state_names)) { + PyErr_SetString(PyExc_TypeError, "Policy names must be tuples of exact strings"); + return -1; + } + if (!PyTuple_CheckExact(native_state_methods)) { + PyErr_SetString(PyExc_TypeError, "Invalid native state policy"); + return -1; + } + for (Py_ssize_t i = 0; i < PyTuple_Size(native_state_methods); i++) { + PyObject *pair = PyTuple_GetItem(native_state_methods, i); + if (!PyTuple_CheckExact(pair) || PyTuple_Size(pair) != 2 || + !PyUnicode_CheckExact(PyTuple_GetItem(pair, 0)) || + !PyTuple_CheckExact(PyTuple_GetItem(pair, 1))) { + PyErr_SetString(PyExc_TypeError, "Invalid native state policy"); + return -1; + } + } + if ((PyObject *)Py_TYPE(mro_descriptor) != getset_descriptor_type && + (PyObject *)Py_TYPE(mro_descriptor) != member_descriptor_type) { + PyErr_SetString(PyExc_TypeError, "mro_descriptor must be a native type descriptor"); + return -1; + } + if ((PyObject *)Py_TYPE(dict_descriptor) != getset_descriptor_type || + (PyObject *)Py_TYPE(enum_namespace) != getset_descriptor_type) { + PyErr_SetString(PyExc_TypeError, + "Policy namespace descriptors must be native getset descriptors"); + return -1; + } + if ((PyObject *)Py_TYPE(name_descriptor) != getset_descriptor_type && + (PyObject *)Py_TYPE(name_descriptor) != member_descriptor_type) { + PyErr_SetString(PyExc_TypeError, "name_descriptor must be a native type descriptor"); + return -1; + } + descrgetfunc get_name = (descrgetfunc)PyType_GetSlot(Py_TYPE(name_descriptor), Py_tp_descr_get); + descrgetfunc get_mro = (descrgetfunc)PyType_GetSlot(Py_TYPE(mro_descriptor), Py_tp_descr_get); + descrgetfunc get_dict = (descrgetfunc)PyType_GetSlot(Py_TYPE(dict_descriptor), Py_tp_descr_get); + if (!get_mro || !get_dict || !get_name) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_TypeError, "Invalid native type descriptors"); + return -1; + } +#define GUARD_STORE(name) self->name = Py_NewRef(name); + GUARD_POLICY_FIELDS(GUARD_STORE) +#undef GUARD_STORE + self->copy_name = PyUnicode_InternFromString("copy"); + if (!self->copy_name) { + guard_clear(self); + return -1; + } + self->name_name = PyUnicode_InternFromString("__name__"); + if (!self->name_name) { + guard_clear(self); + return -1; + } + self->deepcopy_name = PyUnicode_InternFromString("__deepcopy__"); + if (!self->deepcopy_name) { + guard_clear(self); + return -1; + } + self->dict_name = PyUnicode_InternFromString("__dict__"); + if (!self->dict_name) { + guard_clear(self); + return -1; + } + self->dataclass_fields_name = PyUnicode_InternFromString("__dataclass_fields__"); + if (!self->dataclass_fields_name) { + guard_clear(self); + return -1; + } + self->kind_atomic = PyUnicode_InternFromString("atomic"); + if (!self->kind_atomic) { + guard_clear(self); + return -1; + } + self->kind_state = PyUnicode_InternFromString("state"); + if (!self->kind_state) { + guard_clear(self); + return -1; + } + self->slots_name = PyUnicode_InternFromString("__slots__"); + if (!self->slots_name) { + guard_clear(self); + return -1; + } + self->slotnames_name = PyUnicode_InternFromString("__slotnames__"); + if (!self->slotnames_name) { + guard_clear(self); + return -1; + } + self->get_mro = get_mro; + self->get_dict = get_dict; + self->get_name = get_name; + self->initialized = 1; + return 0; +} + +static int class_id_contains(PyObject *ids, PyTypeObject *cls) { + PyObject *key = PyLong_FromVoidPtr(cls); + if (key == NULL) + return -1; + int result = PySet_Contains(ids, key); + Py_DECREF(key); + return result; +} + +static PyObject *class_info_impl(Guard *self, PyObject *cls); + +typedef struct { + PyObject **slots; + Py_ssize_t cap, count; +} Identity; +static size_t ihash(PyObject *x) { + return ((uintptr_t)x >> 4) * (size_t)11400714819323198485ull; +} +static int seen_add(Identity *s, PyObject *x) { + if (!s->cap || s->count >= s->cap - s->cap / 3) { + if (s->cap > PY_SSIZE_T_MAX / 2) { + PyErr_NoMemory(); + return -1; + } + Py_ssize_t cap = s->cap ? s->cap * 2 : 32; + if ((size_t)cap > SIZE_MAX / sizeof(PyObject *)) { + PyErr_NoMemory(); + return -1; + } + PyObject **out = PyMem_Calloc((size_t)cap, sizeof(PyObject *)); + if (!out) { + PyErr_NoMemory(); + return -1; + } + for (Py_ssize_t i = 0; i < s->cap; i++) + if (s->slots[i]) { + size_t k = ihash(s->slots[i]) & (cap - 1); + while (out[k]) + k = (k + 1) & (cap - 1); + out[k] = s->slots[i]; + } + PyMem_Free(s->slots); + s->slots = out; + s->cap = cap; + } + size_t k = ihash(x) & (s->cap - 1); + while (s->slots[k]) { + if (s->slots[k] == x) + return 0; + k = (k + 1) & (s->cap - 1); + } + Py_INCREF(x); + s->slots[k] = x; + s->count++; + return 1; +} +static void seen_clear(Identity *s) { + for (Py_ssize_t i = 0; i < s->cap; i++) + Py_XDECREF(s->slots[i]); + PyMem_Free(s->slots); +} +typedef struct { + PyTypeObject *cls; + PyObject *info; + descrgetfunc nsget, slotget; + int reference; +} Entry; +typedef struct { + Entry *items; + Py_ssize_t size, cap; +} Cache; +static Entry *cache_get(Cache *cache, PyTypeObject *cls, Guard *self) { + for (Py_ssize_t i = 0; i < cache->size; i++) + if (cache->items[i].cls == cls) + return &cache->items[i]; + int reference = class_id_contains(self->reference_ids, cls); + if (reference < 0) + return NULL; + int atomic = class_id_contains(self->atomic_ids, cls); + if (atomic < 0) + return NULL; + PyObject *info = + (reference || atomic) ? Py_NewRef(Py_None) : class_info_impl(self, (PyObject *)cls); + if (!info) + return NULL; + descrgetfunc nsget = NULL, slotget = NULL; + if (info != Py_None) { + if (!PyTuple_CheckExact(info) || PyTuple_Size(info) != 3 || + !PyTuple_CheckExact(PyTuple_GetItem(info, 2))) { + Py_DECREF(info); + PyErr_SetString(PyExc_TypeError, "Invalid class policy result"); + return NULL; + } + PyObject *ns = PyTuple_GetItem(info, 1), *slots = PyTuple_GetItem(info, 2); + if (ns != Py_None) + nsget = (descrgetfunc)PyType_GetSlot(Py_TYPE(ns), Py_tp_descr_get); + if (PyTuple_Size(slots)) { + PyTypeObject *first = Py_TYPE(PyTuple_GetItem(slots, 0)); + slotget = (descrgetfunc)PyType_GetSlot(first, Py_tp_descr_get); + for (Py_ssize_t i = 1; i < PyTuple_Size(slots); i++) + if (Py_TYPE(PyTuple_GetItem(slots, i)) != first) { + Py_DECREF(info); + PyErr_SetString(PyExc_TypeError, "Slot descriptors must have one native type"); + return NULL; + } + } + if ((ns != Py_None && !nsget) || (PyTuple_Size(slots) && !slotget)) { + Py_DECREF(info); + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_TypeError, "Invalid state descriptor"); + return NULL; + } + } + if (cache->size == cache->cap) { + if (cache->cap > PY_SSIZE_T_MAX / 2) { + Py_DECREF(info); + PyErr_NoMemory(); + return NULL; + } + Py_ssize_t cap = cache->cap ? cache->cap * 2 : 4; + if ((size_t)cap > SIZE_MAX / sizeof(Entry)) { + Py_DECREF(info); + PyErr_NoMemory(); + return NULL; + } + Entry *out = PyMem_Realloc(cache->items, (size_t)cap * sizeof(Entry)); + if (!out) { + Py_DECREF(info); + PyErr_NoMemory(); + return NULL; + } + cache->items = out; + cache->cap = cap; + } + Py_INCREF((PyObject *)cls); + Entry *entry = &cache->items[cache->size++]; + entry->cls = cls; + entry->info = info; + entry->nsget = nsget; + entry->slotget = slotget; + entry->reference = reference; + return entry; +} +static void cache_clear(Cache *cache) { + for (Py_ssize_t i = 0; i < cache->size; i++) { + Py_DECREF(cache->items[i].cls); + Py_DECREF(cache->items[i].info); + } + PyMem_Free(cache->items); +} +static int atom(PyObject *x) { + return x == Py_None || PyBool_Check(x) || PyLong_CheckExact(x) || PyFloat_CheckExact(x) || + PyUnicode_CheckExact(x) || PyBytes_CheckExact(x); +} +static int push(PyObject *stack, PyObject *x) { + return atom(x) ? 0 : PyList_Append(stack, x); +} +static int check_slot_binding(Guard *self, PyObject *maps, PyObject *name) { + for (Py_ssize_t i = 0; i < PyTuple_Size(maps); i++) { + PyObject *descriptor = PyDict_GetItemWithError(PyTuple_GetItem(maps, i), name); + if (descriptor != NULL) { + if ((PyObject *)Py_TYPE(descriptor) == self->member_descriptor_type) + return 0; + break; + } + if (PyErr_Occurred()) + return -1; + } + PyErr_SetString(PyExc_TypeError, "Non-native slot bindings are not supported in copied data"); + return -1; +} + +static int declared_slot(Guard *self, PyObject *base, PyObject *maps, PyObject *name) { + if (!PyUnicode_CheckExact(name)) { + PyErr_SetString(PyExc_TypeError, "Slot names must be exact strings in copied data"); + return -1; + } + if (PyUnicode_CompareWithASCIIString(name, "__dict__") == 0 || + PyUnicode_CompareWithASCIIString(name, "__weakref__") == 0) + return 0; + Py_ssize_t length = PyUnicode_GetLength(name); + if (length < 0) + return -1; + int private_name = length >= 2 && PyUnicode_ReadChar(name, 0) == '_' && + PyUnicode_ReadChar(name, 1) == '_' && + !(PyUnicode_ReadChar(name, length - 2) == '_' && + PyUnicode_ReadChar(name, length - 1) == '_'); + if (PyErr_Occurred()) + return -1; + if (!private_name) + return check_slot_binding(self, maps, name); + PyObject *class_name = self->get_name(self->name_descriptor, base, (PyObject *)Py_TYPE(base)); + if (class_name == NULL) + return -1; + if (!PyUnicode_Check(class_name)) { + Py_DECREF(class_name); + PyErr_SetString(PyExc_TypeError, "Invalid native class name descriptor"); + return -1; + } + Py_ssize_t class_length = PyUnicode_GetLength(class_name), start = 0; + if (class_length < 0) { + Py_DECREF(class_name); + return -1; + } + while (start < class_length && PyUnicode_ReadChar(class_name, start) == '_') + start++; + if (PyErr_Occurred()) { + Py_DECREF(class_name); + return -1; + } + if (start == class_length) { + Py_DECREF(class_name); + return check_slot_binding(self, maps, name); + } + PyObject *stripped = PyUnicode_Substring(class_name, start, class_length); + Py_DECREF(class_name); + if (stripped == NULL) + return -1; + PyObject *mangled = PyUnicode_FromFormat("_%U%U", stripped, name); + Py_DECREF(stripped); + if (mangled == NULL) + return -1; + int status = check_slot_binding(self, maps, mangled); + Py_DECREF(mangled); + return status; +} + +static int check_slot_bindings(Guard *self, PyObject *mro, PyObject *maps) { + for (Py_ssize_t i = 0; i < PyTuple_Size(maps); i++) { + PyObject *base = PyTuple_GetItem(mro, i), *mapping = PyTuple_GetItem(maps, i); + PyObject *declared = PyDict_GetItemWithError(mapping, self->slots_name); + if (declared == NULL && PyErr_Occurred()) + return -1; + if (declared != NULL) { + if (PyUnicode_CheckExact(declared)) { + if (declared_slot(self, base, maps, declared) < 0) + return -1; + } else { + if (!PyTuple_CheckExact(declared) && !PyList_CheckExact(declared) && + !PySet_CheckExact(declared) && !PyFrozenSet_CheckExact(declared) && + !PyDict_CheckExact(declared)) { + PyErr_SetString(PyExc_TypeError, + "Unsupported slot declarations in copied data"); + return -1; + } + PyObject *iter = PyObject_GetIter(declared); + if (iter == NULL) + return -1; + PyObject *name; + while ((name = PyIter_Next(iter)) != NULL) { + int status = declared_slot(self, base, maps, name); + Py_DECREF(name); + if (status < 0) { + Py_DECREF(iter); + return -1; + } + } + Py_DECREF(iter); + if (PyErr_Occurred()) + return -1; + } + } + PyObject *cached = PyDict_GetItemWithError(mapping, self->slotnames_name); + if (cached == NULL && PyErr_Occurred()) + return -1; + if (cached == NULL || cached == Py_None) + continue; + if (!PyList_CheckExact(cached)) { + PyErr_SetString(PyExc_TypeError, "Unsupported cached slot names in copied data"); + return -1; + } + for (Py_ssize_t j = 0; j < PyList_Size(cached); j++) { + PyObject *name = PyList_GetItem(cached, j); + if (!PyUnicode_CheckExact(name)) { + PyErr_SetString(PyExc_TypeError, "Slot names must be exact strings in copied data"); + return -1; + } + if (check_slot_binding(self, maps, name) < 0) + return -1; + } + } + return 0; +} + +static PyObject *class_info_impl(Guard *self, PyObject *cls) { + PyObject *mro = NULL, *maps = NULL, *slots = NULL, *result = NULL; + if (!PyType_Check(cls)) { + PyErr_SetString(PyExc_TypeError, "class_info() requires a type"); + return NULL; + } + PyObject *brands = self->model_brands; + PyObject *states = self->native_state_methods; + PyObject *atomic = self->atomic_ids; + PyObject *Struct = self->struct_type; + PyObject *Enum = self->enum_type; + PyObject *getsettype = self->getset_descriptor_type; + PyObject *membertype = self->member_descriptor_type; + PyObject *missing = self->missing; + mro = self->get_mro(self->mro_descriptor, cls, (PyObject *)Py_TYPE(cls)); + if (!mro) + goto done; + if (!PyTuple_CheckExact(mro)) { + PyErr_SetString(PyExc_TypeError, "Invalid class MRO descriptor"); + goto done; + } + maps = PyTuple_New(PyTuple_Size(mro)); + if (!maps) + goto done; + for (Py_ssize_t i = 0; i < PyTuple_Size(mro); i++) { + PyObject *base = PyTuple_GetItem(mro, i), *mapping; + if (base == (PyObject *)&PyBaseObject_Type && self->object_namespace != NULL) { + mapping = Py_NewRef(self->object_namespace); + } else { + PyObject *proxy = self->get_dict(self->dict_descriptor, base, (PyObject *)Py_TYPE(base)); + if (proxy == NULL) + goto done; + mapping = PyObject_CallMethodObjArgs(proxy, self->copy_name, NULL); + Py_DECREF(proxy); + if (mapping == NULL) + goto done; + if (base == (PyObject *)&PyBaseObject_Type && PyDict_CheckExact(mapping) && + self->object_namespace == NULL) + self->object_namespace = Py_NewRef(mapping); + } + if (!PyDict_CheckExact(mapping)) { + Py_DECREF(mapping); + PyErr_SetString(PyExc_TypeError, "Invalid class namespace descriptor"); + goto done; + } + if (PyTuple_SetItem(maps, i, mapping) < 0) + goto done; + for (Py_ssize_t j = 0; j < PyTuple_Size(brands); j++) { + int has = PyDict_Contains(mapping, PyTuple_GetItem(brands, j)); + if (has) { + if (has > 0) + PyErr_SetString(PyExc_TypeError, + "Pydantic model behavior is not supported for msgspec data"); + goto done; + } + } + } + int isenum = PyType_IsSubtype((PyTypeObject *)cls, (PyTypeObject *)Enum), isatomic = 0; + if (isenum < 0) + goto done; + if (!isenum) { + PyObject *bases[5] = {(PyObject *)&PyDict_Type, (PyObject *)&PyList_Type, + (PyObject *)&PyTuple_Type, (PyObject *)&PySet_Type, + (PyObject *)&PyFrozenSet_Type}; + for (int i = 0; i < 5; i++) { + int issub = PyType_IsSubtype((PyTypeObject *)cls, (PyTypeObject *)bases[i]); + if (issub) { + if (issub > 0) + PyErr_SetString(PyExc_TypeError, + "Container subclasses are not supported in copied data"); + goto done; + } + } + PyObject *key = PyLong_FromVoidPtr(cls); + if (!key) + goto done; + isatomic = PySet_Contains(atomic, key); + Py_DECREF(key); + if (isatomic < 0) + goto done; + if (!isatomic) { + int supported = PyType_IsSubtype((PyTypeObject *)cls, (PyTypeObject *)Struct); + if (supported < 0) + goto done; + if (!supported) { + for (Py_ssize_t i = 0; i < PyTuple_Size(maps); i++) { + PyObject *fields = PyDict_GetItemWithError(PyTuple_GetItem(maps, i), + self->dataclass_fields_name); + if (fields != NULL) { + supported = 1; + break; + } + if (PyErr_Occurred()) + goto done; + } + } + if (!supported) { + PyErr_SetString(PyExc_TypeError, "Unsupported object in copied data"); + goto done; + } + } + } + if (isenum || !isatomic) { + for (Py_ssize_t i = 0; i < PyTuple_Size(states); i++) { + PyObject *pair = PyTuple_GetItem(states, i), *name = PyTuple_GetItem(pair, 0), + *native = PyTuple_GetItem(pair, 1), *current = missing; + for (Py_ssize_t j = 0; j < PyTuple_Size(maps); j++) { + PyObject *v = PyDict_GetItemWithError(PyTuple_GetItem(maps, j), name); + if (v && v != missing) { + current = v; + break; + } + if (PyErr_Occurred()) + goto done; + } + int allowed = 0; + for (Py_ssize_t j = 0; j < PyTuple_Size(native); j++) + if (current == PyTuple_GetItem(native, j)) { + allowed = 1; + break; + } + if (!allowed) { + PyErr_SetString( + PyExc_TypeError, + "Custom state or reduction methods are not supported in copied data"); + goto done; + } + } + PyObject *method = missing; + for (Py_ssize_t j = 0; j < PyTuple_Size(maps); j++) { + PyObject *v = PyDict_GetItemWithError(PyTuple_GetItem(maps, j), self->deepcopy_name); + if (v && v != missing) { + method = v; + break; + } + if (PyErr_Occurred()) + goto done; + } + if (method != missing) { + PyObject *copy_types[3] = {self->function_type, self->method_descriptor_type, + self->wrapper_descriptor_type}; + int allowed = 0; + for (Py_ssize_t j = 0; j < 3; j++) + if ((PyObject *)Py_TYPE(method) == copy_types[j]) { + allowed = 1; + break; + } + if (!allowed) { + PyErr_SetString(PyExc_TypeError, + "Custom copy descriptors are not supported in copied data"); + goto done; + } + } + } + PyObject *namespace = isenum ? self->enum_namespace : Py_None; + if (!isenum) { + if (check_slot_bindings(self, mro, maps) < 0) + goto done; + for (Py_ssize_t i = 0; i < PyTuple_Size(maps); i++) { + PyObject *mapping = PyTuple_GetItem(maps, i), + *descriptor = PyDict_GetItemWithError(mapping, self->dict_name); + if (descriptor == NULL && PyErr_Occurred()) + goto done; + if (descriptor && descriptor != Py_None && namespace == Py_None) { + if ((PyObject *)Py_TYPE(descriptor) != getsettype) { + PyErr_SetString(PyExc_TypeError, "Unsupported instance state in copied data"); + goto done; + } + namespace = descriptor; + } + Py_ssize_t pos = 0; + PyObject *k, *v; + while (PyDict_Next(mapping, &pos, &k, &v)) { + if ((PyObject *)Py_TYPE(v) != membertype) + continue; + if (slots == NULL) { + slots = PyList_New(0); + if (slots == NULL) + goto done; + } + if (PyList_Append(slots, v) < 0) + goto done; + } + } + for (Py_ssize_t i = 0; slots != NULL && i < PyList_Size(slots); i++) { + PyObject *descriptor = PyList_GetItem(slots, i), + *name = PyObject_GetAttr(descriptor, self->name_name), *effective = NULL; + if (!name) + goto done; + for (Py_ssize_t j = 0; j < PyTuple_Size(maps); j++) { + effective = PyDict_GetItemWithError(PyTuple_GetItem(maps, j), name); + if (effective || PyErr_Occurred()) + break; + } + Py_DECREF(name); + if (PyErr_Occurred()) + goto done; + if (effective != descriptor) { + PyErr_SetString(PyExc_TypeError, "Shadowed slots are not supported in copied data"); + goto done; + } + } + } + PyObject *slot_tuple = slots == NULL ? PyTuple_New(0) : PyList_AsTuple(slots); + if (slot_tuple) { + result = + PyTuple_Pack(3, isenum ? self->kind_atomic : self->kind_state, namespace, slot_tuple); + Py_DECREF(slot_tuple); + } +done: + Py_XDECREF(mro); + Py_XDECREF(maps); + Py_XDECREF(slots); + return result; +} +static int registry_snapshot(Guard *self, PyObject **out) { + PyObject *keys = PyList_New(0); + if (keys == NULL) + return -1; + Py_ssize_t position = 0; + PyObject *key, *reducer; + while (PyDict_Next(self->registry, &position, &key, &reducer)) { + if (key == self->union_type && reducer == self->trusted_union_reducer) + continue; + if (PyList_Append(keys, key) < 0) { + Py_DECREF(keys); + return -1; + } + } + *out = keys; + return 0; +} + +static int registered_type(PyObject *keys, PyTypeObject *cls) { + for (Py_ssize_t i = 0; i < PyList_Size(keys); i++) { + if (PyList_GetItem(keys, i) == (PyObject *)cls) + return 1; + } + return 0; +} + +static int reject_reducer(void) { + PyErr_SetString(PyExc_TypeError, "Registered copy reducers are not supported in copied data"); + return -1; +} + +static int validate_namespace(Guard *self, PyObject *state) { + if (!PyDict_CheckExact(state)) { + PyErr_SetString(PyExc_TypeError, "Unsupported instance state in copied data"); + return -1; + } + Py_ssize_t position = 0; + PyObject *key, *value; + while (PyDict_Next(state, &position, &key, &value)) { + if (!PyUnicode_CheckExact(key)) { + PyErr_SetString(PyExc_TypeError, "Unsupported instance state in copied data"); + return -1; + } + } + for (Py_ssize_t i = 0; i < PyTuple_Size(self->state_names); i++) { + int present = PyDict_Contains(state, PyTuple_GetItem(self->state_names, i)); + if (present < 0) + return -1; + if (present) { + PyErr_SetString(PyExc_TypeError, + "Instance state methods are not supported in copied data"); + return -1; + } + } + PyObject *copier = PyDict_GetItemWithError(state, self->deepcopy_name); + if (copier == NULL && PyErr_Occurred()) + return -1; + if (copier != NULL && (PyObject *)Py_TYPE(copier) != self->function_type) { + PyErr_SetString(PyExc_TypeError, "Instance copy methods must be ordinary Python functions"); + return -1; + } + return 0; +} + +static int check_impl(Guard *self, PyObject *root) { + PyObject *stack = PyList_New(0); + PyObject *registered = NULL, *item = NULL; + Identity seen = {0}; + Cache cache = {0}; + int status = -1; + if (stack == NULL || registry_snapshot(self, ®istered) < 0 || push(stack, root) < 0) + goto done; + while (PyList_Size(stack)) { + Py_ssize_t last = PyList_Size(stack) - 1; + item = Py_NewRef(PyList_GetItem(stack, last)); + if (PyList_SetSlice(stack, last, last + 1, NULL) < 0) + goto done; + int fresh = seen_add(&seen, item); + if (fresh < 0) + goto done; + if (!fresh) { + Py_CLEAR(item); + continue; + } + PyTypeObject *cls = Py_TYPE(item); + if (PyList_CheckExact(item) || PyTuple_CheckExact(item)) { + Py_ssize_t size = PyList_CheckExact(item) ? PyList_Size(item) : PyTuple_Size(item); + for (Py_ssize_t i = 0; i < size; i++) { + PyObject *value = + PyList_CheckExact(item) ? PyList_GetItem(item, i) : PyTuple_GetItem(item, i); + if (push(stack, value) < 0) + goto done; + } + } else if (PyDict_CheckExact(item)) { + Py_ssize_t position = 0; + PyObject *key, *value; + while (PyDict_Next(item, &position, &key, &value)) { + if (push(stack, key) < 0 || push(stack, value) < 0) + goto done; + } + } else if (PyType_Check(item) || (PyObject *)cls == self->function_type || + (PyObject *)cls == self->builtin_function_type) { + /* These references are atomic in stdlib deepcopy. */ + } else if (PySet_CheckExact(item) || PyFrozenSet_CheckExact(item)) { + if (registered_type(registered, cls)) { + reject_reducer(); + goto done; + } + PyObject *result = PyObject_CallFunctionObjArgs(self->fallback, item, NULL); + if (result == NULL) + goto done; + Py_DECREF(result); + } else { + Entry *entry = cache_get(&cache, cls, self); + if (entry == NULL) + goto done; + if (entry->info == Py_None) { + if (entry->reference && registered_type(registered, cls)) { + reject_reducer(); + goto done; + } + PyObject *result = PyObject_CallFunctionObjArgs(self->fallback, item, NULL); + if (result == NULL) + goto done; + Py_DECREF(result); + } else { + PyObject *kind = PyTuple_GetItem(entry->info, 0); + PyObject *namespace = PyTuple_GetItem(entry->info, 1); + PyObject *slots = PyTuple_GetItem(entry->info, 2); + int state_kind = kind == self->kind_state; + if (namespace != Py_None) { + PyObject *state = entry->nsget(namespace, item, (PyObject *)cls); + if (state == NULL) + goto done; + if (validate_namespace(self, state) < 0) { + Py_DECREF(state); + goto done; + } + if (state_kind) { + Py_ssize_t position = 0; + PyObject *key, *value; + while (PyDict_Next(state, &position, &key, &value)) { + if (push(stack, value) < 0) { + Py_DECREF(state); + goto done; + } + } + } + Py_DECREF(state); + } + if (registered_type(registered, cls)) { + PyObject *copier = PyObject_GetAttr(item, self->deepcopy_name); + if (copier == NULL) { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) + goto done; + PyErr_Clear(); + reject_reducer(); + goto done; + } + int missing_copier = copier == Py_None; + Py_DECREF(copier); + if (missing_copier) { + reject_reducer(); + goto done; + } + } + if (state_kind) { + for (Py_ssize_t i = 0; i < PyTuple_Size(slots); i++) { + PyObject *value = + entry->slotget(PyTuple_GetItem(slots, i), item, (PyObject *)cls); + if (value == NULL) { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) + goto done; + PyErr_Clear(); + continue; + } + int pushed = push(stack, value); + Py_DECREF(value); + if (pushed < 0) + goto done; + } + } + } + } + Py_CLEAR(item); + } + status = 0; +done: + Py_XDECREF(item); + Py_XDECREF(stack); + Py_XDECREF(registered); + cache_clear(&cache); + seen_clear(&seen); + return status; +} + +static PyObject *guard_check(Guard *self, PyObject *value) { + if (!guard_ready(self)) + return NULL; + if (Py_EnterRecursiveCall(" while checking copied data")) + return NULL; + int status = check_impl(self, value); + Py_LeaveRecursiveCall(); + if (status < 0) + return NULL; + Py_RETURN_NONE; +} + +static PyObject *guard_class_info(Guard *self, PyObject *cls) { + if (!guard_ready(self)) + return NULL; + if (Py_EnterRecursiveCall(" while inspecting a copied type")) + return NULL; + PyObject *result = class_info_impl(self, cls); + Py_LeaveRecursiveCall(); + return result; +} + +static PyMethodDef guard_methods[] = { + {"check", (PyCFunction)guard_check, METH_O, + "check($self, value, /)\n--\n\nCheck supported data before an implicit copy."}, + {"class_info", (PyCFunction)guard_class_info, METH_O, + "class_info($self, cls, /)\n--\n\nInspect static copy policy for a type."}, + {NULL, NULL, 0, NULL}}; + +static const char guard_doc[] = + "_Guard(fallback, registry, trusted_union_reducer, union_type, function_type, " + "builtin_function_type, getset_descriptor_type, member_descriptor_type, " + "method_descriptor_type, wrapper_descriptor_type, struct_type, enum_type, atomic_ids, " + "reference_ids, model_brands, native_state_methods, state_names, enum_namespace, missing, " + "mro_descriptor, dict_descriptor, name_descriptor)\n--\n\n" + "Native traversal and static policy checks for copied data."; + +static PyType_Slot guard_slots[] = { + {Py_tp_doc, (void *)guard_doc}, {Py_tp_new, PyType_GenericNew}, + {Py_tp_init, guard_init}, {Py_tp_dealloc, guard_dealloc}, + {Py_tp_traverse, guard_traverse}, {Py_tp_clear, guard_clear}, + {Py_tp_methods, guard_methods}, {0, NULL}}; + +static PyType_Spec guard_spec = {"msgspec_fastapi._defaults._Guard", sizeof(Guard), 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE, + guard_slots}; + +typedef struct { + PyObject *guard_type; + PyObject *copier_type; +} ModuleState; + +PyObject *msgspec_fastapi_copier_type(PyObject *module); + +static int module_traverse(PyObject *module, visitproc visit, void *arg) { + ModuleState *state = PyModule_GetState(module); + if (state != NULL) { + Py_VISIT(state->guard_type); + Py_VISIT(state->copier_type); + } + return 0; +} + +static int module_clear(PyObject *module) { + ModuleState *state = PyModule_GetState(module); + if (state != NULL) { + Py_CLEAR(state->guard_type); + Py_CLEAR(state->copier_type); + } + return 0; +} + +static void module_free(void *module) { + module_clear((PyObject *)module); +} + +static int module_exec(PyObject *module) { + ModuleState *state = PyModule_GetState(module); + if (state == NULL) + return -1; + if (state->guard_type != NULL) + return 0; + PyObject *type = PyType_FromModuleAndSpec(module, &guard_spec, NULL); + if (type == NULL) + return -1; + PyObject *copier = msgspec_fastapi_copier_type(module); + if (copier == NULL) { + Py_DECREF(type); + return -1; + } + if (PyModule_AddObjectRef(module, "_Guard", type) < 0 || + PyModule_AddObjectRef(module, "_Copier", copier) < 0) { + Py_DECREF((PyObject *)type); + Py_DECREF(copier); + return -1; + } + state->guard_type = type; + state->copier_type = copier; + return 0; +} + +static PyModuleDef_Slot module_slots[] = {{Py_mod_exec, module_exec}, {0, NULL}}; + +static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT, + .m_name = "msgspec_fastapi._defaults", + .m_doc = + "Internal checks for copied defaults and metadata.", + .m_size = sizeof(ModuleState), + .m_slots = module_slots, + .m_traverse = module_traverse, + .m_clear = module_clear, + .m_free = module_free}; + +PyMODINIT_FUNC PyInit__defaults(void) { + return PyModuleDef_Init(&module_def); +} diff --git a/src/msgspec_fastapi/_defaults.pyi b/src/msgspec_fastapi/_defaults.pyi new file mode 100644 index 0000000..790c513 --- /dev/null +++ b/src/msgspec_fastapi/_defaults.pyi @@ -0,0 +1,50 @@ +from collections.abc import Callable +from types import ModuleType +from typing import Any, final + +@final +class _Guard: + def __init__( + self, + fallback: Callable[[Any], None], + registry: dict[type[Any], Any], + trusted_union_reducer: Any, + union_type: type[Any], + function_type: type[Any], + builtin_function_type: type[Any], + getset_descriptor_type: type[Any], + member_descriptor_type: type[Any], + method_descriptor_type: type[Any], + wrapper_descriptor_type: type[Any], + struct_type: type[Any], + enum_type: type[Any], + atomic_ids: set[int], + reference_ids: set[int], + model_brands: tuple[str, ...], + native_state_methods: tuple[tuple[str, tuple[Any, ...]], ...], + state_names: tuple[str, ...], + enum_namespace: Any, + missing: Any, + mro_descriptor: Any, + dict_descriptor: Any, + name_descriptor: Any, + ) -> None: ... + def check(self, value: Any, /) -> None: ... + def class_info(self, cls: type[Any], /) -> tuple[str, Any, tuple[Any, ...]]: ... + +@final +class _Copier: + def __init__( + self, + copy_module: ModuleType, + deepcopy: Callable[..., Any], + list_copier: Callable[..., Any], + dict_copier: Callable[..., Any], + tuple_copier: Callable[..., Any], + atomic_copier: Callable[..., Any] | None, + reconstruct: Callable[..., Any], + atomic_first: bool, + memo_sentinel: object, + ) -> None: ... + def __call__(self, value: Any, memo: dict[int, Any] | None = None, /) -> Any: ... + def deepcopy(self, value: Any, memo: dict[int, Any] | None = None, /) -> Any: ... diff --git a/src/msgspec_fastapi/_defaults_copy.c b/src/msgspec_fastapi/_defaults_copy.c new file mode 100644 index 0000000..34f873b --- /dev/null +++ b/src/msgspec_fastapi/_defaults_copy.c @@ -0,0 +1,809 @@ +#define Py_LIMITED_API 0x030b0000 +#include + +#define COPIER_POLICY_FIELDS(X) \ + X(module) \ + X(fallback) \ + X(list_copier) X(dict_copier) X(tuple_copier) X(atomic_copier) X(original_reconstruct) \ + X(memo_sentinel) +#define COPIER_NAME_FIELDS(X) \ + X(dispatch_name) \ + X(registry_name) \ + X(atomicset_name) \ + X(keepalive_name) \ + X(reconstruct_name) \ + X(copy_name) X(reduce_name) X(reduceex_name) X(items_name) X(get_name) X(setstate_name) \ + X(namespace_name) X(update_name) + +typedef struct { + PyObject_HEAD +#define OWNED_FIELD(name) PyObject *name; + COPIER_POLICY_FIELDS(OWNED_FIELD) + COPIER_NAME_FIELDS(OWNED_FIELD) +#undef OWNED_FIELD + int atomic_first, initialized; +} Copier; + +typedef struct { + Copier *owner; + PyObject *memo; +} Context; +static PyObject *clone(Context *, PyObject *); + +static void replace_owned(PyObject **slot, PyObject *next) { + PyObject *old = *slot; + *slot = next; + Py_XDECREF(old); +} + +static PyObject *module_value(Context *c, PyObject *name) { + PyObject *value = PyDict_GetItemWithError(PyModule_GetDict(c->owner->module), name); + if (value == NULL && !PyErr_Occurred()) + PyErr_SetObject(PyExc_AttributeError, name); + return value == NULL ? NULL : Py_NewRef(value); +} + +static PyObject *id_key(PyObject *value) { + return PyLong_FromVoidPtr(value); +} + +static PyObject *memo_get(Context *c, PyObject *value) { + PyObject *key = id_key(value); + if (key == NULL) + return NULL; + PyObject *out = PyDict_GetItemWithError(c->memo, key); + Py_XINCREF(out); + Py_DECREF(key); + return out; +} + +static int memo_set(Context *c, PyObject *value, PyObject *result) { + PyObject *key = id_key(value); + if (key == NULL) + return -1; + int out = PyDict_SetItem(c->memo, key, result); + Py_DECREF(key); + return out; +} + +static int keep_alive(Context *c, PyObject *value) { + PyObject *function = module_value(c, c->owner->keepalive_name); + if (function == NULL) + return -1; + PyObject *result = PyObject_CallFunctionObjArgs(function, value, c->memo, NULL); + Py_DECREF(function); + if (result == NULL) + return -1; + Py_DECREF(result); + return 0; +} + +static int atomic(PyObject *x) { + return x == Py_None || PyBool_Check(x) || PyLong_CheckExact(x) || PyFloat_CheckExact(x) || + PyUnicode_CheckExact(x) || PyBytes_CheckExact(x); +} + +static PyObject *clone_list(Context *c, PyObject *x) { + PyObject *out = PyList_New(0), *item = NULL; + if (out == NULL) + return NULL; + if (memo_set(c, x, out) < 0) + goto failed; + for (Py_ssize_t i = 0; i < PyList_Size(x); i++) { + PyObject *next = Py_NewRef(PyList_GetItem(x, i)); + replace_owned(&item, next); + PyObject *value = clone(c, item); + if (value == NULL) + goto failed; + int status = PyList_Append(out, value); + Py_DECREF(value); + if (status < 0) + goto failed; + } + Py_XDECREF(item); + return out; +failed: + Py_XDECREF(item); + Py_DECREF(out); + return NULL; +} + +static PyObject *clone_tuple(Context *c, PyObject *x) { + Py_ssize_t n = PyTuple_Size(x); + PyObject *out = PyList_New(0); + if (out == NULL) + return NULL; + int changed = 0; + for (Py_ssize_t i = 0; i < n; i++) { + PyObject *item = PyTuple_GetItem(x, i); + PyObject *value = clone(c, item); + if (value == NULL) { + Py_DECREF(out); + return NULL; + } + if (item != value) + changed = 1; + int status = PyList_Append(out, value); + Py_DECREF(value); + if (status < 0) { + Py_DECREF(out); + return NULL; + } + } + PyObject *cached = memo_get(c, x); + if (cached != NULL) { + Py_DECREF(out); + return cached; + } + if (PyErr_Occurred()) { + Py_DECREF(out); + return NULL; + } + if (!changed) { + Py_DECREF(out); + return Py_NewRef(x); + } + PyObject *result = PyList_AsTuple(out); + Py_DECREF(out); + return result; +} + +static PyObject *clone_dict(Context *c, PyObject *x) { + PyObject *out = PyDict_New(), *iter = NULL, *key_local = NULL, *value_local = NULL; + if (out == NULL) + return NULL; + if (memo_set(c, x, out) < 0) + goto failed; + PyObject *items = PyObject_CallMethodObjArgs(x, c->owner->items_name, NULL); + if (items == NULL) + goto failed; + iter = PyObject_GetIter(items); + Py_DECREF(items); + if (iter == NULL) + goto failed; + PyObject *pair; + while ((pair = PyIter_Next(iter)) != NULL) { + PyObject *next_key = Py_NewRef(PyTuple_GetItem(pair, 0)); + PyObject *next_value = Py_NewRef(PyTuple_GetItem(pair, 1)); + replace_owned(&key_local, next_key); + replace_owned(&value_local, next_value); + Py_DECREF(pair); + PyObject *value = clone(c, value_local); + PyObject *key = value == NULL ? NULL : clone(c, key_local); + if (key == NULL) { + Py_XDECREF(value); + goto failed; + } + int status = PyObject_SetItem(out, key, value); + Py_DECREF(key); + Py_DECREF(value); + if (status < 0) + goto failed; + } + if (PyErr_Occurred()) + goto failed; + Py_DECREF(iter); + if (c->owner->atomic_first) { + Py_XDECREF(value_local); + Py_XDECREF(key_local); + } else { + Py_XDECREF(key_local); + Py_XDECREF(value_local); + } + return out; +failed: + Py_XDECREF(iter); + if (c->owner->atomic_first) { + Py_XDECREF(value_local); + Py_XDECREF(key_local); + } else { + Py_XDECREF(key_local); + Py_XDECREF(value_local); + } + Py_DECREF(out); + return NULL; +} + +static PyObject *optional_attr(Context *c, PyObject *x, PyObject *name) { + (void)c; + PyObject *out = PyObject_GetAttr(x, name); + if (out == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { + PyErr_Clear(); + return Py_NewRef(Py_None); + } + return out; +} + +typedef struct { + PyObject *x, *memo, *d, *y, *cls, *copier, *reductor, *rv; +} CopyFrame; + +static void frame_clear(Context *c, CopyFrame *frame) { + if (c->owner->atomic_first) { + Py_CLEAR(frame->rv); + Py_CLEAR(frame->reductor); + Py_CLEAR(frame->copier); + Py_CLEAR(frame->y); + Py_CLEAR(frame->d); + Py_CLEAR(frame->cls); + Py_CLEAR(frame->memo); + Py_CLEAR(frame->x); + } else { + Py_CLEAR(frame->x); + Py_CLEAR(frame->memo); + Py_CLEAR(frame->d); + Py_CLEAR(frame->y); + Py_CLEAR(frame->cls); + Py_CLEAR(frame->copier); + Py_CLEAR(frame->reductor); + Py_CLEAR(frame->rv); + } +} + +/* The source helper's state branch remains dynamic after construction. */ +typedef struct { + PyObject *state, *y, *slotstate, *key, *value; +} StateFrame; + +static void state_frame_clear(Context *c, StateFrame *frame) { + if (c->owner->atomic_first) { + Py_CLEAR(frame->value); + Py_CLEAR(frame->key); + Py_CLEAR(frame->slotstate); + Py_CLEAR(frame->y); + Py_CLEAR(frame->state); + } else { + Py_CLEAR(frame->state); + Py_CLEAR(frame->y); + Py_CLEAR(frame->slotstate); + Py_CLEAR(frame->key); + Py_CLEAR(frame->value); + } +} + +static int unpack_pair(Context *c, PyObject *source, PyObject **first, PyObject **second) { + PyObject *iter = PyObject_GetIter(source); + if (iter == NULL) { + if (PyErr_ExceptionMatches(PyExc_TypeError) && + PyType_GetSlot(Py_TYPE(source), Py_tp_iter) == NULL && !PySequence_Check(source)) { + PyObject *name = PyType_GetName(Py_TYPE(source)); + if (name != NULL) { + PyErr_Format(PyExc_TypeError, "cannot unpack non-iterable %U object", name); + Py_DECREF(name); + } + } + return -1; + } + PyObject *a = PyIter_Next(iter), *b = NULL, *extra = NULL; + int count = 0; + if (a != NULL) { + count = 1; + b = PyIter_Next(iter); + } + if (b != NULL) { + count = 2; + extra = PyIter_Next(iter); + } + if (PyErr_Occurred()) + goto failed; + if (extra != NULL) { + Py_CLEAR(extra); + Py_ssize_t size = -1; + if (c->owner->atomic_first) { + if (PyList_CheckExact(source)) + size = PyList_Size(source); + else if (PyTuple_CheckExact(source)) + size = PyTuple_Size(source); + else if (PyDict_CheckExact(source)) + size = PyDict_Size(source); + } + if (size > 2) + PyErr_Format(PyExc_ValueError, "too many values to unpack (expected 2, got %zd)", size); + else + PyErr_SetString(PyExc_ValueError, "too many values to unpack (expected 2)"); + goto failed; + } + if (count < 2) { + PyErr_Format(PyExc_ValueError, "not enough values to unpack (expected 2, got %d)", count); + goto failed; + } + Py_DECREF(iter); + *first = a; + *second = b; + return 0; +failed: + Py_XDECREF(extra); + Py_XDECREF(b); + Py_XDECREF(a); + Py_DECREF(iter); + return -1; +} + +static void argument_stopiteration(void) { + if (!PyErr_ExceptionMatches(PyExc_StopIteration)) + return; + PyObject *type = NULL, *value = NULL, *traceback = NULL; + PyErr_Fetch(&type, &value, &traceback); + PyErr_NormalizeException(&type, &value, &traceback); + if (!PyErr_Occurred() && value != NULL && + PyException_SetTraceback(value, traceback == NULL ? Py_None : traceback) == 0) { + PyObject *error = + PyObject_CallFunction(PyExc_RuntimeError, "s", "generator raised StopIteration"); + if (error != NULL) { + PyException_SetCause(error, Py_NewRef(value)); + PyException_SetContext(error, Py_NewRef(value)); + PyErr_SetObject(PyExc_RuntimeError, error); + Py_DECREF(error); + } + } + Py_XDECREF(type); + Py_XDECREF(value); + Py_XDECREF(traceback); +} + +static PyObject *construct(Context *c, CopyFrame *frame) { + PyObject *args = PyTuple_GetItem(frame->rv, 1); + PyObject *copied = PyList_New(0); + if (copied == NULL) + return NULL; + for (Py_ssize_t i = 0; i < PyTuple_Size(args); i++) { + PyObject *value = clone(c, PyTuple_GetItem(args, i)); + if (value == NULL) { + argument_stopiteration(); + Py_DECREF(copied); + return NULL; + } + int status = PyList_Append(copied, value); + Py_DECREF(value); + if (status < 0) { + Py_DECREF(copied); + return NULL; + } + } + PyObject *call_args = PyList_AsTuple(copied); + Py_DECREF(copied); + if (call_args == NULL) + return NULL; + PyObject *out = PyObject_CallObject(PyTuple_GetItem(frame->rv, 0), call_args); + Py_DECREF(call_args); + if (out != NULL && memo_set(c, frame->x, out) < 0) + Py_CLEAR(out); + return out; +} + +static PyObject *reconstruct_state(Context *c, CopyFrame *frame) { + StateFrame state = {0}; + state.state = Py_NewRef(PyTuple_GetItem(frame->rv, 2)); + state.y = construct(c, frame); + if (state.y == NULL) + goto done; + PyObject *copied_state = clone(c, state.state); + if (copied_state == NULL) + goto done; + replace_owned(&state.state, copied_state); + PyObject *probe = PyObject_GetAttr(state.y, c->owner->setstate_name); + if (probe != NULL) { + Py_DECREF(probe); + PyObject *function = PyObject_GetAttr(state.y, c->owner->setstate_name); + if (function == NULL) + goto done; + PyObject *result = PyObject_CallFunctionObjArgs(function, state.state, NULL); + Py_DECREF(function); + if (result == NULL) + goto done; + Py_DECREF(result); + } else { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) + goto done; + PyErr_Clear(); + int tuple_state = PyObject_IsInstance(state.state, (PyObject *)&PyTuple_Type); + if (tuple_state < 0) + goto done; + Py_ssize_t length = tuple_state ? PyObject_Length(state.state) : -2; + if (length == -1) + goto done; + if (length == 2) { + PyObject *first = NULL, *second = NULL; + if (unpack_pair(c, state.state, &first, &second) < 0) + goto done; + replace_owned(&state.state, first); + state.slotstate = second; + } else + state.slotstate = Py_NewRef(Py_None); + if (state.state != Py_None) { + PyObject *namespace = PyObject_GetAttr(state.y, c->owner->namespace_name); + if (namespace == NULL) + goto done; + PyObject *result = + PyObject_CallMethodObjArgs(namespace, c->owner->update_name, state.state, NULL); + Py_DECREF(namespace); + if (result == NULL) + goto done; + Py_DECREF(result); + } + if (state.slotstate != Py_None) { + PyObject *items = + PyObject_CallMethodObjArgs(state.slotstate, c->owner->items_name, NULL); + if (items == NULL) + goto done; + PyObject *iter = PyObject_GetIter(items); + Py_DECREF(items); + if (iter == NULL) + goto done; + PyObject *pair; + while ((pair = PyIter_Next(iter)) != NULL) { + PyObject *first = NULL, *second = NULL; + int status = unpack_pair(c, pair, &first, &second); + Py_DECREF(pair); + if (status < 0) { + Py_DECREF(iter); + goto done; + } + replace_owned(&state.key, first); + replace_owned(&state.value, second); + if (PyObject_SetAttr(state.y, state.key, state.value) < 0) { + Py_DECREF(iter); + goto done; + } + } + Py_DECREF(iter); + if (PyErr_Occurred()) + goto done; + } + } + PyObject *out = Py_NewRef(state.y); + state_frame_clear(c, &state); + return out; +done: + state_frame_clear(c, &state); + return NULL; +} + +static PyObject *reconstruct(Context *c, CopyFrame *frame) { + PyObject *out = NULL; + PyObject *x = frame->x, *rv = frame->rv; + PyObject *reconstruct = module_value(c, c->owner->reconstruct_name); + if (reconstruct == NULL) { + return NULL; + } + int standard = reconstruct == c->owner->original_reconstruct; + Py_ssize_t rv_size = PyTuple_CheckExact(rv) ? PyTuple_Size(rv) : -1; + int state_shape = rv_size == 3 || (rv_size == 5 && PyTuple_GetItem(rv, 3) == Py_None && + PyTuple_GetItem(rv, 4) == Py_None); + if (standard && state_shape && PyTuple_CheckExact(PyTuple_GetItem(rv, 1)) && + PyDict_CheckExact(PyTuple_GetItem(rv, 2))) + out = reconstruct_state(c, frame); + else if (standard && rv_size == 2 && PyTuple_CheckExact(PyTuple_GetItem(rv, 1))) { + out = construct(c, frame); + } else { + PyObject *sequence = PySequence_Tuple(rv); + if (sequence == NULL) + goto reconstruct_done; + Py_ssize_t n = PyTuple_Size(sequence); + if (n > PY_SSIZE_T_MAX - 2) { + Py_DECREF(sequence); + PyErr_NoMemory(); + goto reconstruct_done; + } + PyObject *args = PyTuple_New(n + 2); + if (args == NULL) { + Py_DECREF(sequence); + goto reconstruct_done; + } + PyTuple_SetItem(args, 0, Py_NewRef(x)); + PyTuple_SetItem(args, 1, Py_NewRef(c->memo)); + for (Py_ssize_t i = 0; i < n; i++) + PyTuple_SetItem(args, i + 2, Py_NewRef(PyTuple_GetItem(sequence, i))); + if (standard) { + PyObject *callback = Py_NewRef((PyObject *)c->owner); + PyObject *kwargs = + callback == NULL ? NULL : Py_BuildValue("{sO}", "deepcopy", callback); + out = kwargs == NULL ? NULL : PyObject_Call(reconstruct, args, kwargs); + Py_XDECREF(kwargs); + Py_XDECREF(callback); + } else + out = PyObject_CallObject(reconstruct, args); + Py_DECREF(args); + Py_DECREF(sequence); + } +reconstruct_done: + Py_DECREF(reconstruct); + return out; +} + +static PyObject *mapping_get(PyObject *mapping, PyObject *key, PyObject *get_name) { + if (PyDict_CheckExact(mapping)) { + PyObject *out = PyDict_GetItemWithError(mapping, key); + if (out != NULL) + return Py_NewRef(out); + if (PyErr_Occurred()) + return NULL; + return Py_NewRef(Py_None); + } + return PyObject_CallMethodObjArgs(mapping, get_name, key, NULL); +} + +static PyObject *clone_impl(Context *c, PyObject *x) { + CopyFrame f = {0}; + f.x = Py_NewRef(x); + f.memo = Py_NewRef(c->memo); + if (c->owner->atomic_first) { + f.cls = Py_NewRef((PyObject *)Py_TYPE(x)); + PyObject *types = module_value(c, c->owner->atomicset_name); + if (types == NULL) + goto done; + int found = PySequence_Contains(types, f.cls); + Py_DECREF(types); + if (found < 0) + goto done; + if (found) { + f.y = Py_NewRef(x); + goto done; + } + } + f.d = id_key(x); + if (f.d == NULL) + goto done; + PyObject *cached = PyDict_GetItemWithError(c->memo, f.d); + if (cached != NULL && cached != c->owner->memo_sentinel) { + f.y = Py_NewRef(cached); + goto done; + } + if (PyErr_Occurred()) + goto done; + if (f.cls == NULL) + f.cls = Py_NewRef((PyObject *)Py_TYPE(x)); + PyObject *table = module_value(c, c->owner->dispatch_name); + if (table == NULL) + goto done; + f.copier = mapping_get(table, f.cls, c->owner->get_name); + Py_DECREF(table); + if (f.copier == NULL) + goto done; + if (f.copier != Py_None) { + if (f.copier == c->owner->atomic_copier && atomic(x)) + f.y = Py_NewRef(x); + else if (f.cls == (PyObject *)&PyList_Type && f.copier == c->owner->list_copier) + f.y = clone_list(c, x); + else if (f.cls == (PyObject *)&PyDict_Type && f.copier == c->owner->dict_copier) + f.y = clone_dict(c, x); + else if (f.cls == (PyObject *)&PyTuple_Type && f.copier == c->owner->tuple_copier) + f.y = clone_tuple(c, x); + else + f.y = PyObject_CallFunctionObjArgs(f.copier, x, c->memo, NULL); + } else if (PyType_IsSubtype((PyTypeObject *)f.cls, &PyType_Type)) + f.y = Py_NewRef(x); + else { + PyObject *next = optional_attr(c, x, c->owner->copy_name); + if (next == NULL) + goto done; + replace_owned(&f.copier, next); + if (f.copier != Py_None) + f.y = PyObject_CallFunctionObjArgs(f.copier, c->memo, NULL); + else { + PyObject *registry = module_value(c, c->owner->registry_name); + if (registry == NULL) + goto done; + f.reductor = mapping_get(registry, f.cls, c->owner->get_name); + Py_DECREF(registry); + if (f.reductor == NULL) + goto done; + int present = PyObject_IsTrue(f.reductor); + if (present < 0) + goto done; + if (present) + f.rv = PyObject_CallFunctionObjArgs(f.reductor, x, NULL); + else { + next = optional_attr(c, x, c->owner->reduceex_name); + if (next == NULL) + goto done; + replace_owned(&f.reductor, next); + if (f.reductor != Py_None) + f.rv = PyObject_CallFunction(f.reductor, "i", 4); + else { + next = optional_attr(c, x, c->owner->reduce_name); + if (next == NULL) + goto done; + replace_owned(&f.reductor, next); + present = PyObject_IsTrue(f.reductor); + if (present < 0) + goto done; + if (present) + f.rv = PyObject_CallFunctionObjArgs(f.reductor, NULL); + else { + PyObject *error = + PyDict_GetItemString(PyModule_GetDict(c->owner->module), "Error"); + if (error != NULL) + PyErr_Format(error, "un(deep)copyable object of type %S", f.cls); + else + PyErr_SetString(PyExc_TypeError, "Missing copy.Error"); + } + } + } + if (f.rv != NULL) { + int string_rv = PyObject_IsInstance(f.rv, (PyObject *)&PyUnicode_Type); + if (string_rv < 0) + goto done; + if (string_rv) + f.y = Py_NewRef(x); + else + f.y = reconstruct(c, &f); + } + } + } + if (f.y != NULL && f.y != x) { + if (PyDict_SetItem(c->memo, f.d, f.y) < 0 || keep_alive(c, x) < 0) + Py_CLEAR(f.y); + } +done:; + PyObject *out = f.y == NULL ? NULL : Py_NewRef(f.y); + frame_clear(c, &f); + return out; +} + +static PyObject *clone(Context *c, PyObject *x) { + if (Py_EnterRecursiveCall(" while copying msgspec defaults")) + return NULL; + PyObject *out = clone_impl(c, x); + Py_LeaveRecursiveCall(); + return out; +} + +static int copier_ready(Copier *self) { + if (!self->initialized || self->module == NULL) { + PyErr_SetString(PyExc_TypeError, "Copier is not initialized"); + return 0; + } + return 1; +} + +static PyObject *copier_copy(Copier *self, PyObject *args) { + PyObject *value, *memo = Py_None; + if (!copier_ready(self)) + return NULL; + if (!PyArg_ParseTuple(args, "O|O:deepcopy", &value, &memo)) + return NULL; + if (memo == Py_None) + memo = PyDict_New(); + else + Py_INCREF(memo); + if (memo == NULL) + return NULL; + if (!PyDict_CheckExact(memo)) { + PyObject *out = PyObject_CallFunctionObjArgs(self->fallback, value, memo, NULL); + Py_DECREF(memo); + return out; + } + Context current = {self, memo}; + PyObject *out = clone(¤t, value); + Py_DECREF(memo); + return out; +} + +static PyObject *copier_call(Copier *self, PyObject *args, PyObject *kwargs) { + if (kwargs != NULL && PyDict_Size(kwargs)) { + PyErr_SetString(PyExc_TypeError, "deepcopy accepts positional arguments only"); + return NULL; + } + return copier_copy(self, args); +} + +static int copier_traverse(Copier *self, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE((PyObject *)self)); +#define VISIT_FIELD(name) Py_VISIT(self->name); + COPIER_POLICY_FIELDS(VISIT_FIELD) + COPIER_NAME_FIELDS(VISIT_FIELD) +#undef VISIT_FIELD + return 0; +} + +static int copier_clear(Copier *self) { +#define CLEAR_FIELD(name) Py_CLEAR(self->name); + COPIER_POLICY_FIELDS(CLEAR_FIELD) + COPIER_NAME_FIELDS(CLEAR_FIELD) +#undef CLEAR_FIELD + return 0; +} + +static void copier_dealloc(Copier *self) { + PyTypeObject *type = Py_TYPE((PyObject *)self); + PyObject_GC_UnTrack(self); + copier_clear(self); + ((freefunc)PyType_GetSlot(type, Py_tp_free))(self); + Py_DECREF(type); +} + +static PyObject *copier_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { + (void)args; + (void)kwargs; + return PyType_GenericAlloc(type, 0); +} + +static int copier_init(Copier *self, PyObject *args, PyObject *kwargs) { + static char *keywords[] = { + "copy_module", "deepcopy", "list_copier", "dict_copier", "tuple_copier", + "atomic_copier", "reconstruct", "atomic_first", "memo_sentinel", NULL}; + PyObject *module, *fallback, *list_copier, *dict_copier, *tuple_copier; + PyObject *atomic_copier, *original_reconstruct, *atomic_first, *memo_sentinel; + if (self->initialized) { + PyErr_SetString(PyExc_TypeError, "Copier cannot be reinitialized"); + return -1; + } + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOOOOOOO:_Copier", keywords, &module, + &fallback, &list_copier, &dict_copier, &tuple_copier, + &atomic_copier, &original_reconstruct, &atomic_first, + &memo_sentinel)) + return -1; + if (!PyModule_Check(module)) { + PyErr_SetString(PyExc_TypeError, "copy_module must be a module"); + return -1; + } + PyObject *functions[] = {fallback, list_copier, dict_copier, tuple_copier, + original_reconstruct}; + for (size_t i = 0; i < sizeof(functions) / sizeof(functions[0]); i++) { + if (!PyCallable_Check(functions[i])) { + PyErr_SetString(PyExc_TypeError, "Copy helpers must be callable"); + return -1; + } + } + if (atomic_copier != Py_None && !PyCallable_Check(atomic_copier)) { + PyErr_SetString(PyExc_TypeError, "atomic_copier must be callable or None"); + return -1; + } + if (!PyBool_Check(atomic_first)) { + PyErr_SetString(PyExc_TypeError, "atomic_first must be a bool"); + return -1; + } +#define SAVE_FIELD(name) self->name = Py_NewRef(name); + COPIER_POLICY_FIELDS(SAVE_FIELD) +#undef SAVE_FIELD + self->atomic_first = atomic_first == Py_True; +#define SAVE_NAME(field, name) \ + do { \ + self->field = PyUnicode_InternFromString(name); \ + if (self->field == NULL) { \ + copier_clear(self); \ + return -1; \ + } \ + } while (0) + SAVE_NAME(dispatch_name, "_deepcopy_dispatch"); + SAVE_NAME(registry_name, "dispatch_table"); + SAVE_NAME(atomicset_name, "_atomic_types"); + SAVE_NAME(keepalive_name, "_keep_alive"); + SAVE_NAME(reconstruct_name, "_reconstruct"); + SAVE_NAME(copy_name, "__deepcopy__"); + SAVE_NAME(reduce_name, "__reduce__"); + SAVE_NAME(reduceex_name, "__reduce_ex__"); + SAVE_NAME(items_name, "items"); + SAVE_NAME(get_name, "get"); + SAVE_NAME(setstate_name, "__setstate__"); + SAVE_NAME(namespace_name, "__dict__"); + SAVE_NAME(update_name, "update"); +#undef SAVE_NAME + self->initialized = 1; + return 0; +} + +static PyMethodDef copier_methods[] = { + {"deepcopy", (PyCFunction)copier_copy, METH_VARARGS, + "deepcopy($self, value, memo=None, /)\n--\n\nCopy data using a shared deepcopy memo."}, + {NULL, NULL, 0, NULL}}; +static PyType_Slot copier_slots[] = { + {Py_tp_new, copier_new}, + {Py_tp_init, copier_init}, + {Py_tp_call, copier_call}, + {Py_tp_dealloc, copier_dealloc}, + {Py_tp_traverse, copier_traverse}, + {Py_tp_clear, copier_clear}, + {Py_tp_methods, copier_methods}, + {Py_tp_doc, "_Copier(copy_module, deepcopy, list_copier, dict_copier, tuple_copier, " + "atomic_copier, reconstruct, atomic_first, memo_sentinel)" + "\n--\n\nCopy supported defaults with stdlib protocol ordering."}, + {0, NULL}}; +static PyType_Spec copier_spec = { + "msgspec_fastapi._defaults._Copier", sizeof(Copier), 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE, copier_slots}; + +PyObject *msgspec_fastapi_copier_type(PyObject *module) { + return PyType_FromModuleAndSpec(module, &copier_spec, NULL); +} diff --git a/src/msgspec_fastapi/body_errors.py b/src/msgspec_fastapi/body_errors.py new file mode 100644 index 0000000..44ee568 --- /dev/null +++ b/src/msgspec_fastapi/body_errors.py @@ -0,0 +1,506 @@ +"""Error-only JSON traversal; successful requests retain typed msgspec decoding.""" + +from __future__ import annotations + +import dataclasses +import datetime +import decimal +import enum +import inspect +import uuid +from collections.abc import Callable +from types import UnionType +from typing import ( + Annotated, + Any, + Literal, + NamedTuple, + Tuple, + Union, + get_args, + get_origin, + get_type_hints, +) + +import msgspec +from typing_extensions import NotRequired, Required, is_typeddict + +from .validation import base_type, pattern_validator + + +class _Unsupported(Exception): + pass + + +class _Node(NamedTuple): + target: Any + origin: Any + args: tuple[Any, ...] + cls: Any + metadata: tuple[Any, ...] + check_patterns: Callable[..., list[dict[str, Any]]] | None + + +_TYPE_MRO = type.__dict__['__mro__'].__get__ +_TYPE_DICT = type.__dict__['__dict__'].__get__ +_ENUM_METHODS = tuple( + (name, tuple(inspect.getattr_static(base, name) for base in bases)) + for name, bases in ( + ('_missing_', (enum.Enum,)), + ('__hash__', (enum.Enum, object, int, str)), + ('__eq__', (enum.Enum, object, int, str)), + ('__getattribute__', (enum.Enum, object, int, str)), + ) +) + + +def _type_guard(watched: list[tuple[type, tuple[str, ...]]]) -> Callable[[], bool]: + sentinel = object() + namespaces = [] + mros = [] + seen: set[tuple[int, str]] = set() + classes: set[int] = set() + for cls, names in watched: + mro = _TYPE_MRO(cls) + if id(cls) not in classes: + classes.add(id(cls)) + mros.append((cls, mro)) + for base in mro: + mapping = _TYPE_DICT(base) + for name in names: + key = (id(base), name) + if key not in seen: + seen.add(key) + namespaces.append((mapping, name, mapping.get(name, sentinel))) + + def unchanged() -> bool: + return all(_TYPE_MRO(cls) is before for cls, before in mros) and all( + mapping.get(name, sentinel) is before for mapping, name, before in namespaces + ) + + return unchanged + + +def _fields(annotation: Any) -> tuple[list[tuple[str | int, Any, bool]], Any]: + cls = get_origin(annotation) or annotation + if isinstance(cls, type) and issubclass(cls, msgspec.Struct): + config = cls.__struct_config__ + offset = int(config.array_like and config.tag is not None) + fields = [ + (index + offset if config.array_like else field.encode_name, field.type, field.required) + for index, field in enumerate(msgspec.structs.fields(annotation)) + ] + return fields, config + if is_typeddict(cls): + if get_origin(annotation) is not None: + raise _Unsupported + hints = get_type_hints(cls, include_extras=True) + fields = [] + for name, target in hints.items(): + required = name in cls.__required_keys__ + if get_origin(target) in (Required, NotRequired): + required = get_origin(target) is Required + target = get_args(target)[0] + fields.append((name, target, required)) + return fields, None + if isinstance(cls, type) and dataclasses.is_dataclass(cls): + if get_origin(annotation) is not None: + raise _Unsupported + hints = get_type_hints(cls, include_extras=True) + return [ + ( + field.name, + hints[field.name], + field.default is dataclasses.MISSING + and field.default_factory is dataclasses.MISSING, + ) + for field in dataclasses.fields(cls) + ], None + raise _Unsupported + + +def _safe(annotation: Any, seen: set[Any], field_cache: dict[Any, Any], plain: set[Any]) -> bool: + """Check hooks and identify types decoded without constructing models.""" + annotation = base_type(annotation) + origin = get_origin(annotation) + cls = origin or annotation + if origin is None and not isinstance(cls, type) and annotation is not Any: + raise _Unsupported + if isinstance(cls, type) and issubclass(cls, tuple) and hasattr(cls, '_fields'): + raise _Unsupported + if isinstance(cls, type) and ( + issubclass(cls, msgspec.Struct) or dataclasses.is_dataclass(cls) or is_typeddict(cls) + ): + if annotation in seen: + return False + seen.add(annotation) + if getattr(cls, '__post_init__', None) is not None: + raise _Unsupported + field_cache[annotation] = _fields(annotation) + fields, _ = field_cache[annotation] + for _, child, _ in fields: + _safe(child, seen, field_cache, plain) + return False + if origin in (list, tuple, set, frozenset, dict, Union, UnionType): + children = [ + _safe(arg, seen, field_cache, plain) + for arg in get_args(annotation) + if arg is not Ellipsis + ] + decodable = all(children) + elif origin is Literal or annotation is Any: + decodable = True + elif isinstance(cls, type) and issubclass(cls, enum.Enum): + for name, defaults in _ENUM_METHODS: + method = inspect.getattr_static(cls, name) + if not any(method is default for default in defaults): + raise _Unsupported + decodable = True + elif cls in ( + type(None), + bool, + int, + float, + str, + bytes, + bytearray, + memoryview, + datetime.datetime, + datetime.date, + datetime.time, + datetime.timedelta, + decimal.Decimal, + uuid.UUID, + msgspec.Raw, + ): + decodable = True + else: + raise _Unsupported + if decodable: + plain.add(annotation) + return decodable + + +def _error( + loc: tuple[str | int, ...], message: str, kind: str = 'value_error.msgspec' +) -> dict[str, Any]: + return {'loc': loc, 'msg': message, 'type': kind} + + +def _convert_errors( + value: Any, target: Any, loc: tuple[str | int, ...], nodes: dict[Any, _Node] +) -> list[dict[str, Any]]: + try: + msgspec.json.decode(msgspec.json.encode(value), type=target) + except msgspec.ValidationError as exc: + return [_error(loc, str(exc))] + check = nodes[target].check_patterns + return check(value, loc) if check is not None else [] + + +def _walk( + value: Any, + annotation: Any, + loc: tuple[str | int, ...], + field_cache: dict[Any, Any], + plain: set[Any], + nodes: dict[Any, _Node], +) -> list[dict[str, Any]]: + target, origin, args, cls, metadata, _ = nodes[annotation] + if target in plain: + errors = _convert_errors(value, annotation, loc, nodes) + if not errors or origin not in (list, tuple, set, frozenset, dict): + return errors + if origin is Union or origin is UnionType: + if Any in args: + return [] + if value is None and type(None) in args: + return [] + structured = [] + for child in args: + child_cls = get_origin(base_type(child)) or base_type(child) + if isinstance(child_cls, type) and ( + issubclass(child_cls, msgspec.Struct) + or dataclasses.is_dataclass(child_cls) + or is_typeddict(child_cls) + ): + config = ( + child_cls.__struct_config__ if issubclass(child_cls, msgspec.Struct) else None + ) + if isinstance(value, dict) and not (config and config.array_like): + structured.append(child) + elif isinstance(value, list) and config and config.array_like: + structured.append(child) + if len(structured) == 1: + return _walk(value, structured[0], loc, field_cache, plain, nodes) + if structured: + for child in structured: + config = (get_origin(base_type(child)) or base_type(child)).__struct_config__ + key = 0 if config.array_like else config.tag_field + if isinstance(value, dict): + tag = value.get(key, msgspec.NODEFAULT) + else: + tag = value[0] if value else msgspec.NODEFAULT + if type(tag) is type(config.tag) and tag == config.tag: + return _walk(value, child, loc, field_cache, plain, nodes) + key = ( + 0 + if isinstance(value, list) + else ( + get_origin(base_type(structured[0])) or base_type(structured[0]) + ).__struct_config__.tag_field + ) + return [_error(loc + (key,), 'Invalid or missing type tag')] + if target not in plain: + raise _Unsupported + return _convert_errors(value, annotation, loc, nodes) + + if isinstance(cls, type) and ( + issubclass(cls, msgspec.Struct) or dataclasses.is_dataclass(cls) or is_typeddict(cls) + ): + fields, config = field_cache[target] + array = bool(config and config.array_like) + if not isinstance(value, list if array else dict): + if type(value) is int and not -(1 << 63) <= value < (1 << 64): + return _convert_errors(value, annotation, loc, nodes) + try: + msgspec.convert(value, type=annotation) + except msgspec.ValidationError as exc: + return [_error(loc, str(exc))] + raise _Unsupported + errors = [] + if config and config.tag is not None: + key = 0 if array else config.tag_field + present = len(value) > 0 if array else key in value + if present and (type(value[key]) is not type(config.tag) or value[key] != config.tag): + errors.append(_error(loc + (key,), 'Invalid type tag')) + for key, child, required in fields: + if array: + assert isinstance(value, list) and isinstance(key, int) + present = key < len(value) + else: + assert isinstance(value, dict) + present = key in value + if present: + errors.extend(_walk(value[key], child, loc + (key,), field_cache, plain, nodes)) + elif required: + errors.append(_error(loc + (key,), 'Field required', 'missing')) + if config and config.forbid_unknown_fields: + known = {key for key, _, _ in fields} + if config.tag is not None: + known.add(0 if array else config.tag_field) + for key in range(len(value)) if array else value: + if key not in known: + errors.append(_error(loc + (key,), 'Unexpected field', 'extra_forbidden')) + return errors + + if origin in (list, tuple, set, frozenset, dict): + if target in (tuple[()], Tuple[()]): + raise _Unsupported + if ( + origin in (set, frozenset) + or (origin is dict and args and base_type(args[0]) not in (str, Any)) + ) and any( + isinstance(item, msgspec.Meta) + and (item.min_length is not None or item.max_length is not None) + for item in metadata + ): + raise _Unsupported + if not isinstance(value, dict if origin is dict else list): + return _convert_errors(value, annotation, loc, nodes) + relaxed: Any + if origin is dict: + relaxed = dict[Any, Any] + elif origin is tuple and args and args[-1] is not Ellipsis: + relaxed = origin[tuple(Any for _ in args)] + else: + relaxed = origin[Any] if origin is not tuple else tuple[Any, ...] + if metadata: + relaxed = Annotated[relaxed, *metadata] + errors = [] + try: + msgspec.convert(value, type=relaxed) + except msgspec.ValidationError as exc: + errors.append(_error(loc, str(exc))) + except TypeError as exc: + raise _Unsupported from exc + if origin is dict: + assert isinstance(value, dict) + key_type, child_type = args or (Any, Any) + for key, item in value.items(): + try: + msgspec.json.decode( + msgspec.json.encode({key: None}), type=origin[key_type, Any] + ) + except msgspec.ValidationError as exc: + errors.append(_error(loc + (key, '[key]'), str(exc))) + check = nodes[key_type].check_patterns + if check is not None: + errors.extend(check(key, loc + (key, '[key]'))) + errors.extend(_walk(item, child_type, loc + (key,), field_cache, plain, nodes)) + else: + for index, item in enumerate(value): + if origin is tuple and args and args[-1] is not Ellipsis: + if index >= len(args): + continue + child = args[index] + else: + child = args[0] if args else Any + errors.extend(_walk(item, child, loc + (index,), field_cache, plain, nodes)) + return errors + return _convert_errors(value, annotation, loc, nodes) + + +def compile_body_errors(annotation: Any) -> Callable[..., list[dict[str, Any]]]: + """Prepare immutable schema metadata; keep error lists local to each call.""" + try: + field_cache: dict[Any, Any] = {} + plain: set[Any] = set() + _safe(annotation, set(), field_cache, plain) + nodes: dict[Any, _Node] = {} + pending = [annotation] + while pending: + current = pending.pop() + if current in nodes: + continue + target = base_type(current) + origin, args = get_origin(target), get_args(target) + metadata = get_args(current)[1:] if get_origin(current) is Annotated else () + nodes[current] = _Node( + target, origin, args, origin or target, metadata, pattern_validator(current) + ) + if target in field_cache: + pending.extend(child for _, child, _ in field_cache[target][0]) + if origin is not Literal: + pending.extend(arg for arg in args if arg is not Ellipsis) + watched: list[tuple[type, tuple[str, ...]]] = [] + for node in nodes.values(): + if node.target in field_cache: + watched.append((node.cls, ('__post_init__',))) + elif isinstance(node.cls, type) and issubclass(node.cls, enum.Enum): + watched.append((node.cls, tuple(name for name, _ in _ENUM_METHODS))) + unchanged = _type_guard(watched) + except (_Unsupported, TypeError, NameError, RecursionError): + return lambda value, loc=('body',): [] + + def diagnose(value: Any, loc: tuple[str | int, ...] = ('body',)) -> list[dict[str, Any]]: + if not unchanged(): + return [] + try: + return _walk(value, annotation, loc, field_cache, plain, nodes) + except (_Unsupported, TypeError, NameError, RecursionError): + return [] + + return diagnose + + +def body_errors( + value: Any, annotation: Any, loc: tuple[str | int, ...] = ('body',) +) -> list[dict[str, Any]]: + return compile_body_errors(annotation)(value, loc) + + +def _raw_pure(annotation: Any) -> bool: + if get_origin(annotation) is Annotated: + if any(not isinstance(m, msgspec.Meta) for m in get_args(annotation)[1:]): + return False + annotation = get_args(annotation)[0] + if any(annotation is t for t in (str, int, float, bool, type(None))): + return True + origin, args = get_origin(annotation), get_args(annotation) + if any(origin is t for t in (list, set, frozenset)): + return len(args) == 1 and _raw_pure(args[0]) + if origin is dict: + return len(args) == 2 and all(_raw_pure(a) for a in args) + if origin is tuple: + return bool(args) and all(_raw_pure(a) for a in args if a is not Ellipsis) + if origin is Union or origin is UnionType: + return bool(args) and all(_raw_pure(a) for a in args) + return False + + +def _compile_raw_list_errors( + annotation: Any, full_walk: Callable[..., list[dict[str, Any]]] +) -> Callable[..., list[dict[str, Any]]] | None: + root = base_type(annotation) + args = get_args(root) + if get_origin(root) is not list or len(args) != 1 or pattern_validator(annotation) is not None: + return None + child = args[0] + cls = get_origin(base_type(child)) or base_type(child) + if not isinstance(cls, type) or not issubclass(cls, msgspec.Struct): + return None + try: + _safe(annotation, set(), {}, set()) + fields = msgspec.structs.fields(child) + if not all(_raw_pure(f.type) for f in fields): + return None + config = cls.__struct_config__ + specs = [ + (f'_field_{index}', f.type, msgspec.field(name=f.encode_name)) + if f.required + else (f'_field_{index}', f.type, msgspec.field(name=f.encode_name, default=None)) + for index, f in enumerate(fields) + ] + # JSON-only fields and inert defaults cannot form cycles in this transient model. + wire = msgspec.defstruct( + '_DiagnosticWire', + specs, + kw_only=True, + gc=False, + tag=config.tag, + tag_field=config.tag_field or 'type', + array_like=config.array_like, + forbid_unknown_fields=True, + ) + wire_decoder = msgspec.json.Decoder(type=wire) + raw_decoder = msgspec.json.Decoder(type=list[msgspec.Raw]) + any_decoder = msgspec.json.Decoder() + child_walk = compile_body_errors(child) + metadata = get_args(annotation)[1:] if get_origin(annotation) is Annotated else () + relaxed = Annotated[list[Any], *metadata] if metadata else None + unchanged = _type_guard([(cls, ('__post_init__',))]) + decode_piece = wire_decoder.decode + except (_Unsupported, TypeError, NameError, RecursionError): + return None + + def diagnose(raw: bytes, loc: tuple[str | int, ...] = ('body',)) -> list[dict[str, Any]]: + if not unchanged() or (not config.array_like and b'{' not in raw): + return full_walk(any_decoder.decode(raw), loc) + try: + pieces = raw_decoder.decode(raw) + except (msgspec.DecodeError, UnicodeError): + return full_walk(any_decoder.decode(raw), loc) + errors = [] + if relaxed is not None: + try: + msgspec.convert(pieces, type=relaxed) + except msgspec.ValidationError as exc: + errors.append(_error(loc, str(exc))) + for index, piece in enumerate(pieces): + try: + decode_piece(piece) + except (msgspec.DecodeError, UnicodeError): + # Validate complete failed/unknown pieces. Full fallback preserves + # original syntax-error text and first-invalid-value ordering. + try: + value = any_decoder.decode(piece) + except (msgspec.DecodeError, UnicodeError): + return full_walk(any_decoder.decode(raw), loc) + errors.extend(child_walk(value, loc + (index,))) + return errors + + return diagnose + + +def compile_json_body_errors(annotation: Any) -> Callable[..., list[dict[str, Any]]]: + """Use original JSON slices when safe, otherwise retain the structural traversal.""" + walk = compile_body_errors(annotation) + raw_walk = _compile_raw_list_errors(annotation, walk) + if raw_walk is not None: + return raw_walk + + def diagnose(raw: bytes, loc: tuple[str | int, ...] = ('body',)) -> list[dict[str, Any]]: + return walk(msgspec.json.decode(raw), loc) + + return diagnose diff --git a/src/msgspec_fastapi/defaults.py b/src/msgspec_fastapi/defaults.py new file mode 100644 index 0000000..7e6e6ec --- /dev/null +++ b/src/msgspec_fastapi/defaults.py @@ -0,0 +1,342 @@ +"""Check supported data before implicit copies.""" + +import copy +import copyreg +import dataclasses +import datetime +import decimal +import enum +import ipaddress +import pathlib +import sys +import types +import typing +import uuid +from copy import deepcopy as _stdlib_deepcopy +from typing import Any, get_args +from zoneinfo import ZoneInfo + +import msgspec +from fastapi.datastructures import _Unset + +from ._defaults import _Copier, _Guard +from .responses import _JSON_ENCODER +from .validation import _CLASS_DICT, _CLASS_MRO, _MODEL_BRANDS, _REQUIRED_DEFAULT + +_NONE_TYPE = type(None) +_ENUM_NAMESPACE = _CLASS_DICT(enum.Enum)['__dict__'] +_TRUSTED_UNION_REDUCER = getattr(copyreg, 'pickle_union', None) + +_ATOMIC_IDS = { + id(cls) + for cls in ( + type(None), + bool, + int, + float, + str, + bytes, + bytearray, + memoryview, + datetime.datetime, + datetime.date, + datetime.time, + datetime.timedelta, + datetime.timezone, + ZoneInfo, + decimal.Decimal, + uuid.UUID, + msgspec.Raw, + type(msgspec.UNSET), + pathlib.Path, + pathlib.PosixPath, + pathlib.WindowsPath, + pathlib.PurePath, + pathlib.PurePosixPath, + pathlib.PureWindowsPath, + ipaddress.IPv4Address, + ipaddress.IPv6Address, + ipaddress.IPv4Interface, + ipaddress.IPv6Interface, + ipaddress.IPv4Network, + ipaddress.IPv6Network, + ) +} + +_TYPE_REFERENCE_IDS = { + id(cls) + for cls in ( + types.GenericAlias, + types.UnionType, + type(typing.List[Any]), + type(typing.Union[int, str]), + type(typing.Annotated[int, 0]), + type(typing.Literal[0]), + type(typing.List), + ) +} + + +_MISSING_STATE_METHOD = object() +_STATE_NAMES = ( + '__getstate__', + '__setstate__', + '__reduce__', + '__reduce_ex__', + '__getnewargs__', + '__getnewargs_ex__', + '__getattribute__', + '__getattr__', +) +_STATE_KEYS = frozenset(_STATE_NAMES) + +_NATIVE_STATE_METHODS = tuple( + ( + name, + tuple( + mapping[name] + for root in (msgspec.Struct, enum.Enum, int, str) + for mapping in map(_CLASS_DICT, _CLASS_MRO(root)) + if name in mapping + ) + + ((_MISSING_STATE_METHOD,)) + + ( + (getattr(dataclasses, '_dataclass_getstate'),) + if name == '__getstate__' + else (getattr(dataclasses, '_dataclass_setstate'),) + if name == '__setstate__' + else () + ), + ) + for name in _STATE_NAMES +) + + +def _checked_namespace(item: Any, cls: type[Any], namespace: Any) -> dict[str, Any]: + state = namespace.__get__(item, cls) + if type(state) is not dict or any(type(key) is not str for key in state): + raise TypeError('Unsupported instance state in copied data') + if not _STATE_KEYS.isdisjoint(state): + raise TypeError('Instance state methods are not supported in copied data') + copier = state.get('__deepcopy__', _MISSING_STATE_METHOD) + if copier is not _MISSING_STATE_METHOD and type(copier) is not types.FunctionType: + raise TypeError('Instance copy methods must be ordinary Python functions') + return state + + +def _check_copy_graph_python(value: Any) -> None: + pending = [value] + seen: set[int] = set() + classes: dict[int, tuple[str, Any, tuple[Any, ...]]] = {} + reducer_ids = { + id(cls) + for cls, reducer in copyreg.dispatch_table.items() + if not (cls is types.UnionType and reducer is _TRUSTED_UNION_REDUCER) + } + while pending: + item = pending.pop() + cls = type(item) + if ( + cls is str + or cls is int + or cls is bool + or cls is float + or cls is bytes + or cls is _NONE_TYPE + ): + continue + oid = id(item) + if oid in seen: + continue + seen.add(oid) + if cls is list or cls is tuple or cls is set or cls is frozenset: + if (cls is set or cls is frozenset) and id(cls) in reducer_ids: + raise TypeError('Registered copy reducers are not supported in copied data') + pending.extend(item) + continue + if cls is dict: + for key, child in item.items(): + pending.append(key) + pending.append(child) + continue + cid = id(cls) + info = classes.get(cid) + if info is None: + if ( + issubclass(cls, type) + or cls is types.FunctionType + or cls is types.BuiltinFunctionType + ): + continue + if cid in _TYPE_REFERENCE_IDS: + if cid in reducer_ids: + raise TypeError('Registered copy reducers are not supported in copied data') + pending.extend(get_args(item)) + continue + info = _copy_class_info(cls) + classes[cid] = info + kind, namespace, slots = info + if cls is datetime.datetime or cls is datetime.time: + pending.append(item.tzinfo) + elif cls is datetime.timezone: + pending.extend((item.utcoffset(None), item.tzname(None))) + elif cls is ZoneInfo: + pending.append(item.key) + if namespace is not None: + state = _checked_namespace(item, cls, namespace) + if kind == 'state': + pending.extend(state.values()) + if cid in reducer_ids and getattr(item, '__deepcopy__', None) is None: + raise TypeError('Registered copy reducers are not supported in copied data') + if kind == 'state': + for descriptor in slots: + try: + child = descriptor.__get__(item, cls) + except AttributeError: + continue + child_cls = type(child) + if ( + child_cls is str + or child_cls is int + or child_cls is bool + or child_cls is float + or child_cls is bytes + or child_cls is _NONE_TYPE + ): + continue + pending.append(child) + + +_NATIVE_GUARD = _Guard( + fallback=_check_copy_graph_python, + registry=copyreg.dispatch_table, + trusted_union_reducer=_TRUSTED_UNION_REDUCER, + union_type=types.UnionType, + function_type=types.FunctionType, + builtin_function_type=types.BuiltinFunctionType, + getset_descriptor_type=types.GetSetDescriptorType, + member_descriptor_type=types.MemberDescriptorType, + method_descriptor_type=types.MethodDescriptorType, + wrapper_descriptor_type=types.WrapperDescriptorType, + struct_type=msgspec.Struct, + enum_type=enum.Enum, + atomic_ids=_ATOMIC_IDS, + reference_ids=_TYPE_REFERENCE_IDS, + model_brands=_MODEL_BRANDS, + native_state_methods=_NATIVE_STATE_METHODS, + state_names=_STATE_NAMES, + enum_namespace=_ENUM_NAMESPACE, + missing=_MISSING_STATE_METHOD, + mro_descriptor=type.__dict__['__mro__'], + dict_descriptor=type.__dict__['__dict__'], + name_descriptor=type.__dict__['__name__'], +) + + +deepcopy = _Copier( + copy_module=copy, + deepcopy=_stdlib_deepcopy, + list_copier=getattr(copy, '_deepcopy_list'), + dict_copier=getattr(copy, '_deepcopy_dict'), + tuple_copier=getattr(copy, '_deepcopy_tuple'), + atomic_copier=getattr(copy, '_deepcopy_atomic', None), + reconstruct=getattr(copy, '_reconstruct'), + atomic_first=sys.version_info >= (3, 14), + memo_sentinel=getattr(_stdlib_deepcopy, '__defaults__')[-1], +) + + +_copy_class_info = _NATIVE_GUARD.class_info +_check_copy_graph = _NATIVE_GUARD.check + + +def _immutable_value(value: Any) -> bool: + cls = type(value) + return ( + cls is _NONE_TYPE or cls is bool or cls is int or cls is float or cls is str or cls is bytes + ) + + +def check_body_default(value: Any) -> None: + if _immutable_value(value): + return + _check_copy_graph(value) + _JSON_ENCODER.encode(value) + + +def copy_body_default(value: Any) -> Any: + if _immutable_value(value): + return value + _check_copy_graph(value) + _JSON_ENCODER.encode(value) + result = deepcopy(value) + # A user-defined copy hook can return a different type; do not pass a branded + # result into FastAPI's subsequent copy of a field default. + _check_copy_graph(result) + return result + + +def copy_metadata(value: Any) -> Any: + _check_copy_graph(value) + result = deepcopy(value) + _check_copy_graph(result) + return result + + +_FIELD_METADATA = ( + 'title', + 'description', + 'examples', + 'example', + 'openapi_examples', + 'json_schema_extra', + 'deprecated', +) + + +def check_field_metadata(info: Any) -> None: + for name in _FIELD_METADATA: + value = getattr(info, name, None) + if name == 'json_schema_extra' and callable(value): + continue + if value is not None and value is not _Unset: + _check_copy_graph(value) + + +def copy_field_metadata(info: Any) -> None: + for name in _FIELD_METADATA: + value = getattr(info, name, None) + if name == 'json_schema_extra' and callable(value): + continue + if value is not None and value is not _Unset: + setattr(info, name, copy_metadata(value)) + + +class _FieldDefault: + __slots__ = ('info',) + + def __init__(self, info: Any) -> None: + self.info = info + + def __call__(self) -> Any: + info = self.info + if info.default_factory is not None: + value = info.default_factory() + check_body_default(value) + return value + if info.default is _REQUIRED_DEFAULT: + return _REQUIRED_DEFAULT + return copy_body_default(info.default) + + def __copy__(self) -> '_FieldDefault': + return self + + def __deepcopy__(self, memo: dict[int, Any]) -> '_FieldDefault': + return self + + +def guard_field_default(field: Any) -> None: + info = field.field_info + if info.default is not _REQUIRED_DEFAULT: + check_body_default(info.default) + field.get_default = _FieldDefault(info) diff --git a/src/msgspec_fastapi/responses.py b/src/msgspec_fastapi/responses.py index b9db53e..6303cc9 100644 --- a/src/msgspec_fastapi/responses.py +++ b/src/msgspec_fastapi/responses.py @@ -1,9 +1,36 @@ +from ipaddress import ( + IPv4Address, + IPv4Interface, + IPv4Network, + IPv6Address, + IPv6Interface, + IPv6Network, +) +from pathlib import PurePath from typing import Any import msgspec from starlette.responses import JSONResponse -_JSON_ENCODER = msgspec.json.Encoder() + +def _encode_hook(value: Any) -> str: + if isinstance( + value, + ( + PurePath, + IPv4Address, + IPv6Address, + IPv4Interface, + IPv6Interface, + IPv4Network, + IPv6Network, + ), + ): + return str(value) + raise TypeError(f'Encoding objects of type {type(value).__name__} is not supported') + + +_JSON_ENCODER = msgspec.json.Encoder(enc_hook=_encode_hook) class MsgspecJSONResponse(JSONResponse): diff --git a/src/msgspec_fastapi/router.py b/src/msgspec_fastapi/router.py index 5fb936b..07312c6 100644 --- a/src/msgspec_fastapi/router.py +++ b/src/msgspec_fastapi/router.py @@ -1,39 +1,58 @@ from __future__ import annotations import inspect -from collections.abc import Callable, Coroutine -from copy import deepcopy +from collections.abc import AsyncIterator, Callable, Coroutine, Iterator, MutableMapping from functools import partial, wraps -from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints +from typing import Annotated, Any, NamedTuple, cast, get_args, get_origin, get_type_hints +from weakref import WeakValueDictionary +import anyio import msgspec from fastapi import APIRouter, FastAPI, HTTPException, Request, Response -from fastapi._compat import ModelField from fastapi.datastructures import DefaultPlaceholder, _Unset from fastapi.dependencies.models import Dependant +from fastapi.dependencies.utils import get_stream_item_type +from fastapi.exception_handlers import request_validation_exception_handler from fastapi.exceptions import RequestValidationError, ResponseValidationError from fastapi.openapi.utils import validation_error_definition, validation_error_response_definition from fastapi.params import Body, Depends, Form, Param -from fastapi.routing import APIRoute, _effective_route_context_var, iter_route_contexts +from fastapi.routing import ( + APIRoute, + _effective_route_context_var, + get_request_handler, + iter_route_contexts, +) +from fastapi.sse import EventSourceResponse, ServerSentEvent from fastapi.utils import deep_dict_update -from pydantic import BaseModel -from starlette.concurrency import run_in_threadpool +from starlette.concurrency import iterate_in_threadpool, run_in_threadpool +from starlette.responses import JSONResponse from starlette.routing import compile_path -from .responses import MsgspecJSONResponse +from .body_errors import body_errors, compile_json_body_errors +from .defaults import check_body_default, check_field_metadata, copy_body_default, copy_metadata +from .responses import _JSON_ENCODER, MsgspecJSONResponse, _encode_hook from .validation import ( + _assert_unbranded, base_type, constrained_type, + ensure_native_type, + pattern_validator, + patterns_need_raw, prepare_parameters, structured_type, - uses_pydantic, ) -def _untyped_encode_hook(value: Any) -> Any: - if isinstance(value, BaseModel): - return value.model_dump(mode='json', by_alias=True) - raise TypeError(f'Encoding objects of type {type(value).__name__} is unsupported') +class _BodyConfig(NamedTuple): + decoder: msgspec.json.Decoder[Any] + default: Any + alias: str | None + check_patterns: Callable[..., list[dict[str, Any]]] | None + pattern_needs_raw: bool + diagnose: Callable[..., list[dict[str, Any]]] | None + + +_BODY_UNDEFINED = inspect.signature(Body).parameters['default'].default def _contains_struct(annotation: Any) -> bool: @@ -63,20 +82,15 @@ def _merge_components(target: dict[str, Any], incoming: dict[str, Any]) -> None: if target[name] != schema: raise ValueError(f'Conflicting OpenAPI component: {name}') continue - target[name] = deepcopy(schema) + target[name] = copy_metadata(schema) def _json_type(annotation: Any) -> bool: - if annotation in (inspect.Signature.empty, None) or uses_pydantic(annotation): + if annotation in (inspect.Signature.empty, None): return False value = base_type(annotation) if isinstance(value, type) and issubclass(value, Response): return False - try: - msgspec.json.Decoder(type=annotation) - msgspec.json.schema(annotation) - except (TypeError, ValueError): - return False return True @@ -92,17 +106,17 @@ def _hints(endpoint: Callable[..., Any]) -> dict[str, Any]: def _parse_body( request: Request, raw: bytes, - config: tuple[msgspec.json.Decoder[Any], Any, bool], + config: _BodyConfig, strict_content_type: bool, ) -> Any: - decoder, default, embedded = config + decoder, default, body_alias, check_patterns, pattern_needs_raw, diagnose = config if not raw: if default is inspect.Signature.empty: # Raised after dependencies run, as in FastAPI. return RequestValidationError( [{'loc': ('body',), 'msg': 'Field required', 'type': 'missing'}] ) - return deepcopy(default) + return copy_body_default(default) content_type = request.headers.get('content-type') if content_type: media_type = content_type.split(';')[0].strip().lower() @@ -119,19 +133,34 @@ def _parse_body( except msgspec.ValidationError as exc: # Typed decoding can stop on a field error before discovering invalid syntax. try: - msgspec.json.decode(raw) + if diagnose is not None: + errors = diagnose(raw) + else: + errors = body_errors(msgspec.json.decode(raw), decoder.type) except (msgspec.DecodeError, UnicodeError) as syntax: raise HTTPException(400, f'Malformed JSON body: {syntax}') from syntax except RecursionError as nested: raise HTTPException(400, 'JSON body is nested too deeply') from nested return RequestValidationError( - [{'loc': ('body',), 'msg': str(exc), 'type': 'value_error.msgspec'}] + errors or [{'loc': ('body',), 'msg': str(exc), 'type': 'value_error.msgspec'}] ) except (msgspec.DecodeError, UnicodeError) as exc: raise HTTPException(400, f'Malformed JSON body: {exc}') from exc except RecursionError as exc: raise HTTPException(400, 'JSON body is nested too deeply') from exc - return value.value if embedded else value + if check_patterns is not None: + if pattern_needs_raw: + parsed = msgspec.json.decode(raw) + if body_alias is not None and body_alias not in parsed: + return value.value + pattern_value = parsed if body_alias is None else parsed[body_alias] + else: + pattern_value = value if body_alias is None else value.value + loc = ('body',) if body_alias is None else ('body', body_alias) + errors = check_patterns(pattern_value, loc) + if errors: + return RequestValidationError(errors) + return value.value if body_alias is not None else value def _revalidate_without_json(result: Any, output_type: Any, error: Exception) -> Any: @@ -159,31 +188,166 @@ def _revalidate_without_json(result: Any, output_type: Any, error: Exception) -> ) from error +async def _body_error_response(request: Request, exc: RequestValidationError) -> Response: + return MsgspecJSONResponse({'detail': exc.errors()}, status_code=422) + + +class _BodyErrorHandlers(MutableMapping[Any, Any]): + def __init__(self, handlers: MutableMapping[Any, Any]) -> None: + self.handlers = handlers + + def __getitem__(self, key: Any) -> Any: + handler = self.handlers[key] + if key is RequestValidationError and handler is request_validation_exception_handler: + return _body_error_response + return handler + + def __setitem__(self, key: Any, value: Any) -> None: + self.handlers[key] = value + + def __delitem__(self, key: Any) -> None: + del self.handlers[key] + + def __iter__(self) -> Iterator[Any]: + return iter(self.handlers) + + def __len__(self) -> int: + return len(self.handlers) + + def copy(self) -> dict[Any, Any]: + return dict(self) + + +def _check_stream_event(item: Any) -> None: + cls = type(item) + if cls is ServerSentEvent: + if item.raw_data is None and item.data is not None: + raise TypeError('Use ServerSentEvent(raw_data=...) for event data') + elif cls is not int and cls is not str and type.__subclasscheck__(ServerSentEvent, cls): + raise TypeError('ServerSentEvent subclasses are not supported') + + +class _StreamItemField: + def __init__(self, validate: Callable[..., Any], serialize_json: Callable[..., bytes]) -> None: + self.validate = validate + self.serialize_json = serialize_json + + class MsgspecRoute(APIRoute): + async def handle(self, scope: Any, receive: Any, send: Any) -> None: + original = scope.get('starlette.exception_handlers') + if ( + original is None + or original[0].get(RequestValidationError) is not request_validation_exception_handler + ): + await super().handle(scope, receive, send) + return + # Keep native exception handling outside dependency cleanup stacks. + installed = (_BodyErrorHandlers(original[0]), original[1]) + scope['starlette.exception_handlers'] = installed + try: + await super().handle(scope, receive, send) + finally: + if scope.get('starlette.exception_handlers') is installed: + scope['starlette.exception_handlers'] = original + def get_route_handler(self) -> Callable[[Request], Coroutine[Any, Any, Response]]: context = _effective_route_context_var.get() route = context if context is not None and context.original_route is self else self body_parameter = getattr(self.endpoint, '_msgspec_body_parameter', None) if body_parameter in compile_path(route.path)[2]: raise AssertionError('Path parameters must be scalar values') + if getattr(self.endpoint, '_msgspec_stream_mode', None) == 'jsonl': + route.is_json_stream = True + stream_callbacks = getattr(self.endpoint, '_msgspec_stream_callbacks', None) + + def build_native_handler() -> Callable[[Request], Coroutine[Any, Any, Response]]: + if stream_callbacks is None: + return super(MsgspecRoute, self).get_route_handler() + return get_request_handler( + dependant=route.dependant, + body_field=route.body_field, + status_code=route.status_code, + response_class=route.response_class, + response_field=route.response_field, + response_model_include=route.response_model_include, + response_model_exclude=route.response_model_exclude, + response_model_by_alias=route.response_model_by_alias, + response_model_exclude_unset=route.response_model_exclude_unset, + response_model_exclude_defaults=route.response_model_exclude_defaults, + response_model_exclude_none=route.response_model_exclude_none, + dependency_overrides_provider=route.dependency_overrides_provider, + embed_body_fields=route._embed_body_fields, + strict_content_type=route.strict_content_type, + stream_item_field=cast(Any, _StreamItemField(*stream_callbacks)), + is_json_stream=route.is_json_stream, + ) + prepare_parameters(route.dependant) - handler = super().get_route_handler() config = getattr(self.endpoint, '_msgspec_body_config', None) if config is None: - return handler - if route.body_field is not None: - raise TypeError('Other FastAPI body fields cannot be combined with a msgspec body') + pending = [route.dependant] + while pending: + dependant = pending.pop() + if any(not isinstance(field.field_info, Form) for field in dependant.body_params): + raise TypeError('JSON body fields must use the route msgspec decoder') + pending.extend(dependant.dependencies) + return build_native_handler() + stream_body = getattr(self.endpoint, '_msgspec_stream_body', None) + if stream_body is None: + if route.body_field is not None: + raise TypeError('Other FastAPI body fields cannot be combined with a msgspec body') + else: + name, decoder, check_patterns, diagnose_stream = stream_body + pending = [route.dependant] + fields: list[Any] = [] + while pending: + dependant = pending.pop() + fields.extend(dependant.body_params) + pending.extend(dependant.dependencies) + if len(fields) != 1 or fields[0].name != name: + raise TypeError('Other FastAPI body fields cannot be combined with a msgspec body') + + def validate_stream_body( + value: Any, values: Any = None, *, loc: tuple[str | int, ...] = () + ) -> tuple[Any, list[dict[str, Any]]]: + try: + encoded = msgspec.json.encode(value) + converted = decoder.decode(encoded) + except msgspec.ValidationError as exc: + errors = diagnose_stream(encoded, loc) + return None, errors or [ + {'loc': loc, 'msg': str(exc), 'type': 'value_error.msgspec'} + ] + errors = check_patterns(value, loc) if check_patterns is not None else [] + return (None, errors) if errors else (converted, []) + + native_field = fields[0] + if native_field.field_info.default is not _BODY_UNDEFINED: + check_body_default(native_field.field_info.default) + + def get_stream_default() -> Any: + return copy_body_default(native_field.field_info.default) + + setattr(native_field, 'get_default', get_stream_default) + setattr(native_field, 'validate', validate_stream_body) + # Only media/syntax checks precede dependencies; native body validation runs after them. + config = _BodyConfig(msgspec.json.Decoder(), None, None, None, True, None) strict_content_type = route.strict_content_type if isinstance(strict_content_type, DefaultPlaceholder): strict_content_type = strict_content_type.value + handler = build_native_handler() + async def handle(request: Request) -> Response: # A body that is not JSON is rejected before dependency side effects. + raw = await request.body() request.scope['_msgspec_body_value'] = _parse_body( - request, await request.body(), config, strict_content_type + request, raw, config, strict_content_type ) - return await handler(request) + response = await handler(request) + return response return handle @@ -241,7 +405,7 @@ def openapi() -> dict[str, Any]: targets: list[tuple[Any, dict[str, Any]]] = [] body_extras: dict[int, tuple[Callable[..., Any], dict[str, Any]]] = {} - def collect_fields(dependant: Dependant) -> list[ModelField]: + def collect_fields(dependant: Dependant) -> list[Any]: fields = [] for group in ('path_params', 'query_params', 'header_params', 'cookie_params'): fields.extend(getattr(dependant, group)) @@ -286,15 +450,24 @@ def collect(routes: Any, paths: dict[str, Any]) -> None: 'ValidationError': validation_error_definition, }, ) - if output_type is not None: - targets.append( - ( - output_type, - operation['responses'][str(output_code)]['content']['application/json'][ - 'schema' - ], + if ( + output_type is not None + and getattr(route.endpoint, '_msgspec_response_media_type', None) is not None + ): + stream_mode = getattr(route.endpoint, '_msgspec_stream_mode', None) + media_type = getattr(route.endpoint, '_msgspec_response_media_type', None) + content = operation['responses'][str(output_code)]['content'][media_type] + schema = content['itemSchema' if stream_mode else 'schema'] + if stream_mode == 'sse': + required = schema.setdefault('required', []) + if 'data' not in required: + required.append('data') + data = schema.setdefault('properties', {}).setdefault( + 'data', {'type': 'string'} ) - ) + data['contentMediaType'] = 'application/json' + schema = data.setdefault('contentSchema', {}) + targets.append((output_type, schema)) fields = collect_fields(route.dependant) for parameter in operation.get('parameters', []): for field in fields: @@ -348,7 +521,7 @@ def collect(routes: Any, paths: dict[str, Any]) -> None: } _merge_components(components, definitions) for (_, target), schema in zip(targets, schemas): - schema = deepcopy(schema) + schema = copy_metadata(schema) if extra := body_extras.get(id(target)): callback, metadata = extra schema.update(metadata) @@ -371,8 +544,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: kwargs.setdefault('default_response_class', MsgspecJSONResponse) if not issubclass(kwargs['route_class'], MsgspecRoute): raise TypeError('route_class must extend MsgspecRoute') + self._body_error_checks: WeakValueDictionary[Any, Callable[..., list[dict[str, Any]]]] = ( + WeakValueDictionary() + ) super().__init__(*args, **kwargs) + def _body_error_check(self, annotation: Any) -> Callable[..., list[dict[str, Any]]]: + check = self._body_error_checks.get(annotation) + if check is None: + check = compile_json_body_errors(annotation) + self._body_error_checks[annotation] = check + return check + def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) -> None: override = kwargs.get('route_class_override') if override is not None and not issubclass(override, MsgspecRoute): @@ -391,15 +574,56 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) if return_type is type(None): # As in FastAPI, `-> None` declares no response model. return_type = None + response_class = kwargs.get('response_class') + if response_class is None or isinstance(response_class, DefaultPlaceholder): + response_class = self.default_response_class + if isinstance(response_class, DefaultPlaceholder): + response_class = response_class.value + target_endpoint = ( + endpoint + if inspect.isfunction(endpoint) + or inspect.ismethod(endpoint) + or isinstance(endpoint, partial) + else getattr(endpoint, '__call__', endpoint) + ) + is_async_generator = inspect.isasyncgenfunction(target_endpoint) + is_generator = is_async_generator or inspect.isgeneratorfunction(target_endpoint) + stream_mode = None + stream_item = get_stream_item_type(return_type) if is_generator else None + if is_generator: + explicit_class = kwargs.get('response_class') + if issubclass(response_class, EventSourceResponse): + stream_mode = 'sse' + elif ( + explicit_class is None or isinstance(explicit_class, DefaultPlaceholder) + ) and response_class is MsgspecJSONResponse: + stream_mode = 'jsonl' + else: + stream_mode = 'raw' explicit = kwargs.get('response_model', inspect.Signature.empty) inferred = explicit is inspect.Signature.empty or isinstance(explicit, DefaultPlaceholder) output_type = return_type if inferred else explicit - validates_output = _json_type(output_type) + if is_generator and stream_mode != 'raw' and inferred: + output_type = stream_item if stream_item is not None else inspect.Signature.empty + if ( + is_generator + and stream_mode == 'sse' + and isinstance(stream_item, type) + and type.__subclasscheck__(ServerSentEvent, stream_item) + ): + if stream_item is not ServerSentEvent: + raise TypeError('ServerSentEvent subclasses are not supported') + if inferred: + output_type = inspect.Signature.empty + validates_output = stream_mode != 'raw' and _json_type(output_type) if validates_output: output_type, output_strict = constrained_type(output_type) + ensure_native_type(output_type) + msgspec.json.schema(output_type) if output_strict is False: raise TypeError('JSON msgspec responses use strict validation') output_decoder = msgspec.json.Decoder(type=output_type) if validates_output else None + output_patterns = pattern_validator(output_type) if validates_output else None # Like FastAPI with a Pydantic response_model at the top level: an instance of exactly # the declared Struct class is trusted and encoded once; other top-level values, # including subclass instances, are validated against the schema. Unlike Pydantic, @@ -414,25 +638,12 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) output_class, msgspec.Struct ) output_encoder = msgspec.json.Encoder() if validates_output else None - response_class = kwargs.get('response_class') - if response_class is None or isinstance(response_class, DefaultPlaceholder): - response_class = self.default_response_class - if isinstance(response_class, DefaultPlaceholder): - response_class = response_class.value - serializes_untyped_output = ( - output_type in (inspect.Signature.empty, None) - and not uses_pydantic(return_type) - and response_class is MsgspecJSONResponse - ) - handles_output = ( - validates_output - or serializes_untyped_output - or (explicit is None and _contains_struct(return_type)) + serializes_untyped_output = output_type in (inspect.Signature.empty, None) and ( + response_class is MsgspecJSONResponse or stream_mode == 'sse' ) + handles_output = stream_mode != 'raw' untyped_encoder = ( - msgspec.json.Encoder(enc_hook=_untyped_encode_hook) - if serializes_untyped_output - else None + msgspec.json.Encoder(enc_hook=_encode_hook) if serializes_untyped_output else None ) path_parameters = compile_path(path)[2] body = None @@ -446,11 +657,31 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) if _contains_struct(parameter.annotation): raise TypeError('msgspec Structs are not supported as Form or File parameters') continue + pending_types = [parameter.annotation] + while pending_types: + current = base_type(pending_types.pop()) + if isinstance(current, type): + _assert_unbranded(current) + elif get_origin(current) is not None: + pending_types.extend(get_args(current)) is_body = _contains_struct(parameter.annotation) or any( isinstance(x, Body) for x in metadata ) if _body_type(parameter.annotation) and not any(isinstance(x, Param) for x in metadata): is_body = True + if not is_body: + for item in metadata: + if isinstance(item, Param): + check_field_metadata(item) + parameter_default = parameter.default + if isinstance(parameter_default, Param): + parameter_default = parameter_default.default + if ( + parameter_default is not inspect.Signature.empty + and parameter_default is not _BODY_UNDEFINED + and parameter_default is not Ellipsis + ): + check_body_default(parameter_default) if is_body and _json_type(parameter.annotation): if parameter.name in path_parameters: raise AssertionError('Path parameters must be scalar values') @@ -465,7 +696,7 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) body = parameter body_meta = next((x for x in metadata if isinstance(x, Body)), None) - if body is None and not handles_output: + if body is None and not handles_output and (not is_generator or stream_mode == 'raw'): return super().add_api_route(path, endpoint, **kwargs) if any(p.kind in (p.VAR_KEYWORD, p.VAR_POSITIONAL) for p in parameters): raise TypeError('Variadic msgspec endpoints are not supported') @@ -484,20 +715,12 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) raise TypeError('msgspec responses use encoded field names') if validates_output and kwargs.get('status_code') in (204, 304): raise TypeError('A typed msgspec response requires a response body') - response_class = kwargs.get('response_class') - if ( - response_class is not None - and not isinstance(response_class, DefaultPlaceholder) - and response_class is not MsgspecJSONResponse - ): - raise TypeError( - 'Custom response_class is not supported for typed msgspec responses' - ) kwargs['response_model'] = None - kwargs['response_class'] = MsgspecJSONResponse + if not is_generator: + kwargs['response_class'] = response_class - responses = deepcopy(kwargs.get('responses') or {}) - openapi_extra = deepcopy(kwargs.get('openapi_extra') or {}) + responses = copy_metadata(kwargs.get('responses') or {}) + openapi_extra = copy_metadata(kwargs.get('openapi_extra') or {}) default = inspect.Signature.empty decoder = None embedded = False @@ -520,9 +743,7 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) raise TypeError('JSON msgspec bodies use strict validation') embedded = body_meta.embed is True default = body_meta.default - from pydantic_core import PydanticUndefined - - if default is PydanticUndefined or default is Ellipsis: + if default is _BODY_UNDEFINED or default is Ellipsis: default = inspect.Signature.empty if ( not isinstance(body.default, Body) @@ -533,6 +754,10 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) raise TypeError('Body default_factory is not supported') else: default = body.default + if default is not inspect.Signature.empty: + check_body_default(default) + ensure_native_type(body_type) + msgspec.json.schema(body_type) field_schema: dict[str, Any] = {} if embedded and default is not inspect.Signature.empty: field_schema['default'] = msgspec.to_builtins(default, str_keys=True) @@ -547,6 +772,8 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) body_meta.json_schema_extra ): raise TypeError('Body json_schema_extra must be a dict or callable') + if field_schema: + field_schema = copy_metadata(field_schema) decode_type = body_type if embedded: assert body_meta is not None @@ -558,7 +785,7 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) else ( 'value', body_type, - msgspec.field(default_factory=lambda: deepcopy(default)), + msgspec.field(default_factory=lambda: copy_body_default(default)), ) ) decode_type = msgspec.defstruct( @@ -579,9 +806,9 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) media: dict[str, Any] = {'schema': request_schema} if body_meta is not None and not embedded: if body_meta.openapi_examples: - media['examples'] = body_meta.openapi_examples + media['examples'] = copy_metadata(body_meta.openapi_examples) elif body_meta.example is not _Unset: - media['example'] = body_meta.example + media['example'] = copy_metadata(body_meta.example) request_body = { 'required': default is inspect.Signature.empty, 'content': {'application/json': media}, @@ -601,20 +828,47 @@ def add_api_route(self, path: str, endpoint: Callable[..., Any], **kwargs: Any) }, }, ) - if validates_output: + output_media = ( + 'application/jsonl' + if stream_mode == 'jsonl' + else 'text/event-stream' + if stream_mode == 'sse' + else response_class.media_type + ) + if validates_output and output_media is not None: code = kwargs.get('status_code') or 200 response_key = str(code) if str(code) in responses else code result_meta = responses.setdefault(response_key, {}) result_meta.setdefault( 'description', kwargs.get('response_description', 'Successful Response') ) - result_meta.setdefault('content', {}).setdefault('application/json', {}).setdefault( - 'schema', {} + media_type = output_media + result_meta.setdefault('content', {}).setdefault(media_type, {}).setdefault( + 'itemSchema' if is_generator else 'schema', {} ) kwargs['responses'] = responses kwargs['openapi_extra'] = openapi_extra remaining = [p for p in parameters if p is not body] + if is_generator and body is not None: + extraction = ( + Body( + default=body_meta.default, + alias=body_meta.alias, + validation_alias=body_meta.validation_alias, + serialization_alias=body_meta.serialization_alias, + alias_priority=body_meta.alias_priority, + embed=body_meta.embed, + media_type=body_meta.media_type, + include_in_schema=body_meta.include_in_schema, + ) + if body_meta is not None + else Body() + ) + replacement = body.replace(annotation=Annotated[Any, extraction]) + remaining = [ + replacement if parameter is body else parameter for parameter in parameters + ] def injected_name(type_: type[Request] | type[Response], fallback: str) -> tuple[str, bool]: for p in remaining: @@ -630,16 +884,34 @@ def injected_name(type_: type[Request] | type[Response], fallback: str) -> tuple return fallback, True request_name, remove_request = ( - injected_name(Request, '__msgspec_request') if body else (None, False) + injected_name(Request, '__msgspec_request') + if body and not is_generator + else (None, False) ) response_name, remove_response = ( - injected_name(Response, '__msgspec_response') if handles_output else (None, False) + injected_name(Response, '__msgspec_response') + if handles_output and not is_generator + else (None, False) ) is_async = inspect.iscoroutinefunction(endpoint) or inspect.iscoroutinefunction( getattr(endpoint, '__call__', None) ) + def validate_result(result: Any) -> Any: + if validates_output and not (trusts_instances and type(result) is output_class): + try: + result = cast(msgspec.json.Decoder[Any], output_decoder).decode( + cast(msgspec.json.Encoder, output_encoder).encode(result) + ) + except (TypeError, ValueError, OverflowError) as exc: + result = _revalidate_without_json(result, output_type, exc) + if output_patterns is not None: + errors = output_patterns(result, ('response',)) + if errors: + raise ResponseValidationError(errors) + return result + @wraps(endpoint) async def wrapper(**values: Any) -> Any: sub_response = cast(Response, values.get(cast(str, response_name))) @@ -661,16 +933,7 @@ async def wrapper(**values: Any) -> Any: result = await result if isinstance(result, Response): return result - if not handles_output: - return msgspec.to_builtins(result) if _contains_struct(return_type) else result - if validates_output and not (trusts_instances and type(result) is output_class): - try: - # Typed decoding validates fields and Meta, even for existing Structs. - result = cast(msgspec.json.Decoder[Any], output_decoder).decode( - cast(msgspec.json.Encoder, output_encoder).encode(result) - ) - except (TypeError, ValueError, OverflowError) as exc: - result = _revalidate_without_json(result, output_type, exc) + result = validate_result(result) status_code = sub_response.status_code or kwargs.get('status_code') or 200 if untyped_encoder is not None: response = Response( @@ -678,8 +941,17 @@ async def wrapper(**values: Any) -> Any: status_code=status_code, media_type='application/json', ) + elif issubclass(response_class, MsgspecJSONResponse): + response = response_class(result, status_code=status_code) + elif issubclass(response_class, JSONResponse): + response = response_class( + msgspec.json.decode(_JSON_ENCODER.encode(result)), + status_code=status_code, + ) else: - response = MsgspecJSONResponse(result, status_code=status_code) + response = response_class( + msgspec.to_builtins(result, str_keys=True), status_code=status_code + ) if response.status_code < 200 or response.status_code in (204, 205, 304): response.body = b'' if 'content-length' in response.headers: @@ -687,17 +959,66 @@ async def wrapper(**values: Any) -> Any: response.raw_headers.extend(sub_response.raw_headers) return response + stream_encoder = ( + msgspec.json.Encoder(enc_hook=_encode_hook) if is_generator and handles_output else None + ) + + def validate_stream_item( + value: Any, + values: dict[str, Any] | None = None, + *, + loc: tuple[str | int, ...] = (), + ) -> tuple[Any, list[dict[str, Any]]]: + return validate_result(value), [] + + def serialize_stream_item(value: Any, **options: Any) -> bytes: + encoded = cast(msgspec.json.Encoder, stream_encoder).encode(value) + # Raw must satisfy the same JSON decoding checks as other output. + msgspec.json.decode(encoded) + if b'\n' in encoded or b'\r' in encoded: + encoded = msgspec.json.format(encoded, indent=-1) + return encoded + + @wraps(endpoint) + async def stream_wrapper(**values: Any) -> AsyncIterator[Any]: + generator = endpoint(**values) + try: + items = generator if is_async_generator else iterate_in_threadpool(generator) + if stream_mode == 'sse': + async for item in items: + _check_stream_event(item) + yield item + else: + async for item in items: + yield item + finally: + with anyio.CancelScope(shield=True): + if is_async_generator: + await generator.aclose() + else: + await run_in_threadpool(generator.close) + + prepared_endpoint: Callable[..., Any] = stream_wrapper if is_generator else wrapper setattr( - wrapper, + prepared_endpoint, '__signature__', signature.replace( parameters=remaining, return_annotation=inspect.Signature.empty if handles_output else return_type, ), ) - setattr(wrapper, '_msgspec_prepared', True) + setattr(prepared_endpoint, '_msgspec_prepared', True) + setattr(prepared_endpoint, '_msgspec_response_media_type', output_media) + if is_generator: + setattr(prepared_endpoint, '_msgspec_stream_mode', stream_mode) + if handles_output and stream_mode in ('jsonl', 'sse'): + setattr( + prepared_endpoint, + '_msgspec_stream_callbacks', + (validate_stream_item, serialize_stream_item), + ) setattr( - wrapper, + prepared_endpoint, '_msgspec_openapi', ( schema_body_type, @@ -706,18 +1027,50 @@ async def wrapper(**values: Any) -> Any: ), ) if body is not None: - setattr(wrapper, '_msgspec_body_config', (decoder, default, embedded)) - setattr(wrapper, '_msgspec_body_parameter', body.name) + assert decoder is not None + pattern_needs_raw = patterns_need_raw(body_type) or ( + body_alias is not None and default is not inspect.Signature.empty + ) + setattr( + prepared_endpoint, + '_msgspec_body_config', + _BodyConfig( + decoder, + default, + body_alias, + pattern_validator(body_type, strings_validated=not pattern_needs_raw), + pattern_needs_raw, + self._body_error_check(decoder.type), + ), + ) + setattr(prepared_endpoint, '_msgspec_body_parameter', body.name) + if is_generator: + setattr( + prepared_endpoint, + '_msgspec_stream_body', + ( + body.name, + msgspec.json.Decoder(type=body_type), + pattern_validator(body_type), + self._body_error_check(body_type), + ), + ) if body_alias is not None: - setattr(wrapper, '_msgspec_body_alias', body_alias) + setattr(prepared_endpoint, '_msgspec_body_alias', body_alias) if callable(schema_callback): setattr( - wrapper, '_msgspec_body_schema_extra', (schema_callback, deepcopy(field_schema)) + prepared_endpoint, + '_msgspec_body_schema_extra', + (schema_callback, copy_metadata(field_schema)), ) route_count = len(self.routes) try: - result = super().add_api_route(path, wrapper, **kwargs) - if body is not None and cast(APIRoute, self.routes[-1]).body_field is not None: + result = super().add_api_route(path, prepared_endpoint, **kwargs) + if ( + body is not None + and not is_generator + and cast(APIRoute, self.routes[-1]).body_field is not None + ): raise TypeError('Other FastAPI body fields cannot be combined with a msgspec body') return result except Exception: diff --git a/src/msgspec_fastapi/validation.py b/src/msgspec_fastapi/validation.py index afb53f2..d0186a7 100644 --- a/src/msgspec_fastapi/validation.py +++ b/src/msgspec_fastapi/validation.py @@ -2,6 +2,7 @@ from __future__ import annotations +import inspect import re from collections.abc import Callable, Iterable from dataclasses import is_dataclass @@ -11,15 +12,146 @@ import annotated_types import msgspec from fastapi.dependencies.models import Dependant -from pydantic import BaseModel -from pydantic.fields import FieldInfo +from fastapi.params import Body, Param from typing_extensions import is_typeddict +_REQUIRED_DEFAULT = inspect.signature(Body).parameters['default'].default + + +class _Pattern: + def __init__(self, pattern: str | re.Pattern[str]) -> None: + if not isinstance(pattern, str): + raise TypeError('Use a string pattern; compiled Python patterns are not supported') + from regex_rs import Regex + + self.pattern = pattern + self.regex = Regex(pattern) + + +def pattern_validator( + annotation: Any, *, strings_validated: bool = False +) -> Callable[..., list[dict[str, Any]]] | None: + """Compile checks for translated FastAPI patterns, preserving raw container positions.""" + metadata = get_args(annotation)[1:] if get_origin(annotation) is Annotated else () + patterns = [item for item in metadata if isinstance(item, _Pattern)] + annotation = base_type(annotation) + origin, args = get_origin(annotation), get_args(annotation) + children = [] + if origin in (list, set, frozenset, tuple, dict, Union, UnionType): + children = [ + None if arg is Ellipsis else pattern_validator(arg, strings_validated=strings_validated) + for arg in args + ] + if not patterns and not any(children): + return None + if ( + strings_validated + and not patterns + and (origin is list or (origin is tuple and len(args) == 2 and args[1] is Ellipsis)) + ): + child = args[0] + child_metadata = get_args(child)[1:] if get_origin(child) is Annotated else () + child_patterns = [item for item in child_metadata if isinstance(item, _Pattern)] + if base_type(child) is str and len(child_patterns) == 1: + pattern = child_patterns[0] + matches = pattern.regex.is_match + message = f"String should match pattern '{pattern.pattern}'" + + def check_strings(value: Any, loc: tuple[str | int, ...] = ()) -> list[dict[str, Any]]: + return [ + { + 'loc': loc + (index,), + 'msg': message, + 'type': 'string_pattern_mismatch', + 'ctx': {'pattern': pattern.pattern}, + } + for index, matched in enumerate(map(matches, value)) + if not matched + ] + + return check_strings + + def check(value: Any, loc: tuple[str | int, ...] = ()) -> list[dict[str, Any]]: + errors: list[dict[str, Any]] = [] + for pattern in patterns: + if not isinstance(value, str): + errors.append( + {'loc': loc, 'msg': 'Input should be a valid string', 'type': 'string_type'} + ) + elif not pattern.regex.is_match(value): + errors.append( + { + 'loc': loc, + 'msg': f"String should match pattern '{pattern.pattern}'", + 'type': 'string_pattern_mismatch', + 'ctx': {'pattern': pattern.pattern}, + } + ) + if origin in (list, set, frozenset, tuple) and isinstance( + value, (list, tuple, set, frozenset) + ): + for index, item in enumerate(value): + child = ( + children[index] + if origin is tuple and args[-1] is not Ellipsis and index < len(children) + else children[0] + ) + if child is not None: + errors.extend(child(item, loc + (index,))) + elif origin is dict and isinstance(value, dict): + key_check, value_check = children + for key, item in value.items(): + if key_check is not None: + errors.extend(key_check(key, loc + (key, '[key]'))) + if value_check is not None: + errors.extend(value_check(item, loc + (key,))) + elif origin in (Union, UnionType) and Any not in args: + branch_errors: list[dict[str, Any]] = [] + for arg, child in zip(args, children): + target = get_origin(base_type(arg)) or base_type(arg) + if target is Literal: + try: + msgspec.convert(value, type=arg) + except msgspec.ValidationError: + continue + break + if not isinstance(target, type) or not isinstance(value, target): + continue + # Check scalar constraints without constructing models or repeating hooks. + if target in (str, int, float, bool, bytes): + try: + msgspec.convert(value, type=arg) + except msgspec.ValidationError: + continue + current = child(value, loc) if child is not None else [] + if not current: + break + if not branch_errors: + branch_errors = current + else: + errors.extend(branch_errors) + return errors + + return check + def base_type(annotation: Any) -> Any: return get_args(annotation)[0] if get_origin(annotation) is Annotated else annotation +def patterns_need_raw(annotation: Any) -> bool: + """Keep raw positions except for strict string, list and tuple trees.""" + metadata = get_args(annotation)[1:] if get_origin(annotation) is Annotated else () + target = base_type(annotation) + if target is str: + return False + if get_origin(target) in (list, tuple) and not any( + isinstance(item, _Pattern) for item in metadata + ): + return any(patterns_need_raw(arg) for arg in get_args(target) if arg is not Ellipsis) + return True + + def structured_type(annotation: Any) -> bool: value = get_origin(base_type(annotation)) or base_type(annotation) return isinstance(value, type) and ( @@ -30,13 +162,6 @@ def structured_type(annotation: Any) -> bool: ) -def uses_pydantic(annotation: Any) -> bool: - value = base_type(annotation) - return (isinstance(value, type) and issubclass(value, BaseModel)) or any( - uses_pydantic(item) for item in get_args(value) - ) - - def _check_structured_metadata(annotation: Any, seen: set[Any] | None = None) -> None: if seen is None: seen = set() @@ -46,6 +171,8 @@ def _check_structured_metadata(annotation: Any, seen: set[Any] | None = None) -> raise TypeError('Use msgspec.Meta inside structured model fields') origin = get_origin(annotation) value = origin or annotation + if isinstance(value, type): + _assert_unbranded(value) structured = structured_type(annotation) if structured and value not in seen: seen.add(value) @@ -61,6 +188,20 @@ def constrained_type(annotation: Any, metadata: Iterable[Any] = ()) -> tuple[Any annotation, *extras = get_args(annotation) metadata = (*extras, *metadata) origin, args = get_origin(annotation), get_args(annotation) + if isinstance(origin or annotation, type): + _assert_unbranded(origin or annotation) + if ( + (type(metadata) is tuple or type(metadata) is list) + and not metadata + and ( + annotation is int + or annotation is str + or annotation is float + or annotation is bool + or annotation is bytes + ) + ): + return annotation, None if structured_type(annotation): _check_structured_metadata(annotation) if args and origin is not Literal: @@ -84,7 +225,46 @@ def constrained_type(annotation: Any, metadata: Iterable[Any] = ()) -> tuple[Any annotation = origin[converted_args[0] if len(converted_args) == 1 else converted_args] expanded = [] for item in metadata: - if isinstance(item, FieldInfo): + if isinstance(item, (Param, Body)): + expanded.extend(item.metadata) + elif type(item).__module__ == 'pydantic.fields' and type(item).__name__ == 'FieldInfo': + # Nested Field supports constraint metadata only, not model field options. + if item.default is not _REQUIRED_DEFAULT or item.default_factory is not None: + raise TypeError('Nested Field defaults are not supported') + for name in ( + 'alias', + 'alias_priority', + 'validation_alias', + 'serialization_alias', + 'annotation', + 'discriminator', + 'exclude', + 'exclude_if', + 'frozen', + 'validate_default', + 'init', + 'init_var', + 'kw_only', + 'title', + 'field_title_generator', + 'description', + 'examples', + 'deprecated', + 'json_schema_extra', + ): + if getattr(item, name, None) is not None: + raise TypeError(f'Unsupported nested Field option: {name}') + if item.repr is not True: + raise TypeError('Unsupported nested Field option: repr') + attributes = item._attributes_set + if type(attributes) is not dict or any(type(key) is not str for key in attributes): + raise TypeError('Unsupported nested Field attributes') + for name, value in attributes.items(): + if name == 'default' and value is _REQUIRED_DEFAULT: + continue + raise TypeError(f'Unsupported nested Field option: {name}') + if type(item.metadata) is not list: + raise TypeError('Unsupported nested Field metadata') expanded.extend(item.metadata) else: expanded.append(item) @@ -93,7 +273,7 @@ def constrained_type(annotation: Any, metadata: Iterable[Any] = ()) -> tuple[Any metas = [] strict = None for item in metadata: - if isinstance(item, msgspec.Meta): + if isinstance(item, (msgspec.Meta, _Pattern)): metas.append(item) elif isinstance( item, @@ -112,18 +292,26 @@ def constrained_type(annotation: Any, metadata: Iterable[Any] = ()) -> tuple[Any {'MultipleOf': 'multiple_of'}.get(type(item).__name__, type(item).__name__.lower()), ) constraints[name] = getattr(item, name) - elif type(item).__module__.startswith('pydantic') and type(item).__name__ == 'Strict': + elif type(item).__module__ == 'pydantic.types' and type(item).__name__ == 'Strict': + if type(item.strict) is not bool: + raise TypeError('Strict metadata must contain a bool') strict = item.strict elif ( - type(item).__module__.startswith('pydantic') + type(item).__module__ == 'pydantic._internal._fields' and type(item).__name__ == '_PydanticGeneralMetadata' ): - for name, value in vars(item).items(): + values = vars(item) + if type(values) is not dict or any(type(key) is not str for key in values): + raise TypeError('Unsupported constraint metadata') + for name, value in values.items(): if name not in ('pattern',): raise TypeError(f'Unsupported msgspec parameter constraint: {name}') constraints[name] = value else: raise TypeError(f'Unsupported msgspec parameter metadata: {type(item).__name__}') + if 'pattern' in constraints: + pattern = _Pattern(constraints.pop('pattern')) + metas.extend((pattern, msgspec.Meta(extra_json_schema={'pattern': pattern.pattern}))) if constraints: metas.append(msgspec.Meta(**constraints)) if metas: @@ -139,6 +327,114 @@ def constrained_type(annotation: Any, metadata: Iterable[Any] = ()) -> tuple[Any return target, strict +_CLASS_MRO = type.__dict__['__mro__'].__get__ +_CLASS_DICT = type.__dict__['__dict__'].__get__ +_MODEL_BRANDS = ( + '__pydantic_validator__', + '__pydantic_serializer__', + '__get_pydantic_core_schema__', + '__pydantic_config__', + '__pydantic_model__', + '__get_validators__', +) + + +def _assert_unbranded(cls: Any) -> None: + if cls is int or cls is str or cls is float or cls is bool or cls is bytes: + return + if not isinstance(cls, type): + cls = get_origin(cls) + if not isinstance(cls, type): + raise TypeError('Unsupported model class') + for base in _CLASS_MRO(cls): + namespace = _CLASS_DICT(base) + try: + for name in _MODEL_BRANDS: + if name in namespace: + raise TypeError('Pydantic model behavior is not supported for msgspec data') + except StopIteration as exc: + raise RuntimeError('generator raised StopIteration') from exc + + +_LEAVES = ( + msgspec.inspect.AnyType, + msgspec.inspect.NoneType, + msgspec.inspect.BoolType, + msgspec.inspect.IntType, + msgspec.inspect.FloatType, + msgspec.inspect.StrType, + msgspec.inspect.BytesType, + msgspec.inspect.ByteArrayType, + msgspec.inspect.MemoryViewType, + msgspec.inspect.DateTimeType, + msgspec.inspect.TimeType, + msgspec.inspect.DateType, + msgspec.inspect.TimeDeltaType, + msgspec.inspect.UUIDType, + msgspec.inspect.DecimalType, + msgspec.inspect.ExtType, + msgspec.inspect.RawType, + msgspec.inspect.EnumType, + msgspec.inspect.LiteralType, +) + + +def ensure_native_type(annotation: Any) -> None: + if ( + annotation is int + or annotation is str + or annotation is float + or annotation is bool + or annotation is bytes + ): + return + direct = get_args(annotation)[0] if get_origin(annotation) is Annotated else annotation + if isinstance(direct, type): + _assert_unbranded(direct) + pending = [msgspec.inspect.type_info(annotation)] + seen: set[int] = set() + while pending: + node = pending.pop() + if id(node) in seen: + continue + seen.add(id(node)) + if isinstance(node, msgspec.inspect.CustomType): + raise TypeError('Custom decoding hooks are not supported for typed msgspec data') + if isinstance( + node, + ( + msgspec.inspect.StructType, + msgspec.inspect.DataclassType, + msgspec.inspect.TypedDictType, + msgspec.inspect.NamedTupleType, + msgspec.inspect.EnumType, + ), + ): + _assert_unbranded(node.cls) + if isinstance(node, msgspec.inspect.Metadata): + pending.append(node.type) + elif isinstance(node, msgspec.inspect.CollectionType): + pending.append(node.item_type) + elif isinstance(node, msgspec.inspect.TupleType): + pending.extend(node.item_types) + elif isinstance(node, msgspec.inspect.UnionType): + pending.extend(node.types) + elif isinstance(node, (msgspec.inspect.DictType, msgspec.inspect.FrozenDictType)): + pending.extend((node.key_type, node.value_type)) + elif isinstance( + node, + ( + msgspec.inspect.StructType, + msgspec.inspect.DataclassType, + msgspec.inspect.TypedDictType, + msgspec.inspect.NamedTupleType, + ), + ): + pending.extend(field.type for field in node.fields) + elif not isinstance(node, _LEAVES): + raise TypeError('Unsupported msgspec type information') + + # Unicode White_Space used by Rust str::trim, excluding Python's extra C0 separators. _HTTP_WHITESPACE = '\t\n\v\f\r \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000' _HTTP_INTEGER = re.compile(r'-?(?:0|[1-9][0-9]*)\Z') @@ -150,7 +446,10 @@ def _http_int(value: Any) -> Any: return value # Preserve the direct parser's size/error behavior before its cleanup fallback. if _HTTP_INTEGER.fullmatch(value): - return int(value) + try: + return int(value) + except ValueError as exc: + raise ValueError('Invalid integer parameter') from exc number = value.strip(_HTTP_WHITESPACE) if number.startswith('+'): number = number[1:] @@ -192,7 +491,10 @@ def _http_int(value: Any) -> Any: number = '-' + number if not _HTTP_INTEGER.fullmatch(number): raise ValueError('Invalid integer parameter') - return int(number) + try: + return int(number) + except ValueError as exc: + raise ValueError('Invalid integer parameter') from exc def _http_normalizer(annotation: Any) -> Callable[[Any], Any] | None: @@ -252,6 +554,7 @@ def _parameter_validator( ) -> Callable[..., tuple[Any, list[dict[str, Any]]]]: normalizer = None if strict else _http_normalizer(target) convert = msgspec.convert + check_patterns = pattern_validator(target) def validate( value: Any, values: dict[str, Any] | None = None, *, loc: tuple[str | int, ...] = () @@ -259,7 +562,9 @@ def validate( try: if normalizer is not None: value = normalizer(value) - return convert(value, type=target, strict=strict), [] + converted = convert(value, type=target, strict=strict) + errors = check_patterns(value, loc) if check_patterns is not None else [] + return (None, errors) if errors else (converted, []) except (ValueError, UnicodeError, OverflowError) as exc: return None, [{'loc': loc, 'msg': str(exc), 'type': 'value_error.msgspec'}] @@ -268,16 +573,19 @@ def validate( def prepare_parameters(dependant: Dependant) -> None: """Keep FastAPI extraction/aliases/defaults; replace supported validators.""" + from .defaults import copy_field_metadata, guard_field_default + for group in ('path_params', 'query_params', 'header_params', 'cookie_params'): for field in getattr(dependant, group): - if uses_pydantic(field.field_info.annotation): - continue if getattr(field, '_msgspec_parameter', False): continue + guard_field_default(field) + copy_field_metadata(field.field_info) target, strict = constrained_type( field.field_info.annotation, field.field_info.metadata ) strict = bool(strict) + ensure_native_type(target) msgspec.json.schema(target) field.validate = _parameter_validator(target, strict) diff --git a/tests/test_body_default_ownership.py b/tests/test_body_default_ownership.py new file mode 100644 index 0000000..be448c3 --- /dev/null +++ b/tests/test_body_default_ownership.py @@ -0,0 +1,169 @@ +"""Default data is checked before framework or route copies.""" + +import dataclasses +import sys +import threading +from collections.abc import AsyncIterator +from typing import Annotated, Any + +import pytest +from fastapi import Body, FastAPI +from fastapi.sse import EventSourceResponse +from fastapi.testclient import TestClient +from pydantic import BaseModel, ConfigDict +from pydantic.dataclasses import dataclass as validated_dataclass + +from msgspec_fastapi import MsgspecRouter + + +class External(BaseModel): + value: int + + +@validated_dataclass(config=ConfigDict(validate_assignment=True), slots=True) +class ExternalSlots: + value: int + + +def application(default: Any, mode: str, embedded: bool) -> FastAPI: + annotation = Annotated[Any, Body(embed=embedded, alias='payload')] + router = MsgspecRouter() + if mode == 'ordinary': + + async def endpoint(value: annotation = default) -> int: + return value.value if hasattr(value, 'value') else len(value['items']) + else: + + async def endpoint(value: annotation = default) -> AsyncIterator[int]: + yield value.value if hasattr(value, 'value') else len(value['items']) + + options = {'response_class': EventSourceResponse} if mode == 'sse' else {} + router.add_api_route('/items', endpoint, methods=['POST'], **options) + app = FastAPI() + app.include_router(router) + return app + + +def record_model_calls(frame: Any, event: str, arg: Any, calls: list[str]) -> None: + if event == 'call' and ( + frame.f_code.co_filename.endswith('/pydantic/main.py') + and frame.f_code.co_name in ('__deepcopy__', 'model_dump', 'model_dump_json') + or frame.f_code.co_name in ('_dataclass_getstate', '_dataclass_setstate') + ): + calls.append(frame.f_code.co_name) + + +@pytest.mark.parametrize('mode', ['ordinary', 'jsonl', 'sse']) +@pytest.mark.parametrize('embedded', [False, True]) +@pytest.mark.parametrize('model', [External, ExternalSlots]) +@pytest.mark.parametrize('nested', [False, True]) +def test_branded_defaults_are_rejected_before_model_copy( + mode: str, embedded: bool, model: Any, nested: bool +) -> None: + value = model(value=1) + default = {'items': [value]} if nested else value + calls = [] + previous = sys.getprofile() + previous_thread = threading.getprofile() + try: + + def hook(frame: Any, event: str, arg: Any) -> None: + record_model_calls(frame, event, arg, calls) + + sys.setprofile(hook) + threading.setprofile(hook) + with pytest.raises(TypeError): + application(default, mode, embedded) + finally: + sys.setprofile(previous) + threading.setprofile(previous_thread) + assert calls == [] + + +@pytest.mark.parametrize('mode', ['ordinary', 'jsonl', 'sse']) +@pytest.mark.parametrize('embedded', [False, True]) +@pytest.mark.parametrize('model', [External, ExternalSlots]) +def test_post_registration_default_mutation_is_checked_before_copy( + mode: str, embedded: bool, model: Any +) -> None: + default: Any = {'items': []} + app = application(default, mode, embedded) + default['items'].append(model(value=1)) + calls = [] + previous = sys.getprofile() + previous_thread = threading.getprofile() + try: + + def hook(frame: Any, event: str, arg: Any) -> None: + record_model_calls(frame, event, arg, calls) + + sys.setprofile(hook) + threading.setprofile(hook) + with pytest.raises(TypeError): + TestClient(app).post('/items', json={} if embedded else None) + finally: + sys.setprofile(previous) + threading.setprofile(previous_thread) + assert calls == [] + + +@pytest.mark.parametrize('mode', ['ordinary', 'jsonl', 'sse']) +@pytest.mark.parametrize('embedded', [False, True]) +def test_native_default_copy_counts_are_preserved(mode: str, embedded: bool) -> None: + calls = [] + + @dataclasses.dataclass + class Payload: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('copy') + return Payload(self.value + len(calls)) + + app = application(Payload(10), mode, embedded) + calls.clear() + result = TestClient(app).post('/items', json={} if embedded else None) + assert result.status_code == 200 + if mode == 'ordinary': + assert result.json() == 11 and calls == ['copy'] + else: + assert result.text == ('data: 13\n\n' if mode == 'sse' else '13\n') + assert calls == ['copy', 'copy'] + + +def test_dataclass_container_defaults_cannot_hide_branded_values() -> None: + @dataclasses.dataclass + class Mapping(dict): + pass + + value = Mapping() + value['items'] = [ExternalSlots(1)] + with pytest.raises(TypeError): + application(value, 'ordinary', False) + + +@pytest.mark.parametrize('slotted', [False, True]) +@pytest.mark.parametrize('mode', ['ordinary', 'jsonl', 'sse']) +def test_dataclass_instance_state_is_checked_beyond_json_fields(slotted: bool, mode: str) -> None: + @dataclasses.dataclass + class Payload: + value: int + + class SlottedPayload(Payload): + __slots__ = ('extra',) + + value = SlottedPayload(1) if slotted else Payload(1) + value.extra = External(value=1) + calls = [] + previous = sys.getprofile() + try: + + def hook(frame: Any, event: str, arg: Any) -> None: + record_model_calls(frame, event, arg, calls) + + sys.setprofile(hook) + with pytest.raises(TypeError): + application(value, mode, False) + finally: + sys.setprofile(previous) + assert calls == [] diff --git a/tests/test_body_errors.py b/tests/test_body_errors.py new file mode 100644 index 0000000..e9bb1cf --- /dev/null +++ b/tests/test_body_errors.py @@ -0,0 +1,345 @@ +"""Structural locations and safe diagnostics without repeating model hooks.""" + +import sys +from dataclasses import dataclass +from decimal import Decimal +from enum import Enum, IntEnum +from typing import Annotated, Any, Generic, NamedTuple, TypeVar + +import msgspec +import pytest +from fastapi import APIRouter, Body, FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel +from typing_extensions import TypedDict + +from msgspec_fastapi import MsgspecRouter, install_openapi + + +class Item(msgspec.Struct): + number: int + count: int + + +class PydItem(BaseModel): + number: int + count: int + + +@pytest.mark.parametrize('method', ['hash', 'equality', 'getattribute']) +def test_diagnostics_do_not_repeat_enum_set_callbacks(method: str) -> None: + calls = [] + checking = False + + class Hashed(str, Enum): + first = 'first' + + def __hash__(self) -> int: + calls.append('hash') + if len(calls) > 1: + raise RuntimeError('Hash callback repeated') + return hash(self.value) + + class Compared(IntEnum): + first = 0 + second = sys.hash_info.modulus + + def __eq__(self, other: Any) -> bool: + calls.append('equality') + if len(calls) > 1: + raise RuntimeError('Equality callback repeated') + return int.__eq__(self, other) + + __hash__ = int.__hash__ + + class LookedUp(Enum): + first = 'first' + + def __getattribute__(self, name: str) -> Any: + if checking and name == '_name_': + calls.append('getattribute') + if len(calls) > 1: + raise RuntimeError('Getter callback repeated') + return object.__getattribute__(self, name) + + target = {'hash': Hashed, 'equality': Compared, 'getattribute': LookedUp}[method] + + class Payload(msgspec.Struct): + choices: set[target] + count: int + + test_client = client(Payload) + checking = True + calls.clear() + values = [0, sys.hash_info.modulus] if method == 'equality' else ['first'] + response = test_client.post('/value', json={'choices': values, 'count': 'bad'}) + assert response.status_code == 422 + assert calls == [method] + assert response.json()['detail'][0]['loc'] == ['body'] + + +def client( + annotation: Any, router_class: type[APIRouter] = MsgspecRouter, *, embed: bool = False +) -> TestClient: + router = router_class() + + @router.post('/value') + async def endpoint(value: Annotated[annotation, Body(embed=embed, alias='payload')]) -> None: + pass + + app = FastAPI() + app.include_router(router) + if router_class is MsgspecRouter: + install_openapi(app) + return TestClient(app) + + +@pytest.mark.parametrize('router_class,model', [(APIRouter, PydItem), (MsgspecRouter, Item)]) +@pytest.mark.parametrize('embed', [False, True]) +def test_multiple_nested_errors_have_structured_locations( + router_class: type[APIRouter], model: Any, embed: bool +) -> None: + payload = [{'number': 'bad', 'count': 'bad'}, {'count': 'bad'}] + result = client(list[model], router_class, embed=embed).post( + '/value', json={'payload': payload} if embed else payload + ) + assert result.status_code == 422 + prefix = ['body', 'payload'] if embed else ['body'] + assert [e['loc'] for e in result.json()['detail']] == [ + prefix + [0, 'number'], + prefix + [0, 'count'], + prefix + [1, 'number'], + prefix + [1, 'count'], + ] + + +def test_encoded_alias_is_one_location_component() -> None: + class Aliased(msgspec.Struct): + value: int = msgspec.field(name='a.b[0]') + + result = client(list[Aliased]).post('/value', json=[{'a.b[0]': 'bad'}]) + assert result.status_code == 422 + assert result.json()['detail'][0]['loc'] == ['body', 0, 'a.b[0]'] + + +def test_raw_field_does_not_produce_a_false_validation_error() -> None: + class Payload(msgspec.Struct): + raw: msgspec.Raw + count: int + + result = client(Payload).post('/value', json={'raw': {'a': 1}, 'count': 'bad'}) + assert result.status_code == 422 + assert [e['loc'] for e in result.json()['detail']] == [['body', 'count']] + + +def test_model_post_init_is_not_repeated() -> None: + calls = [] + + class Child(msgspec.Struct): + number: int + + def __post_init__(self) -> None: + calls.append(self.number) + + class Parent(msgspec.Struct): + child: Child + count: int + + result = client(Parent).post('/value', json={'child': {'number': 1}, 'count': 'bad'}) + assert result.status_code == 422 and calls == [1] + assert result.json()['detail'][0]['loc'] == ['body'] + + +def test_generic_specialization_preserves_the_original_hook_failure() -> None: + T = TypeVar('T') + calls = [] + + class Box(msgspec.Struct, Generic[T]): + value: T + + class Checked(msgspec.Struct): + number: int + + def __post_init__(self) -> None: + calls.append(self.number) + if self.number < 0: + raise ValueError('Semantic invariant failed') + + class Parent(msgspec.Struct): + first: Box[int] + second: Box[Checked] + count: int + + result = client(Parent).post( + '/value', json={'first': {'value': 1}, 'second': {'value': {'number': -1}}, 'count': 'bad'} + ) + assert result.status_code == 422 and calls == [-1] + assert 'Semantic invariant failed' in result.json()['detail'][0]['msg'] + + +@pytest.mark.parametrize('alias', [False, True]) +def test_tuple_diagnostics_do_not_repeat_nested_hooks(alias: bool) -> None: + calls = [] + + class Child(msgspec.Struct): + number: int + + def __post_init__(self) -> None: + calls.append(self.number) + + class Pair(NamedTuple): + child: Child + + if alias: + import typing + + if not hasattr(typing, 'TypeAliasType'): + pytest.skip('Native type aliases require Python 3.12') + annotation = typing.TypeAliasType('AliasedTuple', tuple[Child]) + else: + annotation = Pair + + class Parent(msgspec.Struct): + first: annotation + count: int + + result = client(Parent).post('/value', json={'first': [{'number': 1}], 'count': 'bad'}) + assert result.status_code == 422 and calls == [1] + + +def test_container_deduplication_failure_is_not_lost() -> None: + class Payload(msgspec.Struct): + values: Annotated[set[Decimal], msgspec.Meta(min_length=2)] + count: int + + result = client(Payload).post('/value', json={'values': ['1', '1.0'], 'count': 'bad'}) + assert result.status_code == 422 + assert 'length' in result.json()['detail'][0]['msg'] + assert 'values' in result.json()['detail'][0]['msg'] + + +def test_empty_tuple_length_error_is_not_lost() -> None: + class Payload(msgspec.Struct): + values: tuple[()] + count: int + + result = client(Payload).post('/value', json={'values': [1], 'count': 'bad'}) + assert result.status_code == 422 + assert 'length' in result.json()['detail'][0]['msg'] + + +def test_dataclass_defaults_are_not_constructed_twice() -> None: + calls = [] + + def default() -> int: + calls.append(True) + return 1 + + from dataclasses import field + + @dataclass + class Child: + number: int = field(default_factory=default) + + class Parent(msgspec.Struct): + child: Child + count: int + + result = client(Parent).post('/value', json={'child': {}, 'count': 'bad'}) + assert result.status_code == 422 and calls == [True] + assert [e['loc'] for e in result.json()['detail']] == [['body', 'count']] + + +def test_typed_dict_and_dataclass_fields_have_locations() -> None: + class Mapping(TypedDict): + number: int + count: int + + @dataclass + class Record: + number: int + count: int + + for annotation in (Mapping, Record): + result = client(annotation).post('/value', json={'number': 'bad', 'count': 'bad'}) + assert result.status_code == 422 + assert [e['loc'] for e in result.json()['detail']] == [ + ['body', 'number'], + ['body', 'count'], + ] + + +def test_numeric_dictionary_keys_keep_original_locations() -> None: + result = client(dict[int, int]).post('/value', json={'1': 'bad', 'wrong': 'bad'}) + assert result.status_code == 422 + assert [e['loc'] for e in result.json()['detail']] == [ + ['body', '1'], + ['body', 'wrong', '[key]'], + ['body', 'wrong'], + ] + + +def test_tagged_union_uses_the_selected_struct() -> None: + class First(msgspec.Struct, tag='first', tag_field='kind'): + number: int + count: int + + class Second(msgspec.Struct, tag='second', tag_field='kind'): + text: str + + c = client(First | Second) + result = c.post('/value', json={'kind': 'first', 'number': 'bad', 'count': 'bad'}) + assert result.status_code == 422 + assert [e['loc'] for e in result.json()['detail']] == [['body', 'number'], ['body', 'count']] + assert c.post('/value', json={'kind': 'second', 'text': 'ok'}).status_code == 200 + wrong = c.post('/value', json={'kind': 'wrong'}) + assert wrong.status_code == 422 and wrong.json()['detail'][0]['loc'] == ['body', 'kind'] + + +@pytest.mark.parametrize('recover', [False, True]) +def test_enum_missing_hook_is_not_repeated(recover: bool) -> None: + from enum import Enum + + calls = [] + + class Choice(str, Enum): + first = 'first' + + @classmethod + def _missing_(cls, value: object) -> Any: + calls.append(value) + return cls.first if recover else None + + class Payload(msgspec.Struct): + choice: Choice + count: int + + result = client(Payload).post('/value', json={'choice': 'other', 'count': 'bad'}) + assert result.status_code == 422 and calls == ['other'] + assert result.json()['detail'][0]['loc'] == ['body'] + assert ('count' if recover else 'enum') in result.json()['detail'][0]['msg'] + + +def test_legacy_empty_tuple_length_error_is_not_lost() -> None: + from typing import Tuple + + class Payload(msgspec.Struct): + values: Tuple[()] + count: int + + result = client(Payload).post('/value', json={'values': [1], 'count': 'bad'}) + assert result.status_code == 422 + assert 'length' in result.json()['detail'][0]['msg'] + assert 'values' in result.json()['detail'][0]['msg'] + + +@pytest.mark.parametrize('key_type,values', [(int, {'-0': 1, '0': 2}), (float, {'1': 1, '1.0': 2})]) +def test_coerced_dictionary_key_length_error_is_not_lost(key_type: Any, values: Any) -> None: + class Payload(msgspec.Struct): + values: Annotated[dict[key_type, int], msgspec.Meta(min_length=2)] + count: int + + result = client(Payload).post('/value', json={'values': values, 'count': 'bad'}) + assert result.status_code == 422 + assert 'length' in result.json()['detail'][0]['msg'] + assert 'values' in result.json()['detail'][0]['msg'] diff --git a/tests/test_constraint_carriers.py b/tests/test_constraint_carriers.py new file mode 100644 index 0000000..d13bdbe --- /dev/null +++ b/tests/test_constraint_carriers.py @@ -0,0 +1,87 @@ +"""Constraint carriers must not silently supply unsupported field behavior.""" + +from typing import Annotated, Any + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient +from pydantic import Field + +from msgspec_fastapi import MsgspecRouter + + +@pytest.mark.parametrize('position', ['body', 'response']) +@pytest.mark.parametrize( + 'option', ['alias', 'discriminator', 'exclude', 'default', 'default_factory', 'description'] +) +def test_mutated_nested_field_options_are_rejected(position: str, option: str) -> None: + calls = [] + + def default() -> str: + calls.append('factory') + return 'a' + + field = Field(pattern='^a+$') + value = { + 'alias': 'external', + 'discriminator': 'kind', + 'exclude': True, + 'default': 'a', + 'default_factory': default, + 'description': 'Additional field semantics', + }[option] + setattr(field, option, value) + annotation = list[Annotated[str, field]] + router = MsgspecRouter() + if position == 'body': + + async def endpoint(items: annotation) -> None: + pass + + with pytest.raises(TypeError): + router.post('/items')(endpoint) + else: + + async def endpoint() -> annotation: + return ['a'] + + with pytest.raises(TypeError): + router.get('/items')(endpoint) + assert calls == [] + + +def test_nested_field_numeric_constraints_remain_enforced_by_body_decoding() -> None: + annotation = list[Annotated[int, Field(ge=2, le=6, multiple_of=2)]] + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: annotation) -> None: + pass + + app = FastAPI() + app.include_router(router) + client = TestClient(app) + assert client.post('/items', json=[2, 4, 6]).status_code == 200 + result = client.post('/items', json=[2, 3, 8]) + assert result.status_code == 422 + assert [error['loc'] for error in result.json()['detail']] == [['body', 1], ['body', 2]] + + +def test_unknown_constraint_carrier_is_rejected_without_reading_its_properties() -> None: + calls = [] + + class Carrier: + @property + def metadata(self) -> list[Any]: + calls.append('metadata') + raise RuntimeError('Unsupported carrier evaluated') + + annotation = list[Annotated[str, Carrier()]] + router = MsgspecRouter() + + async def endpoint(items: annotation) -> None: + pass + + with pytest.raises(TypeError): + router.post('/items')(endpoint) + assert calls == [] diff --git a/tests/test_copy_callbacks.py b/tests/test_copy_callbacks.py new file mode 100644 index 0000000..7cc56df --- /dev/null +++ b/tests/test_copy_callbacks.py @@ -0,0 +1,72 @@ +import dataclasses +from copy import deepcopy +from typing import Any + +import msgspec +import pytest + +from msgspec_fastapi.defaults import copy_body_default, copy_metadata + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_copy_hook_scalar_memo_override_matches_stdlib(operation: Any) -> None: + @dataclasses.dataclass + class Record: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + memo[id(42)] = 'overridden' + return Record(self.value) + + original = [Record(0), 42] + expected = deepcopy(original) + result = operation(original) + assert result == expected + assert original == [Record(0), 42] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_cached_post_init_can_add_copy_hook_for_later_items(operation: Any) -> None: + active = False + calls = [] + + class Record(msgspec.Struct): + value: int + + def __post_init__(self) -> None: + if active and self.value == 1: + + def copied(self: Any, memo: dict[int, Any]) -> Any: + calls.append('copy') + return Record(self.value + 10) + + Record.__deepcopy__ = copied + + original = [Record(1), Record(2)] + del Record.__post_init__ + active = True + result = operation(original) + assert [item.value for item in result] == [1, 12] + assert [item.value for item in original] == [1, 2] + assert calls == ['copy'] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_copy_hook_memo_override_applies_to_later_mutable_value(operation: Any) -> None: + target = bytearray(b'value') + + @dataclasses.dataclass + class Record: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + memo[id(target)] = 'overridden' + return Record(self.value) + + original = [Record(0), target] + expected = deepcopy(original) + assert expected == [Record(0), 'overridden'] + result = operation(original) + assert result == expected + assert original == [Record(0), bytearray(b'value')] + assert original[1] is target diff --git a/tests/test_copy_protocol_regressions.py b/tests/test_copy_protocol_regressions.py new file mode 100644 index 0000000..210a1cc --- /dev/null +++ b/tests/test_copy_protocol_regressions.py @@ -0,0 +1,300 @@ +"""Copied defaults retain stdlib memo, callback and type-snapshot behavior.""" + +import copy +import copyreg +import dataclasses +import sys +import weakref +from collections.abc import Callable +from typing import Any + +import msgspec +import pytest + +from msgspec_fastapi import defaults +from msgspec_fastapi._defaults import _Copier + + +@pytest.fixture +def native_copy() -> Callable[..., Any]: + copier = _Copier( + copy_module=copy, + deepcopy=copy.deepcopy, + list_copier=copy._deepcopy_list, + dict_copier=copy._deepcopy_dict, + tuple_copier=copy._deepcopy_tuple, + atomic_copier=getattr(copy, '_deepcopy_atomic', None), + reconstruct=copy._reconstruct, + atomic_first=sys.version_info >= (3, 14), + memo_sentinel=copy.deepcopy.__defaults__[-1], + ) + return copier.deepcopy + + +def run_case( + operation: str, copier: Callable[..., Any], case: Callable[[Callable[..., Any]], Any] +) -> Any: + with pytest.MonkeyPatch.context() as patch: + patch.setattr(defaults, 'deepcopy', copier) + operations = { + 'direct': copier, + 'body': defaults.copy_body_default, + 'metadata': defaults.copy_metadata, + } + return case(operations[operation]) + + +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +@pytest.mark.parametrize('kind', ['dataclass', 'struct', 'list', 'dict']) +def test_initial_memo_sentinel_matches_stdlib( + native_copy: Callable[..., Any], operation: str, kind: str +) -> None: + @dataclasses.dataclass + class Record: + value: int + + class Struct(msgspec.Struct): + value: int + + def run(copier: Callable[..., Any]) -> Any: + target = ( + Record(7) + if kind == 'dataclass' + else Struct(7) + if kind == 'struct' + else [7] + if kind == 'list' + else {'value': 7} + ) + + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + memo[id(target)] = copy.deepcopy.__defaults__[-1] + return self + + return copier([Setter(0), target])[1] + + expected = run_case(operation, copy.deepcopy, run) + actual = run_case(operation, native_copy, run) + assert type(actual) is type(expected) and actual == expected + + +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +def test_tuple_second_memo_lookup_preserves_sentinel( + native_copy: Callable[..., Any], operation: str +) -> None: + sentinel = copy.deepcopy.__defaults__[-1] + + def run(copier: Callable[..., Any]) -> Any: + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + memo[id(original)] = sentinel + return self + + original = (Setter(0),) + return copier(original) + + assert run_case(operation, copy.deepcopy, run) is sentinel + assert run_case(operation, native_copy, run) is sentinel + + +@pytest.mark.skipif(sys.version_info < (3, 14), reason='Atomic-first copying starts in Python 3.14') +@pytest.mark.parametrize('operation', ['direct', 'metadata']) +@pytest.mark.parametrize('kind', ['function', 'class', 'builtin']) +def test_atomic_references_precede_memo_on_python314( + native_copy: Callable[..., Any], operation: str, kind: str +) -> None: + @dataclasses.dataclass + class Record: + value: int + + def function() -> None: + pass + + target = function if kind == 'function' else Record if kind == 'class' else len + + def run(copier: Callable[..., Any]) -> Any: + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + memo[id(target)] = 'overridden' + return self + + return copier([Setter(0), target])[1] + + assert run_case(operation, copy.deepcopy, run) is target + assert run_case(operation, native_copy, run) is target + + +@pytest.mark.skipif(sys.version_info < (3, 14), reason='Python 3.14 has a separate atomic type set') +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +def test_removed_atomic_type_uses_current_reducer( + native_copy: Callable[..., Any], monkeypatch: pytest.MonkeyPatch, operation: str +) -> None: + atomics = set(copy._atomic_types) + atomics.remove(int) + monkeypatch.setattr(copy, '_atomic_types', atomics) + monkeypatch.setitem(copyreg.dispatch_table, int, lambda value: (str, ('changed',))) + + def run(copier: Callable[..., Any]) -> Any: + return copier([42]) + + assert run_case(operation, copy.deepcopy, run) == ['changed'] + assert run_case(operation, native_copy, run) == ['changed'] + + +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +def test_false_reducer_release_follows_next_lookup( + native_copy: Callable[..., Any], operation: str +) -> None: + def run(copier: Callable[..., Any]) -> Any: + active = False + calls = [] + + class Value(msgspec.Struct): + value: int + + def __post_init__(self) -> None: + if active: + calls.append(('post', self.value)) + if self.value == 1: + copyreg.dispatch_table[Value] = Reducer() + else: + Value.__reduce_ex__ = object.__reduce_ex__ + + def replaced(self: Any, protocol: int) -> tuple[Any, tuple[int]]: + calls.append(('replaced', self.value)) + return Value, (self.value + 10,) + + class Reducer: + def __bool__(self) -> bool: + calls.append(('bool',)) + copyreg.dispatch_table.pop(Value, None) + return False + + def __del__(self) -> None: + calls.append(('del',)) + Value.__reduce_ex__ = replaced + + original = [Value(1), Value(2)] + active = True + try: + result = copier(original) + return [item.value for item in result], calls + finally: + copyreg.dispatch_table.pop(Value, None) + + expected = run_case(operation, copy.deepcopy, run) + assert expected[0] == [1, 2] + assert run_case(operation, native_copy, run) == expected + + +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +def test_copy_hook_release_follows_memoization( + native_copy: Callable[..., Any], operation: str +) -> None: + def run(copier: Callable[..., Any]) -> Any: + state: dict[str, Any] = {} + calls = [] + + @dataclasses.dataclass + class Target: + value: int + + class Value(msgspec.Struct): + value: int + + target = Target(7) + original = Value(1) + + def copied(self: Any, memo: dict[int, Any]) -> Any: + state['memo'] = memo + del Value.__deepcopy__ + return Value(self.value + 10) + + def released(reference: weakref.ReferenceType[Any]) -> None: + memo = state['memo'] + stage = 'memoized' if id(original) in memo else 'not memoized' + calls.append(stage) + memo[id(target)] = stage + + reference = weakref.ref(copied, released) + Value.__deepcopy__ = copied + del copied + result = copier([original, target]) + assert reference() is None + return [result[0].value, result[1]], calls + + expected = run_case(operation, copy.deepcopy, run) + assert expected == ([11, 'memoized'], ['memoized']) + assert run_case(operation, native_copy, run) == expected + + +@pytest.mark.parametrize('operation', ['direct', 'body', 'metadata']) +def test_dispatch_lookup_retains_original_class_snapshot( + native_copy: Callable[..., Any], operation: str +) -> None: + def run(copier: Callable[..., Any]) -> Any: + active = False + calls = [] + + class Meta(type): + def __hash__(cls) -> int: + nonlocal active + if active and cls is Before: + active = False + target.__class__ = After + calls.append('class changed') + return type.__hash__(cls) + + @dataclasses.dataclass + class Before(metaclass=Meta): + value: int + + @dataclasses.dataclass + class After(metaclass=Meta): + value: int + + def make(value: int) -> Any: + copyreg.dispatch_table.pop(Before, None) + copyreg.dispatch_table.pop(After, None) + return Before(value) + + def reduce_before(value: Any) -> tuple[Any, tuple[int]]: + calls.append('before reducer') + return make, (value.value + 10,) + + def reduce_after(value: Any) -> tuple[Any, tuple[int]]: + calls.append('after reducer') + return make, (value.value + 100,) + + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: dict[int, Any]) -> Any: + nonlocal active + copyreg.dispatch_table[Before] = reduce_before + copyreg.dispatch_table[After] = reduce_after + active = True + return self + + target = Before(1) + try: + result = copier([Setter(0), target]) + return result[1].value, calls + finally: + copyreg.dispatch_table.pop(Before, None) + copyreg.dispatch_table.pop(After, None) + + expected = run_case(operation, copy.deepcopy, run) + assert expected == (11, ['class changed', 'before reducer']) + assert run_case(operation, native_copy, run) == expected diff --git a/tests/test_copy_state_protocols.py b/tests/test_copy_state_protocols.py new file mode 100644 index 0000000..2b4deb8 --- /dev/null +++ b/tests/test_copy_state_protocols.py @@ -0,0 +1,358 @@ +"""Copy protocols preserve native state operations and argument exceptions.""" + +import copy +import dataclasses +import weakref +from typing import Any + +import msgspec +import pytest + +from msgspec_fastapi.defaults import deepcopy as native + + +def state_case(operation: Any, kind: Any) -> Any: + events = [] + original_dict_copier = copy._deepcopy_dispatch[dict] + raw_state = {'value': 7} + + class Slots: + def items(self) -> Any: + events.append('items') + if kind == 'bad-pair': + return [[1]] + return [['extra', 9]] + + class TupleState(tuple): + def __len__(self) -> Any: + events.append('len') + return 2 + + def __iter__(self) -> Any: + events.append('iter') + return iter(({'value': 8}, Slots())) + + class Namespace: + def update(self, state: Any) -> None: + events.append(('update', dict(state))) + + class Product: + def __setattr__(self, key: Any, value: Any) -> None: + events.append(('setattr', key, value)) + object.__setattr__(self, key, value) + + if kind.startswith('setstate'): + + def getter(self: Any) -> Any: + events.append('getter') + if kind == 'setstate-valueerror': + raise ValueError('probe') + if kind == 'setstate-none': + return None + + def apply(state: Any) -> Any: + events.append(('setstate', state)) + + return apply + + Product.__setstate__ = property(getter) + if kind == 'namespace': + + def namespace(self: Any) -> Any: + events.append('namespace') + return Namespace() + + class NamespaceProduct(Product): + __dict__ = property(namespace) + + Product = NamespaceProduct + + def state_copier(value: Any, memo: Any) -> Any: + if value is not raw_state: + return original_dict_copier(value, memo) + events.append('state-copy') + if kind in ('none', 'setstate-copy-none'): + return None + if kind == 'tuple-subclass': + return TupleState((1, 2)) + if kind == 'bad-pair': + return (None, Slots()) + return {'value': 7} + + def constructor() -> Any: + events.append('constructor') + copy._deepcopy_dispatch[dict] = state_copier + return Product() + + @dataclasses.dataclass + class Original: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + events.append(('reduce', protocol)) + return (constructor, (), raw_state, None, None) + + try: + result = operation(Original(7)) + if kind != 'namespace': + result = dict(object.__getattribute__(result, '__dict__')) + else: + result = None + return (result, events) + except Exception as error: + return ((type(error).__name__, str(error)), events) + finally: + copy._deepcopy_dispatch[dict] = original_dict_copier + + +@pytest.mark.parametrize( + ('kind', 'expected_result'), + [ + ('none', {}), + ('tuple-subclass', {'value': 8, 'extra': 9}), + ('bad-pair', ValueError), + ('setstate-copy-none', {}), + ('setstate-none', TypeError), + ('setstate-valueerror', ValueError), + ('namespace', None), + ], +) +def test_dynamic_state_matches_stdlib(kind: Any, expected_result: Any) -> None: + expected = state_case(copy.deepcopy, kind) + if isinstance(expected_result, type): + assert expected[0][0] == expected_result.__name__ + else: + assert expected[0] == expected_result + assert state_case(native, kind) == expected + + +def test_probe_result_release_precedes_second_setstate_getter() -> None: + + def run(operation: Any) -> Any: + events = [] + + class Probe: + def __del__(self) -> None: + events.append('probe-release') + Product.__setstate__ = lambda self, value: events.append(('new-setstate', value)) + + class Product: + @property + def __setstate__(self) -> Any: + events.append('getter') + return Probe() + + @dataclasses.dataclass + class Original: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + return (Product, (), {'value': 7}) + + operation(Original(7)) + return events + + assert ( + run(native) + == run(copy.deepcopy) + == ['getter', 'probe-release', ('new-setstate', {'value': 7})] + ) + + +def test_constructor_uses_snapshot_arguments_before_state() -> None: + + def run(operation: Any) -> Any: + events = [] + original_state = {'value': 7} + + @dataclasses.dataclass + class Original: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + return (create, (self.value,), original_state) + + class Result: + pass + + def create(value: Any) -> Any: + events.append(('constructor', value)) + original_state['value'] = value + 1 + return Result() + + result = operation(Original(7)) + return (result.value, events) + + assert run(native) == run(copy.deepcopy) == (8, [('constructor', 7)]) + + +def test_copied_state_lifetime_precedes_outer_keepalive() -> None: + + def run(operation: Any) -> Any: + events = [] + original_state = {'value': 7} + old = copy._deepcopy_dispatch[dict] + holder = {} + + class State(dict): + def __del__(self) -> None: + memo = holder['memo'] + present = any((item is original for item in memo.get(id(memo), []))) + events.append(('state-release', id(original) in memo, present)) + + @dataclasses.dataclass + class Record: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + return (Product, (), original_state) + + class Product: + pass + + original = Record(7) + + def function(value: Any, memo: Any) -> Any: + holder['memo'] = memo + copy._deepcopy_dispatch[dict] = old + return State(value) + + def released(reference: Any) -> Any: + holder['memo'].pop(id(original_state), None) + + reference = weakref.ref(function, released) + copy._deepcopy_dispatch[dict] = function + del function + try: + result = operation(original) + assert reference() is None + assert result.value == 7 + return events + finally: + copy._deepcopy_dispatch[dict] = old + + assert run(native) == run(copy.deepcopy) == [('state-release', True, False)] + + +def describe(operation: Any, value: Any) -> Any: + try: + operation(value) + except BaseException as error: + return ( + type(error).__name__, + str(error), + type(error.__cause__).__name__, + str(error.__cause__), + error.__context__ is error.__cause__, + error.__suppress_context__, + ) + raise AssertionError('expected error') + + +@pytest.mark.parametrize('shape,exception', [(2, StopAsyncIteration), (5, StopIteration)]) +def test_constructor_argument_copy_generator_errors(shape: Any, exception: Any) -> None: + + @dataclasses.dataclass + class Child: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + raise exception('copy child') + + @dataclasses.dataclass + class Original: + child: Child + + def __reduce_ex__(self, protocol: Any) -> Any: + result = (Product, (self.child,)) + if shape == 5: + result += ({}, None, None) + return result + + class Product: + def __init__(self, value: Any) -> Any: + self.value = value + + original = Original(Child(7)) + assert describe(native, original) == describe(copy.deepcopy, original) + + +def test_struct_argument_body_metadata_supported_error() -> None: + + @dataclasses.dataclass + class Child: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + raise StopIteration('copy child') + + class Root(msgspec.Struct): + child: Child + + from msgspec_fastapi import defaults + + original = Root(Child(7)) + expected = describe(copy.deepcopy, original) + assert describe(native, original) == expected + assert describe(defaults.copy_body_default, original) == expected + assert describe(defaults.copy_metadata, original) == expected + + +@pytest.mark.parametrize('shape', [2, 5]) +def test_constructor_itself_stopiteration_is_not_wrapped(shape: Any) -> None: + + def constructor() -> Any: + raise StopIteration('constructor') + + @dataclasses.dataclass + class Original: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + result = (constructor, ()) + if shape == 5: + result += ({}, None, None) + return result + + value = Original(7) + assert describe(native, value) == describe(copy.deepcopy, value) + + +def test_top_level_hook_stopiteration_is_not_wrapped() -> None: + + @dataclasses.dataclass + class Original: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + raise StopIteration('root hook') + + value = Original(7) + assert describe(native, value) == describe(copy.deepcopy, value) + + +def test_state_copy_stopiteration_is_not_wrapped() -> None: + old = copy._deepcopy_dispatch[dict] + state = {'value': 7} + + def copied(value: Any, memo: Any) -> Any: + if value is state: + raise StopIteration('state') + return old(value, memo) + + class Product: + pass + + @dataclasses.dataclass + class Original: + value: int + + def __reduce_ex__(self, protocol: Any) -> Any: + return (Product, (), state) + + value = Original(7) + copy._deepcopy_dispatch[dict] = copied + try: + assert describe(native, value) == describe(copy.deepcopy, value) + finally: + copy._deepcopy_dispatch[dict] = old diff --git a/tests/test_copy_type_predicates.py b/tests/test_copy_type_predicates.py new file mode 100644 index 0000000..47b7511 --- /dev/null +++ b/tests/test_copy_type_predicates.py @@ -0,0 +1,138 @@ +"""Copy predicates retain ordinary Python instance-check behavior.""" + +import copy +import copyreg +import dataclasses +from typing import Annotated, Any + +import pytest +from fastapi import Body, FastAPI +from fastapi.testclient import TestClient + +from msgspec_fastapi import MsgspecRouter, defaults + + +def operation_with(copier: Any, operation: Any, value: Any) -> Any: + with pytest.MonkeyPatch.context() as patch: + patch.setattr(defaults, 'deepcopy', copier) + return operation(value) + + +@pytest.mark.parametrize('raises', [False, True]) +def test_copied_state_class_property_matches_stdlib(raises: bool) -> None: + def run(copier: Any) -> Any: + events = [] + original_state = {'value': 7} + old = copy._deepcopy_dispatch[dict] + + class State: + @property + def __class__(self) -> Any: + events.append('class') + if raises: + raise ValueError('state class') + return tuple + + def __len__(self) -> int: + events.append('len') + return 2 + + def __iter__(self) -> Any: + events.append('iter') + return iter(({'value': 8}, None)) + + @dataclasses.dataclass + class Product: + value: int = 0 + + @dataclasses.dataclass + class Original: + value: int + + def copied_state(value: Any, memo: Any) -> Any: + return State() if value is original_state else old(value, memo) + + def construct() -> Any: + copyreg.dispatch_table.pop(Original, None) + copy._deepcopy_dispatch[dict] = copied_state + return Product() + + def reduced(value: Any) -> Any: + return construct, (), original_state + + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + copyreg.dispatch_table[Original] = reduced + return self + + def body_route(value: Any) -> Any: + router = MsgspecRouter() + + async def endpoint(items: Annotated[Any, Body()] = value) -> int: + return items[1].value + + router.post('/copy')(endpoint) + app = FastAPI() + app.include_router(router) + response = TestClient(app).post('/copy') + assert response.status_code == 200 + return response.json() + + try: + result = operation_with(copier, body_route, [Setter(0), Original(7)]) + return result, events + except ValueError as error: + return str(error), events + finally: + copy._deepcopy_dispatch[dict] = old + copyreg.dispatch_table.pop(Original, None) + + expected = ('state class', ['class']) if raises else (8, ['class', 'len', 'iter']) + assert run(copy.deepcopy) == expected + assert run(defaults.deepcopy) == expected + + +@pytest.mark.parametrize('raises', [False, True]) +def test_reduction_result_class_property_matches_stdlib(raises: bool) -> None: + def run(copier: Any) -> Any: + events = [] + + @dataclasses.dataclass + class Original: + value: int + + class Proxy: + @property + def __class__(self) -> Any: + events.append('class') + if raises: + raise ValueError('reduction class') + return str + + def reduced(value: Any) -> Any: + copyreg.dispatch_table.pop(Original, None) + return Proxy() + + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + copyreg.dispatch_table[Original] = reduced + return self + + original = Original(7) + try: + result = operation_with(copier, defaults.copy_metadata, [Setter(0), original]) + return result[1] is original, events + except ValueError as error: + return str(error), events + finally: + copyreg.dispatch_table.pop(Original, None) + + expected = ('reduction class', ['class']) if raises else (True, ['class']) + assert run(copy.deepcopy) == expected + assert run(defaults.deepcopy) == expected diff --git a/tests/test_copy_unpack_order.py b/tests/test_copy_unpack_order.py new file mode 100644 index 0000000..174e45a --- /dev/null +++ b/tests/test_copy_unpack_order.py @@ -0,0 +1,96 @@ +"""Failed state unpacking preserves cleanup order and versioned errors.""" + +import copy +import copyreg +import dataclasses +import sys +from typing import Any + +import pytest + +from msgspec_fastapi import defaults + + +@pytest.mark.parametrize('exact_pair', [False, True]) +def test_failed_state_pair_unpack_matches_stdlib(exact_pair: bool) -> None: + def run(copier: Any) -> Any: + events = [] + state = {'value': 7} + old = copy._deepcopy_dispatch[dict] + + class Extra: + def __del__(self) -> None: + events.append('extra') + + class Iterator: + def __init__(self) -> None: + self.index = 0 + + def __iter__(self) -> Any: + return self + + def __next__(self) -> Any: + self.index += 1 + if self.index == 1: + return 'key' + if self.index == 2: + return 1 + if self.index == 3: + return Extra() + raise StopIteration + + def __del__(self) -> None: + events.append('iterator') + + class Pair: + def __iter__(self) -> Any: + return Iterator() + + class Slots: + def items(self) -> Any: + return [['key', 1, 2]] if exact_pair else [Pair()] + + def state_copy(value: Any, memo: Any) -> Any: + return (None, Slots()) if value is state else old(value, memo) + + class Product: + pass + + @dataclasses.dataclass + class Original: + value: int + + def construct() -> Any: + copyreg.dispatch_table.pop(Original, None) + copy._deepcopy_dispatch[dict] = state_copy + return Product() + + def reduced(value: Any) -> Any: + return construct, (), state + + @dataclasses.dataclass + class Setter: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + copyreg.dispatch_table[Original] = reduced + return self + + try: + with pytest.MonkeyPatch.context() as patch: + patch.setattr(defaults, 'deepcopy', copier) + with pytest.raises(ValueError) as error: + defaults.copy_metadata([Setter(0), Original(7)]) + return str(error.value), events + finally: + copy._deepcopy_dispatch[dict] = old + copyreg.dispatch_table.pop(Original, None) + + message = ( + 'too many values to unpack (expected 2, got 3)' + if exact_pair and sys.version_info >= (3, 14) + else 'too many values to unpack (expected 2)' + ) + expected = message, [] if exact_pair else ['extra', 'iterator'] + assert run(copy.deepcopy) == expected + assert run(defaults.deepcopy) == expected diff --git a/tests/test_default_accessors.py b/tests/test_default_accessors.py new file mode 100644 index 0000000..2f6770a --- /dev/null +++ b/tests/test_default_accessors.py @@ -0,0 +1,165 @@ +import dataclasses +from typing import Annotated, Any + +import msgspec +import pytest +from fastapi import Body, FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter + + +def application(default: Any) -> FastAPI: + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(value: Annotated[Any, Body()] = default) -> int: + return value.value + + app = FastAPI() + app.include_router(router) + return app + + +@pytest.mark.parametrize('kind', ['attribute', 'shadowed_slot', 'copy_descriptor']) +def test_dynamic_copy_protocol_rejected_before_hidden_model_copy(kind: str) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=1) + + @dataclasses.dataclass + class Data: + value: int + + if kind == 'attribute': + + class Default(Data): + __slots__ = ('extra',) + + def __getattribute__(self, name: str) -> Any: + if name == 'extra': + return hidden + return super().__getattribute__(name) + + value = Default(7) + object.__setattr__(value, 'extra', None) + elif kind == 'shadowed_slot': + + class Slotted(Data): + __slots__ = ('extra',) + + class Default(Slotted): + @property + def extra(self) -> Any: + return hidden + + value = Default(7) + Slotted.__dict__['extra'].__set__(value, None) + else: + + class Default(Data): + @property + def __deepcopy__(self) -> Any: + return hidden.__deepcopy__ + + value = Default(7) + + with pytest.raises(TypeError): + application(value) + assert calls == [] + + +@pytest.mark.parametrize('kind', ['dataclass', 'frozen_slots', 'struct']) +def test_ordinary_native_default_copy_remains_supported(kind: str) -> None: + if kind == 'struct': + + class Record(msgspec.Struct): + value: int + else: + + @dataclasses.dataclass(frozen=kind == 'frozen_slots', slots=kind == 'frozen_slots') + class Record: + value: int + + app = application(Record(7)) + response = TestClient(app).post('/value') + assert response.status_code == 200 and response.json() == 7 + + +@pytest.mark.parametrize('kind', ['dataclass', 'struct']) +def test_ordinary_python_copy_function_count_is_preserved(kind: str) -> None: + calls = [] + + if kind == 'struct': + + class Record(msgspec.Struct): + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('copy') + return Record(self.value + 1) + else: + + @dataclasses.dataclass + class Record: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('copy') + return Record(self.value + 1) + + app = application(Record(7)) + calls.clear() + response = TestClient(app).post('/value') + assert response.status_code == 200 and response.json() == 8 + assert calls == ['copy'] + + +@pytest.mark.parametrize('kind', ['enum', 'int_enum', 'str_enum']) +def test_native_enum_default_remains_supported(kind: str) -> None: + from enum import Enum, IntEnum, StrEnum + + enum_class = {'enum': Enum, 'int_enum': IntEnum, 'str_enum': StrEnum}[kind] + value = enum_class('Choice', {'value': 7 if kind != 'str_enum' else 'seven'}).value + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(payload: Annotated[Any, Body()] = value) -> Any: + return payload + + app = FastAPI() + app.include_router(router) + response = TestClient(app).post('/value') + assert response.status_code == 200 and response.json() == value.value + + +@pytest.mark.parametrize('kind', ['datetime', 'time', 'zoneinfo']) +def test_temporal_default_native_state_remains_supported(kind: str) -> None: + import datetime + from zoneinfo import ZoneInfo + + timezone = datetime.timezone(datetime.timedelta(hours=2)) + value = { + 'datetime': datetime.datetime(2026, 1, 2, tzinfo=timezone), + 'time': datetime.time(12, 30, tzinfo=timezone), + 'zoneinfo': datetime.datetime(2026, 1, 2, tzinfo=ZoneInfo('UTC')), + }[kind] + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(payload: Annotated[Any, Body()] = value) -> Any: + return payload + + app = FastAPI() + app.include_router(router) + response = TestClient(app).post('/value') + assert response.status_code == 200 + assert response.content == msgspec.json.encode(value) diff --git a/tests/test_default_copy_protocols.py b/tests/test_default_copy_protocols.py new file mode 100644 index 0000000..30f20fe --- /dev/null +++ b/tests/test_default_copy_protocols.py @@ -0,0 +1,124 @@ +"""Body default copying excludes arbitrary state reconstruction protocols.""" + +import dataclasses +from typing import Annotated, Any + +import msgspec +import pytest +from fastapi import Body, FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter + + +def register(default: Any) -> FastAPI: + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(value: Annotated[Any, Body()] = default) -> int: + return value.value + + app = FastAPI() + app.include_router(router) + return app + + +@pytest.mark.parametrize( + 'method', + [ + '__getstate__', + '__setstate__', + '__reduce__', + '__reduce_ex__', + '__getnewargs__', + '__getnewargs_ex__', + ], +) +def test_custom_state_protocol_is_rejected_without_invocation(method: str) -> None: + calls = [] + + @dataclasses.dataclass + class Record: + value: int + + def protocol(self: Any, *args: Any) -> Any: + calls.append(method) + raise RuntimeError('Custom reconstruction protocol executed') + + setattr(Record, method, protocol) + with pytest.raises(TypeError): + register(Record(1)) + assert calls == [] + + +def test_inherited_state_protocol_is_rejected() -> None: + calls = [] + + class State: + def __getstate__(self) -> dict[str, int]: + calls.append('state') + return {'value': 1} + + @dataclasses.dataclass + class Record(State): + value: int + + with pytest.raises(TypeError): + register(Record(1)) + assert calls == [] + + +def test_standard_frozen_slots_dataclass_state_remains_supported() -> None: + @dataclasses.dataclass(frozen=True, slots=True) + class Record: + value: int + + response = TestClient(register(Record(7))).post('/items') + assert response.status_code == 200 + assert response.json() == 7 + + +def test_late_state_hook_is_rejected_before_state_or_model_copy() -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('model-copy') + return self + + hidden = External(value=1) + + @dataclasses.dataclass + class Record: + value: int + + app = register(Record(1)) + + def state(self: Any) -> dict[str, Any]: + calls.append('state') + return {'value': 1, 'hidden': hidden} + + Record.__getstate__ = state + with pytest.raises(TypeError): + TestClient(app).post('/items') + assert calls == [] + + +def test_struct_default_copy_preserves_user_copy_count() -> None: + calls = [] + + class Record(msgspec.Struct): + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('copy') + return Record(self.value + len(calls)) + + app = register(Record(7)) + calls.clear() + response = TestClient(app).post('/items') + assert response.status_code == 200 and response.json() == 8 + assert calls == ['copy'] diff --git a/tests/test_default_instance_protocols.py b/tests/test_default_instance_protocols.py new file mode 100644 index 0000000..d1c742b --- /dev/null +++ b/tests/test_default_instance_protocols.py @@ -0,0 +1,206 @@ +"""Reject instance reconstruction protocols before copying their hidden data.""" + +import copy +import dataclasses +from typing import Any + +import pytest +from pydantic import BaseModel + +from msgspec_fastapi.defaults import copy_body_default, copy_metadata + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_instance_getstate_does_not_copy_model_arguments(operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + @dataclasses.dataclass + class Record: + value: Any + + hidden = External(value=7) + original = Record(1) + original.__getstate__ = lambda: {'value': hidden} + assert copy.deepcopy(original).value.value == 7 + assert calls == ['model-copy'] + calls.clear() + with pytest.raises(TypeError, match='Instance state methods'): + operation(original) + assert calls == [] + + +@pytest.mark.parametrize( + 'name', + [ + '__reduce__', + '__reduce_ex__', + '__getnewargs__', + '__getnewargs_ex__', + '__setstate__', + '__getattribute__', + '__getattr__', + ], +) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_instance_state_protocols_are_rejected_before_application_callbacks( + name: str, operation: Any +) -> None: + calls = [] + + @dataclasses.dataclass + class Record: + value: int + + def protocol(*args: Any) -> Any: + calls.append(name) + raise RuntimeError('Instance state callback executed') + + original = Record(1) + setattr(original, name, protocol) + with pytest.raises(TypeError, match='Instance state methods'): + operation(original) + assert calls == [] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_plain_instance_copy_function_still_runs_once(operation: Any) -> None: + calls = [] + + @dataclasses.dataclass + class Record: + value: int + + original = Record(1) + + def duplicate(memo: Any) -> Record: + calls.append('application-copy') + return Record(9) + + original.__deepcopy__ = duplicate + result = operation(original) + assert result.value == 9 + assert calls == ['application-copy'] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_instance_copy_result_still_requires_supported_data(operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + @dataclasses.dataclass + class Record: + value: int + + hidden = External(value=7) + original = Record(1) + + def duplicate(memo: Any) -> Any: + calls.append('application-copy') + return hidden + + original.__deepcopy__ = duplicate + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + operation(original) + assert calls == ['application-copy'] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +@pytest.mark.parametrize('position', ['class', 'instance']) +def test_explicit_copy_function_precedes_registered_reducer( + operation: Any, position: str, monkeypatch: pytest.MonkeyPatch +) -> None: + import copyreg + + calls = [] + + @dataclasses.dataclass + class Record: + value: int + + def duplicate(self: Any, memo: Any) -> Record: + calls.append('application-copy') + return Record(self.value + 10) + + original = Record(1) + if position == 'class': + Record.__deepcopy__ = duplicate + else: + + def with_copy(value: int) -> Record: + instance = Record(value) + + def duplicate_instance(memo: Any) -> Record: + calls.append('application-copy') + return with_copy(instance.value + 10) + + instance.__deepcopy__ = duplicate_instance + return instance + + original = with_copy(1) + + def reducer(value: Any) -> Any: + calls.append('reducer') + raise AssertionError('Ignored registered reducer executed') + + monkeypatch.setitem(copyreg.dispatch_table, Record, reducer) + assert copy.deepcopy(original).value == 11 + assert calls == ['application-copy'] + calls.clear() + assert operation(original).value == 11 + assert calls == ['application-copy'] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_instance_copy_result_cannot_expose_reducer_on_framework_recopy( + operation: Any, monkeypatch: pytest.MonkeyPatch +) -> None: + import copyreg + + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + @dataclasses.dataclass + class Record: + value: Any + + hidden = External(value=7) + original = Record(1) + + def duplicate(memo: Any) -> Record: + calls.append('application-copy') + return Record(11) + + original.__deepcopy__ = duplicate + + def reducer(value: Any) -> Any: + calls.append('reducer') + return Record, (hidden,) + + monkeypatch.setitem(copyreg.dispatch_table, Record, reducer) + first = copy.deepcopy(original) + assert calls == ['application-copy'] + assert copy.deepcopy(first).value.value == 7 + assert calls == ['application-copy', 'reducer', 'model-copy'] + calls.clear() + with pytest.raises(TypeError, match='Registered copy reducers'): + operation(original) + assert calls == ['application-copy'] diff --git a/tests/test_default_slot_bindings.py b/tests/test_default_slot_bindings.py new file mode 100644 index 0000000..ec15562 --- /dev/null +++ b/tests/test_default_slot_bindings.py @@ -0,0 +1,274 @@ +"""Declared and cached slot names must not expose property-backed copy state.""" + +import copy +import dataclasses +from typing import Any + +import pytest +from pydantic import BaseModel + +from msgspec_fastapi.defaults import _check_copy_graph, copy_body_default, copy_metadata + + +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +def test_same_declaring_class_slot_replacement_is_rejected_before_getter(operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=7) + + @dataclasses.dataclass + class Record: + value: int + + class Slotted(Record): + __slots__ = ('extra',) + + original = Slotted(1) + original.extra = None + + def replaced(self: Any) -> Any: + calls.append('slot-getter') + return hidden + + Slotted.extra = property(replaced) + with pytest.raises(AttributeError): + copy.deepcopy(original) + assert calls == ['slot-getter', 'model-copy'] + calls.clear() + if '__slotnames__' in Slotted.__dict__: + del Slotted.__slotnames__ + with pytest.raises(TypeError, match='Non-native slot bindings'): + operation(original) + assert calls == [] + + +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +def test_forged_cached_slot_name_is_rejected_before_getter(operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=7) + + @dataclasses.dataclass + class Record: + value: int + + @property + def exposed(self) -> Any: + calls.append('slot-getter') + return hidden + + Record.__slotnames__ = ['exposed'] + original = Record(1) + with pytest.raises(AttributeError): + copy.deepcopy(original) + assert calls == ['slot-getter', 'model-copy'] + calls.clear() + with pytest.raises(TypeError, match='Non-native slot bindings'): + operation(original) + assert calls == [] + + +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +@pytest.mark.parametrize('kind', ['declared', 'cached']) +def test_slot_name_iterable_is_rejected_without_application_iteration( + operation: Any, kind: str +) -> None: + calls = [] + + @dataclasses.dataclass + class Record: + value: int + + class Slotted(Record): + __slots__ = ('extra',) + + class Names: + def __iter__(self) -> Any: + calls.append('slot-iteration') + raise RuntimeError('Application slot iterable executed') + + original = Slotted(1) + original.extra = None + if kind == 'declared': + Slotted.__slots__ = Names() + else: + Slotted.__slotnames__ = Names() + with pytest.raises(TypeError, match='slot'): + operation(original) + assert calls == [] + + +@pytest.mark.parametrize('container', [str, tuple, list, set, frozenset, dict]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_closed_slot_declarations_keep_native_slot_copy(container: Any, operation: Any) -> None: + declaration = ( + 'extra' + if container is str + else {'extra': 'native slot'} + if container is dict + else container(['extra']) + ) + + @dataclasses.dataclass + class Record: + value: int + + class Slotted(Record): + __slots__ = declaration + + original = Slotted(1) + original.extra = [7] + result = operation(original) + assert result.value == 1 and result.extra == [7] + assert result.extra is not original.extra + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_private_inherited_and_special_native_slots_remain_supported(operation: Any) -> None: + @dataclasses.dataclass + class __Base: + __slots__ = ('__value', '__dict__', '__weakref__') + __value: int + + class _Child(__Base): + __slots__ = ('__extra',) + + original = _Child(7) + original._Child__extra = [9] + result = operation(original) + assert result._Base__value == 7 and result._Child__extra == [9] + assert result._Child__extra is not original._Child__extra + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_frozen_slotted_dataclass_native_state_remains_supported(operation: Any) -> None: + @dataclasses.dataclass(frozen=True, slots=True) + class Record: + value: list[int] + + original = Record([7]) + result = operation(original) + assert result.value == [7] and result.value is not original.value + + +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +def test_private_slot_validation_uses_native_class_name(operation: Any) -> None: + calls = [] + active = False + + class ReadonlyMeta(type): + def __getattribute__(cls, name: str) -> Any: + if active and name == '__name__': + calls.append('metaclass-name') + raise RuntimeError('Application class-name lookup executed') + return super().__getattribute__(name) + + def __setattr__(cls, name: str, value: Any) -> None: + if name == '__slotnames__': + raise RuntimeError('Readonly slot-name cache') + super().__setattr__(name, value) + + @dataclasses.dataclass + class _Record(metaclass=ReadonlyMeta): + __slots__ = ('__value',) + __value: int + + original = _Record(7) + type.__setattr__(_Record, '__slotnames__', ['_Record__value']) + active = True + result = operation(original) + if operation is not _check_copy_graph: + assert result._Record__value == 7 + assert calls == [] + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_private_slot_in_all_underscore_class_name_remains_supported(operation: Any) -> None: + @dataclasses.dataclass + class ___: + __slots__ = ('__value',) + __value: int + + result = operation(___(7)) + assert result.__value == 7 + + +@pytest.mark.parametrize('shape', [tuple, set, frozenset, dict]) +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +def test_nonlist_cached_slot_names_are_rejected(shape: Any, operation: Any) -> None: + @dataclasses.dataclass + class Record: + __slots__ = ('value',) + value: int + + Record.__slotnames__ = {'value': 7} if shape is dict else shape(['value']) + with pytest.raises(TypeError, match='__slotnames__ should be a list or None'): + copy.deepcopy(Record(7)) + with pytest.raises(TypeError, match='cached slot names'): + operation(Record(7)) + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_enum_identity_copy_ignores_unrelated_slot_metadata(operation: Any) -> None: + from enum import Enum + + calls = [] + + class Names: + def __iter__(self) -> Any: + calls.append('slot-iteration') + raise RuntimeError('Unrelated Enum slots inspected') + + class Choice(Enum): + one = 1 + + Choice.__slots__ = Names() + Choice.__slotnames__ = Names() + assert operation(Choice.one) is Choice.one + assert calls == [] + + +@pytest.mark.parametrize('operation', [_check_copy_graph, copy_body_default, copy_metadata]) +@pytest.mark.parametrize('kind', ['declared', 'cached']) +def test_slot_name_subclass_is_rejected_before_string_callbacks(operation: Any, kind: str) -> None: + calls = [] + + class Name(str): + def startswith(self, *args: Any) -> Any: + calls.append('string-prefix') + raise RuntimeError('Application string callback executed') + + def __hash__(self) -> int: + calls.append('string-hash') + raise RuntimeError('Application string callback executed') + + @dataclasses.dataclass + class Record: + value: int + + class Slotted(Record): + __slots__ = ('extra',) + + original = Slotted(1) + original.extra = None + if kind == 'declared': + Slotted.__slots__ = [Name('extra')] + else: + Slotted.__slotnames__ = [Name('extra')] + with pytest.raises(TypeError, match='exact strings'): + operation(original) + assert calls == [] diff --git a/tests/test_diagnostic_plans.py b/tests/test_diagnostic_plans.py new file mode 100644 index 0000000..61c0a67 --- /dev/null +++ b/tests/test_diagnostic_plans.py @@ -0,0 +1,199 @@ +"""Wire-level diagnostics preserve validation and user callback boundaries.""" + +import sys +from enum import Enum +from typing import Annotated, Any + +import msgspec +import pytest +from fastapi import Body, FastAPI +from fastapi.testclient import TestClient + +from msgspec_fastapi import MsgspecRouter + + +def client(annotation: Any) -> TestClient: + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: annotation) -> None: + pass + + app = FastAPI() + app.include_router(router) + return TestClient(app) + + +@pytest.mark.parametrize('array', [False, True]) +def test_list_diagnostics_do_not_construct_original_models(array: bool) -> None: + calls = [] + + def default() -> str: + calls.append('factory') + return 'default' + + class Item(msgspec.Struct, array_like=array, tag='item'): + count: int = msgspec.field(name='n') + note: str = msgspec.field(default_factory=default) + + def __del__(self) -> None: + calls.append('destructor') + + payload = ( + [['item', 1], ['item', 'bad']] + if array + else [ + {'type': 'item', 'n': 1}, + {'type': 'item', 'n': 'bad'}, + ] + ) + raw = msgspec.json.encode(payload) + test_client = client(list[Item]) + calls.clear() + with pytest.raises(msgspec.ValidationError): + msgspec.json.decode(raw, type=list[Item]) + expected = calls.copy() + calls.clear() + response = test_client.post('/items', content=raw, headers={'content-type': 'application/json'}) + assert response.status_code == 422 + assert calls == expected + assert response.json()['detail'][0]['loc'] == ['body', 1, 1 if array else 'n'] + + +@pytest.mark.parametrize('bad', [b'"\xff"', b'"\\ud800"', b'1e999', b'9' * 5000]) +@pytest.mark.parametrize('first', [False, True]) +def test_unknown_fields_cannot_hide_json_errors(bad: bytes, first: bool) -> None: + class Item(msgspec.Struct): + count: int + + unknown = b'{"count":1,"ignored":' + bad + b'}' + invalid = b'{"count":"bad"}' + raw = b'[' + (unknown + b',' + invalid if first else invalid + b',' + unknown) + b']' + with pytest.raises((msgspec.DecodeError, UnicodeError)) as error: + msgspec.json.decode(raw) + response = client(list[Item]).post( + '/items', content=raw, headers={'content-type': 'application/json'} + ) + assert response.status_code == 400 + assert response.json()['detail'].startswith('Malformed JSON body: ') + if not first: + assert response.json()['detail'] == f'Malformed JSON body: {error.value}' + + +def test_invalid_earlier_duplicate_field_is_not_accepted() -> None: + class Item(msgspec.Struct): + count: int + + response = client(list[Item]).post( + '/items', + content=b'[{"count":"bad","count":1}]', + headers={'content-type': 'application/json'}, + ) + assert response.status_code == 422 + assert 'Expected' in response.json()['detail'][0]['msg'] + + +def test_enum_hash_mutation_after_registration_cannot_repeat_callbacks() -> None: + calls = [] + + class Value(str, Enum): + first = 'first' + + class Payload(msgspec.Struct): + choices: set[Value] + count: int + + test_client = client(Payload) + + def hashing(self: Value) -> int: + calls.append('hash') + if len(calls) > 1: + raise RuntimeError('Hash callback repeated') + return hash(self.value) + + Value.__hash__ = hashing + response = test_client.post('/items', json={'choices': ['first'], 'count': 'bad'}) + assert response.status_code == 422 and calls == ['hash'] + + +def test_union_of_model_and_model_container_does_not_repeat_factory() -> None: + calls = [] + + def default() -> int: + calls.append('factory') + if len(calls) > 1: + raise RuntimeError('Factory callback repeated') + return 1 + + class Item(msgspec.Struct): + value: int = msgspec.field(default_factory=default) + + class Payload(msgspec.Struct): + items: Item | list[Item] + count: int + + response = client(Payload).post('/items', json={'items': [{}], 'count': 'bad'}) + assert response.status_code == 422 + assert calls == ['factory'] + assert response.json()['detail'][0]['loc'] == ['body'] + + +def test_field_names_cannot_add_wire_model_destructor_calls() -> None: + class Item(msgspec.Struct): + __del__: int + count: int + + raw = b'[{"__del__":1,"count":1},{"__del__":2,"count":"bad"}]' + test_client = client(list[Item]) + observed = [] + original = sys.unraisablehook + + def record(error: Any) -> None: + observed.append(error.exc_type) + + try: + sys.unraisablehook = record + with pytest.raises(msgspec.ValidationError): + msgspec.json.decode(raw, type=list[Item]) + expected = observed.copy() + observed.clear() + response = test_client.post( + '/items', content=raw, headers={'content-type': 'application/json'} + ) + assert response.status_code == 422 + assert observed == expected + finally: + sys.unraisablehook = original + + +@pytest.mark.parametrize('supplied', [False, True]) +def test_embedded_missing_default_keeps_pattern_behavior(supplied: bool) -> None: + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(value: Annotated[str, Body(embed=True, pattern='^a$')] = 'bad') -> str: + return value + + app = FastAPI() + app.include_router(router) + response = TestClient(app).post('/items', json={'value': 'bad'} if supplied else {}) + assert response.status_code == (422 if supplied else 200) + if not supplied: + assert response.json() == 'bad' + + +@pytest.mark.parametrize('value', [1 << 100, -(1 << 100)]) +def test_wrong_shape_errors_preserve_json_number_classification(value: int) -> None: + class Item(msgspec.Struct): + count: int + + encoded = msgspec.json.encode(value) + with pytest.raises(msgspec.ValidationError) as error: + msgspec.json.decode(encoded, type=Item) + response = client(list[Item]).post( + '/items', content=b'[' + encoded + b']', headers={'content-type': 'application/json'} + ) + assert response.status_code == 422 + assert response.json()['detail'] == [ + {'loc': ['body', 0], 'msg': str(error.value), 'type': 'value_error.msgspec'} + ] diff --git a/tests/test_enum_copy_protocols.py b/tests/test_enum_copy_protocols.py new file mode 100644 index 0000000..4279d26 --- /dev/null +++ b/tests/test_enum_copy_protocols.py @@ -0,0 +1,159 @@ +"""Enum identity copying must not invoke hidden model copy protocols.""" + +import copy +from enum import Enum, IntEnum, StrEnum +from typing import Any + +import pytest +from pydantic import BaseModel + +from msgspec_fastapi.defaults import copy_body_default, copy_metadata + + +def choice(enum_class: Any) -> Any: + value = 'one' if enum_class is StrEnum else 1 + return enum_class('Choice', {'one': value}).one + + +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_bound_model_copy_is_rejected_before_enum_copy(enum_class: Any, operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=7) + value = choice(enum_class) + value.__deepcopy__ = hidden.__deepcopy__ + assert copy.deepcopy(value).value == 7 + assert calls == ['model-copy'] + calls.clear() + with pytest.raises(TypeError, match='Instance copy methods'): + operation(value) + assert calls == [] + + +@pytest.mark.parametrize('kind', ['builtin', 'opaque', 'none']) +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_nonfunction_instance_copier_is_rejected( + kind: str, enum_class: Any, operation: Any +) -> None: + calls = [] + + class Opaque: + def __call__(self, memo: Any) -> Any: + calls.append('opaque-copy') + raise RuntimeError('Unsupported copy callback executed') + + value = choice(enum_class) + value.__deepcopy__ = {'builtin': calls.append, 'opaque': Opaque(), 'none': None}[kind] + with pytest.raises(TypeError, match='Instance copy methods'): + operation(value) + assert calls == [] + + +@pytest.mark.parametrize('name', ['__getstate__', '__reduce_ex__']) +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_enum_instance_state_protocols_are_rejected( + name: str, enum_class: Any, operation: Any +) -> None: + calls = [] + + def state(*args: Any) -> Any: + calls.append(name) + raise RuntimeError('Unsupported state callback executed') + + value = choice(enum_class) + setattr(value, name, state) + with pytest.raises(TypeError, match='Instance state methods'): + operation(value) + assert calls == [] + + +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_native_and_plain_function_enum_copy_remain_supported( + enum_class: Any, operation: Any +) -> None: + calls = [] + value = choice(enum_class) + assert operation(value) is value + + def duplicate(memo: Any) -> Any: + calls.append('application-copy') + return value + + value.__deepcopy__ = duplicate + assert operation(value) is value + assert calls == ['application-copy'] + + +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_plain_enum_copy_result_is_still_checked(enum_class: Any, operation: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=7) + value = choice(enum_class) + + def duplicate(memo: Any) -> Any: + calls.append('application-copy') + return hidden + + value.__deepcopy__ = duplicate + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + operation(value) + assert calls == ['application-copy'] + + +@pytest.mark.parametrize('enum_class', [Enum, IntEnum, StrEnum]) +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_enum_identity_copy_does_not_copy_unrelated_member_state( + enum_class: Any, operation: Any +) -> None: + calls = [] + + class Uncopied: + def __deepcopy__(self, memo: Any) -> Any: + calls.append('unrelated-copy') + raise RuntimeError('Unrelated Enum state copied') + + value = choice(enum_class) + value.extra = Uncopied() + assert operation(value) is value + assert calls == [] + + +def test_none_override_cannot_copy_model_enum_value_during_metadata_copy() -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + hidden = External(value=7) + value = Enum('Choice', {'one': hidden}).one + value.__deepcopy__ = None + assert copy.deepcopy(value) is value + assert calls == ['model-copy'] + calls.clear() + with pytest.raises(TypeError, match='Instance copy methods'): + copy_metadata(value) + assert calls == [] diff --git a/tests/test_error_responses.py b/tests/test_error_responses.py new file mode 100644 index 0000000..92fa6b5 --- /dev/null +++ b/tests/test_error_responses.py @@ -0,0 +1,526 @@ +"""Body error responses preserve FastAPI exception and cleanup behavior.""" + +from collections.abc import AsyncIterator, Callable, Coroutine +from typing import Annotated, Any + +import msgspec +import pytest +from fastapi import Depends, FastAPI, HTTPException, Request, Response +from fastapi.exception_handlers import request_validation_exception_handler +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from fastapi.testclient import TestClient +from pydantic import Field + +from msgspec_fastapi import MsgspecRouter +from msgspec_fastapi.router import MsgspecRoute + + +class Item(msgspec.Struct): + count: int + + +def application(router: MsgspecRouter) -> FastAPI: + app = FastAPI() + app.include_router(router) + return app + + +@pytest.mark.parametrize('asynchronous', [False, True]) +def test_custom_body_error_handler_keeps_exact_exception_type(asynchronous: bool) -> None: + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: list[Item]) -> None: + pass + + app = application(router) + observed = [] + + def handle(request: Request, exc: RequestValidationError) -> Response: + observed.append((type(exc), len(exc.errors()))) + return JSONResponse({'custom': True}, status_code=418) + + async def async_handle(request: Request, exc: RequestValidationError) -> Response: + return handle(request, exc) + + app.add_exception_handler(RequestValidationError, async_handle if asynchronous else handle) + response = TestClient(app).post('/items', json=[{'count': 'bad'}, {'count': 'bad'}]) + assert response.status_code == 418 and response.json() == {'custom': True} + assert observed == [(RequestValidationError, 2)] + + +@pytest.mark.parametrize('scope', ['request', 'function']) +def test_yield_dependency_can_translate_body_errors(scope: str) -> None: + calls = [] + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError: + calls.append('validation') + raise HTTPException(409, 'Translated validation error') + finally: + calls.append('cleanup') + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency, scope=scope)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 409 + assert response.json() == {'detail': 'Translated validation error'} + assert calls == ['validation', 'cleanup'] + + +@pytest.mark.parametrize('scope', ['request', 'function']) +def test_yield_cleanup_precedes_body_error_response(scope: str) -> None: + calls = [] + + async def dependency() -> AsyncIterator[None]: + try: + yield + finally: + calls.append('cleanup') + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency, scope=scope)]) + async def endpoint(items: list[Item]) -> None: + pass + + app = application(router) + + @app.middleware('http') + async def observe_response(request: Request, call_next: Callable[..., Any]) -> Response: + response = await call_next(request) + calls.append('response') + return response + + assert TestClient(app).post('/items', json=[{'count': 'bad'}]).status_code == 422 + assert calls == ['cleanup', 'response'] + + +def test_custom_route_can_intercept_body_error() -> None: + class CustomRoute(MsgspecRoute): + def get_route_handler(self) -> Callable[[Request], Coroutine[Any, Any, Response]]: + handler = super().get_route_handler() + + async def handle(request: Request) -> Response: + try: + return await handler(request) + except RequestValidationError: + return JSONResponse({'route': True}, status_code=409) + + return handle + + router = MsgspecRouter(route_class=CustomRoute) + + @router.post('/items') + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 409 and response.json() == {'route': True} + + +@pytest.mark.parametrize('patterns', [False, True]) +def test_default_body_error_wire_matches_native_handler(patterns: bool) -> None: + annotation = list[Annotated[str, Field(pattern='^a+$')]] if patterns else list[Item] + payload = ['a\n', 'кот'] if patterns else [{'count': 'ошибка'}, {'count': []}] + + def build(wrapped: bool) -> FastAPI: + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: annotation) -> None: + pass + + app = application(router) + if wrapped: + + @app.exception_handler(RequestValidationError) + async def handler(request: Request, exc: RequestValidationError) -> Response: + return await request_validation_exception_handler(request, exc) + + return app + + native = TestClient(build(True)).post('/items', json=payload) + actual = TestClient(build(False)).post('/items', json=payload) + assert actual.status_code == native.status_code == 422 + assert actual.content == native.content + assert actual.headers == native.headers + + +def test_handler_registration_changes_remain_visible_to_later_routes() -> None: + async def custom(request: Request, exc: RequestValidationError) -> Response: + return JSONResponse({'registered': True}, status_code=418) + + async def dependency(request: Request) -> None: + handlers, _ = request.scope['starlette.exception_handlers'] + handlers[RequestValidationError] = custom + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + app = application(router) + + @app.get('/native') + async def native(count: int) -> None: + pass + + with TestClient(app) as client: + assert client.post('/items', json=[{'count': 'bad'}]).status_code == 418 + assert client.get('/native', params={'count': 'bad'}).status_code == 418 + + +@pytest.mark.parametrize( + 'details', + ['custom', 'decimal', 'bytes', 'datetime', 'nan', 'infinity', 'float', 'int_subclass'], +) +def test_dependency_enriched_body_errors_use_msgspec_encoding(details: str) -> None: + import datetime + from decimal import Decimal + + class Details: + def __init__(self) -> None: + self.reason = 'extra' + + class Number(int): + pass + + calls = [] + + def forbidden(*args: Any, **kwargs: Any) -> Any: + calls.append('serialized') + return {'private': 'must-not-leak'} + + Details.model_dump = forbidden + Details.model_dump_json = forbidden + value = { + 'custom': Details(), + 'decimal': Decimal('1.2500'), + 'bytes': b'hello', + 'datetime': datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + 'nan': float('nan'), + 'infinity': float('inf'), + 'float': 1e-7, + 'int_subclass': Number(1), + }[details] + + def build(wrapped: bool) -> FastAPI: + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + exc.errors()[0]['ctx'] = {'details': value} + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + app = application(router) + if wrapped: + + @app.exception_handler(RequestValidationError) + async def handler(request: Request, exc: RequestValidationError) -> Response: + return await request_validation_exception_handler(request, exc) + + return app + + native = TestClient(build(True), raise_server_exceptions=False).post( + '/items', json=[{'count': 'bad'}] + ) + actual = TestClient(build(False), raise_server_exceptions=False).post( + '/items', json=[{'count': 'bad'}] + ) + assert native.status_code == (500 if details in ('nan', 'infinity') else 422) + if details in ('custom', 'int_subclass'): + assert actual.status_code == 500 + assert 'must-not-leak' not in actual.text + else: + expected = { + 'decimal': '1.2500', + 'bytes': 'aGVsbG8=', + 'datetime': '2026-01-01T00:00:00Z', + 'nan': None, + 'infinity': None, + 'float': 1e-7, + }[details] + assert actual.status_code == 422 + assert actual.json() == { + 'detail': [ + { + 'loc': ['body', 0, 'count'], + 'msg': 'Expected `int`, got `str`', + 'type': 'value_error.msgspec', + 'ctx': {'details': expected}, + } + ] + } + assert calls == [] + + +@pytest.mark.parametrize('canonical', [False, True]) +def test_dependency_error_method_is_not_repeated(canonical: bool) -> None: + calls = [] + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + errors = exc.errors() + if not canonical: + errors[0]['ctx'] = {'details': {'reason': 'extra'}} + + def changed_errors() -> list[Any]: + calls.append('errors') + return errors + + exc.errors = changed_errors + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == ['errors'] + + +def test_rebound_native_error_method_keeps_original_receiver() -> None: + from types import MethodType + + calls = [] + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + errors = exc.errors() + errors[0]['ctx'] = {'details': {'reason': 'extra'}} + + class Provider: + @property + def _errors(self) -> list[Any]: + calls.append('errors') + if len(calls) > 1: + raise RuntimeError('Provider repeated') + return errors + + exc.errors = MethodType(RequestValidationError.errors, Provider()) + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == ['errors'] + + +def test_custom_error_descriptor_is_not_repeated(monkeypatch: pytest.MonkeyPatch) -> None: + calls = [] + native_errors = RequestValidationError.errors + + def changed_errors(exc: RequestValidationError) -> Callable[..., Any]: + calls.append('get') + if len(calls) > 1: + raise RuntimeError('Descriptor repeated') + return lambda: native_errors(exc) + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError: + monkeypatch.setattr(RequestValidationError, 'errors', property(changed_errors)) + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == ['get'] + + +def test_custom_error_location_does_not_repeat_metaclass_comparisons() -> None: + calls = [] + + class Meta(type): + __hash__ = type.__hash__ + + def __eq__(cls, other: Any) -> bool: + calls.append('eq') + return False + + class Location(metaclass=Meta): + def __init__(self) -> None: + self.reason = 'extra' + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + exc.errors()[0]['loc'] = Location() + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router), raise_server_exceptions=False).post( + '/items', json=[{'count': 'bad'}] + ) + assert response.status_code == 500 + assert 'extra' not in response.text + assert calls == [] + + +def test_custom_exception_namespace_is_not_iterated() -> None: + calls = [] + + class Namespace(dict): + def __iter__(self) -> Any: + calls.append('iter') + raise RuntimeError('Namespace iteration') + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + exc.__dict__ = Namespace(exc.__dict__) + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == [] + + +def test_body_error_serialization_does_not_repeat_body_reader( + monkeypatch: pytest.MonkeyPatch, +) -> None: + calls = [] + original = Request.body + + async def read(request: Request) -> bytes: + calls.append('read') + if len(calls) > 1: + raise RuntimeError('Body reader repeated') + return await original(request) + + monkeypatch.setattr(Request, 'body', read) + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == ['read'] + + +def test_custom_body_bytes_do_not_receive_an_added_truth_check( + monkeypatch: pytest.MonkeyPatch, +) -> None: + calls = [] + original = Request.body + + class CustomBytes(bytes): + def __bool__(self) -> bool: + calls.append('truth') + if len(calls) > 1: + raise RuntimeError('Body truth check repeated') + return True + + async def read(request: Request) -> bytes: + return CustomBytes(await original(request)) + + monkeypatch.setattr(Request, 'body', read) + router = MsgspecRouter() + + @router.post('/items') + async def endpoint(items: list[Item]) -> None: + pass + + response = TestClient(application(router)).post('/items', json=[{'count': 'bad'}]) + assert response.status_code == 422 + assert calls == ['truth'] + + +@pytest.mark.parametrize('kind', ['model', 'secret']) +def test_default_error_encoding_never_extracts_pydantic_secrets(kind: str) -> None: + from pydantic import BaseModel, SecretStr + + calls = [] + + class PrivateModel(BaseModel): + secret: str + + def model_dump(self, *args: Any, **kwargs: Any) -> Any: + calls.append('model_dump') + return {'secret': self.secret} + + def model_dump_json(self, *args: Any, **kwargs: Any) -> str: + calls.append('model_dump_json') + return self.secret + + class PrivateSecret(SecretStr): + def get_secret_value(self) -> str: + calls.append('secret') + return super().get_secret_value() + + def __str__(self) -> str: + calls.append('str') + return super().__str__() + + value = ( + PrivateModel(secret='must-not-leak') if kind == 'model' else PrivateSecret('must-not-leak') + ) + + async def dependency() -> AsyncIterator[None]: + try: + yield + except RequestValidationError as exc: + exc.errors()[0]['ctx'] = {'details': value} + raise + + router = MsgspecRouter() + + @router.post('/items', dependencies=[Depends(dependency)]) + async def endpoint(items: list[Item]) -> None: + pass + + calls.clear() + response = TestClient(application(router), raise_server_exceptions=False).post( + '/items', json=[{'count': 'bad'}] + ) + assert response.status_code == 500 + assert calls == [] + assert 'must-not-leak' not in response.text diff --git a/tests/test_instance_builtin_codec.py b/tests/test_instance_builtin_codec.py new file mode 100644 index 0000000..92719fd --- /dev/null +++ b/tests/test_instance_builtin_codec.py @@ -0,0 +1,41 @@ +import copy +import dataclasses +import sys +import types +from typing import Any + +import pytest +from pydantic_core import SchemaValidator, core_schema + +from msgspec_fastapi.defaults import copy_body_default, copy_metadata + + +@pytest.mark.parametrize('operation', [copy_body_default, copy_metadata]) +def test_record_instance_builtin_copier_does_not_invoke_pydantic_codec(operation: Any) -> None: + @dataclasses.dataclass + class Record: + value: int + + validator = SchemaValidator(core_schema.any_schema()) + copier = validator.validate_python + assert type(copier) is types.BuiltinFunctionType + original = Record(7) + original.__deepcopy__ = copier + calls = [] + + def observe(frame: Any, event: str, arg: Any) -> None: + if event == 'c_call' and getattr(arg, '__self__', None) is validator: + if getattr(arg, '__name__', None) == 'validate_python': + calls.append('validate_python') + + previous = sys.getprofile() + try: + sys.setprofile(observe) + copy.deepcopy(original) + assert calls == ['validate_python'] + calls.clear() + with pytest.raises(TypeError, match='Instance copy methods'): + operation(original) + finally: + sys.setprofile(previous) + assert calls == [] diff --git a/tests/test_metadata_copy.py b/tests/test_metadata_copy.py new file mode 100644 index 0000000..665887d --- /dev/null +++ b/tests/test_metadata_copy.py @@ -0,0 +1,118 @@ +"""Copied route metadata must not trigger model data processing.""" + +import json +import sys +from typing import Annotated, Any, List, Optional, Union + +import msgspec +import pytest +from fastapi import Body, FastAPI +from fastapi.exceptions import FastAPIDeprecationWarning +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter, install_openapi + + +class Failure(BaseModel): + detail: str + + +def model_copies(frame: Any, event: str, arg: Any, calls: list[str]) -> None: + if ( + event == 'call' + and frame.f_code.co_filename.endswith('/pydantic/main.py') + and frame.f_code.co_name == '__deepcopy__' + ): + calls.append('model copy') + + +@pytest.mark.parametrize( + 'where', + [ + 'responses', + 'openapi_extra', + 'body_examples', + 'body_examples_callback', + 'body_example', + 'body_openapi_examples', + 'body_schema_dict', + ], +) +def test_metadata_model_values_are_rejected_before_copy(where: str) -> None: + value = Failure(detail='private') + router = MsgspecRouter() + calls = [] + previous = sys.getprofile() + + class Payload(msgspec.Struct): + value: int + + def customize(schema: dict[str, Any]) -> None: + schema['description'] = 'Example' + + async def ordinary() -> None: + pass + + body_options = { + 'body_examples': {'examples': [value]}, + 'body_examples_callback': {'examples': [value], 'json_schema_extra': customize}, + 'body_example': {'example': value}, + 'body_openapi_examples': {'openapi_examples': {'sample': {'value': value}}}, + 'body_schema_dict': {'json_schema_extra': {'examples': [value]}}, + }.get(where, {}) + if where == 'body_example': + with pytest.warns(FastAPIDeprecationWarning, match='deprecated'): + info = Body(**body_options) + else: + info = Body(**body_options) + annotation = Annotated[Payload, info] + + async def body(payload: annotation) -> None: + pass + + kwargs = { + 'responses': {'responses': {400: {'content': {'application/json': {'example': value}}}}}, + 'openapi_extra': {'openapi_extra': {'x-example': {'items': [value]}}}, + }.get(where, {}) + try: + + def observe(frame: Any, event: str, arg: Any) -> None: + model_copies(frame, event, arg, calls) + + sys.setprofile(observe) + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + router.add_api_route( + '/value', body if where.startswith('body_') else ordinary, **kwargs + ) + finally: + sys.setprofile(previous) + assert calls == [] + + +@pytest.mark.parametrize( + 'shape', ['direct', 'list', 'union', 'typing_list', 'typing_union', 'typing_optional'] +) +def test_native_schema_type_references_remain_available(shape: str) -> None: + annotation = { + 'direct': Failure, + 'list': list[Failure], + 'union': Failure | None, + 'typing_list': List[Failure], + 'typing_union': Union[Failure, int], + 'typing_optional': Optional[Failure], + }[shape] + router = MsgspecRouter() + + @router.get('/value', responses={400: {'model': annotation}}) + async def endpoint() -> None: + pass + + app = FastAPI() + app.include_router(router) + install_openapi(app) + document = app.openapi() + assert document['components']['schemas']['Failure']['properties']['detail']['type'] == 'string' + schema = document['paths']['/value']['get']['responses']['400']['content']['application/json'][ + 'schema' + ] + assert '#/components/schemas/Failure' in json.dumps(schema) diff --git a/tests/test_native_copier.py b/tests/test_native_copier.py new file mode 100644 index 0000000..520d13f --- /dev/null +++ b/tests/test_native_copier.py @@ -0,0 +1,561 @@ +import copy +import copyreg +import dataclasses +import gc +import inspect +import sys +import types +import weakref +from typing import Any + +import msgspec +import pytest + +from msgspec_fastapi._defaults import _Copier +from msgspec_fastapi.defaults import deepcopy as accelerated + + +def make_copier(**overrides: Any) -> Any: + values = dict( + copy_module=copy, + deepcopy=copy.deepcopy, + list_copier=copy._deepcopy_list, + dict_copier=copy._deepcopy_dict, + tuple_copier=copy._deepcopy_tuple, + atomic_copier=getattr(copy, '_deepcopy_atomic', None), + reconstruct=copy._reconstruct, + atomic_first=sys.version_info >= (3, 14), + memo_sentinel=copy.deepcopy.__defaults__[-1], + ) + values.update(overrides) + return _Copier(**values) + + +def test_named_signatures_and_gc_tracking() -> None: + assert len(inspect.signature(_Copier).parameters) == 9 + assert str(inspect.signature(make_copier().deepcopy)) == '(value, memo=None, /)' + assert gc.is_tracked(make_copier()) + + +def test_uninitialized_instance_and_retry() -> None: + empty = _Copier.__new__(_Copier) + with pytest.raises(TypeError, match='not initialized'): + empty(1) + with pytest.raises(TypeError, match='not initialized'): + empty.deepcopy(1) + with pytest.raises(TypeError): + empty.__init__() + with pytest.raises(TypeError): + make_copier().__init__() + assert make_copier()(42) == 42 + + +@pytest.mark.parametrize( + 'field,value', + [ + ('copy_module', {}), + ('deepcopy', None), + ('list_copier', None), + ('dict_copier', None), + ('tuple_copier', None), + ('atomic_copier', 0), + ('reconstruct', None), + ('atomic_first', 1), + ], +) +def test_malformed_configuration_is_rejected(field: Any, value: Any) -> None: + with pytest.raises(TypeError): + make_copier(**{field: value}) + + +def test_fallback_globals_cycle_is_collected() -> None: + + def template(value: Any, memo: Any) -> Any: + return value + + namespace = {} + callback = types.FunctionType(template.__code__, namespace) + reference = weakref.ref(callback) + owner = make_copier(deepcopy=callback) + namespace['owner'] = owner + del namespace, callback, owner + gc.collect() + assert reference() is None + + +def test_owned_callable_policy_is_not_borrowed() -> None: + + class Function: + def __call__(self, *args: Any) -> Any: + return copy.deepcopy(*args) + + callback = Function() + reference = weakref.ref(callback) + owner = make_copier(deepcopy=callback) + del callback + gc.collect() + assert reference() is not None + del owner + gc.collect() + assert reference() is None + + +def test_owned_memo_sentinel_is_not_borrowed() -> None: + + class Marker: + pass + + marker = Marker() + reference = weakref.ref(marker) + owner = make_copier(memo_sentinel=marker) + del marker + gc.collect() + assert reference() is not None + del owner + gc.collect() + assert reference() is None + + +def test_nonexact_memo_delegates_before_observable_lookups() -> None: + + def run(operation: Any) -> Any: + events = [] + + class Memo(dict): + def get(self, key: Any, default: Any = None) -> Any: + events.append('get') + return super().get(key, default) + + return (operation([1], Memo()), events) + + assert run(make_copier()) == run(copy.deepcopy) + + +@pytest.mark.parametrize('value', [None, False, 42, 1.5, 'text', b'text']) +def test_existing_atomic_memo(value: Any) -> None: + assert accelerated(value, {id(value): 'memo'}) == copy.deepcopy(value, {id(value): 'memo'}) + + +def test_aliases_mutable_and_tuple_cycles() -> None: + leaf = [] + pair = (leaf,) + leaf.extend([pair, leaf]) + original = [pair, leaf, pair] + out = accelerated(original) + assert out[0] is out[2] + assert out[0][0] is out[1] + assert out[1][0] is out[0] and out[1][1] is out[1] + + +def test_tuple_second_memo_read() -> None: + + class Child: + def __deepcopy__(self, memo: Any) -> Any: + memo[id(root)] = 'memo tuple' + return self + + root = (Child(),) + assert accelerated(root) == copy.deepcopy(root) == 'memo tuple' + + +def run_dict_order(fn: Any, mutate: Any = False) -> Any: + calls = [] + + class Key: + def __hash__(self) -> Any: + return 0 + + def __deepcopy__(self, memo: Any) -> Any: + calls.append('key') + return self + + class Value: + def __deepcopy__(self, memo: Any) -> Any: + calls.append('value') + if mutate: + original['new'] = 1 + return self + + original = {Key(): Value()} + try: + fn(original) + except Exception as exc: + return (type(exc), str(exc), calls) + return calls + + +def test_dict_value_before_key() -> None: + assert run_dict_order(accelerated) == run_dict_order(copy.deepcopy) == ['value', 'key'] + + +def test_live_dict_iterator_mutation() -> None: + assert run_dict_order(accelerated, True) == run_dict_order(copy.deepcopy, True) + + +def test_live_list_iterator_mutation() -> None: + + def run(fn: Any) -> Any: + + class Value: + def __deepcopy__(self, memo: Any) -> Any: + original.append('appended') + return 'copied' + + original = [Value()] + return fn(original) + + assert run(accelerated) == run(copy.deepcopy) == ['copied', 'appended'] + + +def test_keepalive_bucket_callback() -> None: + + def run(fn: Any) -> Any: + calls = [] + + class Bucket: + def append(self, value: Any) -> Any: + calls.append(type(value).__name__) + + class Value: + def __deepcopy__(self, memo: Any) -> Any: + memo[id(memo)] = Bucket() + return 'copied' + + result = fn([Value(), [1]]) + return (result, calls) + + assert run(accelerated) == run(copy.deepcopy) + + +def test_keepalive_append_keyerror_matches_stdlib() -> None: + + def run(fn: Any) -> Any: + calls = [] + + class Bucket: + def append(self, value: Any) -> Any: + calls.append(type(value).__name__) + raise KeyError('append') + + class Value(msgspec.Struct): + x: int + + def __deepcopy__(self, memo: Any) -> Any: + memo[id(memo)] = Bucket() + return Value(7) + + memo = {} + result = fn(Value(1), memo) + return (result.x, calls, [type(x).__name__ for x in memo[id(memo)]]) + + assert run(accelerated) == run(copy.deepcopy) + + +def test_late_struct_copy_hook() -> None: + + def run(fn: Any) -> Any: + calls = [] + active = False + + class Value(msgspec.Struct): + x: int + + def __post_init__(self) -> Any: + if active and self.x == 1: + + def copied(self: Any, memo: Any) -> Any: + calls.append('copy') + return Value(self.x + 10) + + Value.__deepcopy__ = copied + + original = [Value(1), Value(2)] + del Value.__post_init__ + active = True + return ([x.x for x in fn(original)], calls) + + assert run(accelerated) == run(copy.deepcopy) == ([1, 12], ['copy']) + + +def test_kwonly_constructor_and_reduce_snapshot() -> None: + + def run(fn: Any) -> Any: + calls = [] + + class Child: + def __deepcopy__(self, memo: Any) -> Any: + original.b = 99 + return 'child' + + class Value(msgspec.Struct, kw_only=True): + a: object + b: int + + def __post_init__(self) -> Any: + calls.append((self.a, self.b)) + + original = Value(a=Child(), b=1) + calls.clear() + out = fn(original) + return (out.a, out.b, calls) + + assert run(accelerated) == run(copy.deepcopy) == ('child', 1, [('child', 1)]) + + +def test_dispatch_mutation_for_later_list() -> None: + + def run(fn: Any) -> Any: + old = copy._deepcopy_dispatch[list] + calls = [] + + class Value(msgspec.Struct): + x: int + + def __deepcopy__(self, memo: Any) -> Any: + + def changed(x: Any, memo: Any) -> Any: + calls.append('list') + return ['changed'] + + copy._deepcopy_dispatch[list] = changed + return self + + try: + result = fn([Value(1), [2]]) + return (result[1], calls) + finally: + copy._deepcopy_dispatch[list] = old + + assert run(accelerated) == run(copy.deepcopy) == (['changed'], ['list']) + + +def test_removed_list_dispatch_fallback_memo_count() -> None: + old = copy._deepcopy_dispatch.pop(list) + try: + + def run(fn: Any) -> Any: + value = [[1]] + memo = {} + result = fn(value, memo) + return (result, [type(x).__name__ for x in memo[id(memo)]]) + + assert run(accelerated) == run(copy.deepcopy) + finally: + copy._deepcopy_dispatch[list] = old + + +def test_struct_late_reduce_and_registration() -> None: + + def run(fn: Any) -> Any: + active = False + calls = [] + + class Value(msgspec.Struct): + x: int + + def __post_init__(self) -> Any: + if active and self.x == 1: + + def reduced(x: Any) -> Any: + calls.append('reduce') + return (Value, (x.x + 10,)) + + copyreg.dispatch_table[Value] = reduced + + original = [Value(1), Value(2)] + active = True + try: + return ([x.x for x in fn(original)], calls) + finally: + copyreg.dispatch_table.pop(Value, None) + + assert run(accelerated) == run(copy.deepcopy) == ([1, 12], ['reduce']) + + +def test_struct_nonstandard_reduce_state_delegation() -> None: + + def run(fn: Any) -> Any: + calls = [] + + class Value(msgspec.Struct, dict=True): + x: int + + def __reduce_ex__(self, protocol: Any) -> Any: + calls.append(protocol) + return (Value, (self.x,), {'extra': [1]}) + + result = fn(Value(2)) + return (result.x, result.extra, calls) + + assert run(accelerated) == run(copy.deepcopy) == (2, [1], [4]) + + +def test_memo_identity_from_user_hook() -> None: + + class Value(msgspec.Struct): + x: int + + def __deepcopy__(self, memo: Any) -> Any: + assert memo is expected + return self + + expected = {} + accelerated([Value(1)], expected) + + +def test_struct_return_original_reconstruction_still_memoizes() -> None: + + class Value(msgspec.Struct): + x: int + + def __reduce_ex__(self, protocol: Any) -> Any: + return (lambda: original, ()) + + original = Value(1) + for fn in [copy.deepcopy, accelerated]: + memo = {} + assert fn(original, memo) is original + assert memo[id(original)] is original + + +def test_reentrant_copy_preserves_outer_memo() -> None: + outer = {} + + class Value(msgspec.Struct): + x: int + + def __deepcopy__(self, memo: Any) -> Any: + assert memo is outer + assert accelerated([42]) == [42] + assert memo is outer + return Value(self.x + 1) + + assert accelerated([Value(1), 42], outer)[0].x == 2 + + +def test_escaped_child_callback_is_safe_and_does_not_retain_root() -> None: + captured = [] + + @dataclasses.dataclass + class Value: + x: int + + def __reduce_ex__(self, protocol: int) -> Any: + return object.__reduce_ex__(self, protocol)[:4] + + def __setstate__(self, state: Any) -> Any: + captured.append(inspect.currentframe().f_back.f_locals['deepcopy']) + self.__dict__.update(state) + + original = Value(7) + reference = weakref.ref(original) + result = accelerated(original) + assert result == original and len(captured) == 1 + assert captured[0]([1, 2], {}) == [1, 2] + del original, result + gc.collect() + assert reference() is None + + +def test_reconstruct_replacement_is_positional_without_reduce_replay() -> None: + + def run(fn: Any) -> Any: + original = copy._reconstruct + calls = [] + + class Value(msgspec.Struct): + x: int + + def __reduce_ex__(self, protocol: Any) -> Any: + calls.append(('reduce', protocol)) + return (Value, (self.x,)) + + def replacement(x: Any, memo: Any, *rv: Any) -> Any: + calls.append(('helper', len(rv))) + return original(x, memo, *rv) + + copy._reconstruct = replacement + try: + return (fn(Value(7)).x, calls) + finally: + copy._reconstruct = original + + assert run(accelerated) == run(copy.deepcopy) == (7, [('reduce', 4), ('helper', 2)]) + + +def test_constructor_can_replace_helper_for_later_item() -> None: + + def run(fn: Any) -> Any: + original = copy._reconstruct + calls = [] + active = False + + def replacement(x: Any, memo: Any, *rv: Any) -> Any: + calls.append(x.x) + return original(x, memo, *rv) + + class Value(msgspec.Struct): + x: int + + def __post_init__(self) -> Any: + if active and self.x == 1: + copy._reconstruct = replacement + + values = [Value(1), Value(2)] + active = True + try: + return ([x.x for x in fn(values)], calls) + finally: + copy._reconstruct = original + + assert run(accelerated) == run(copy.deepcopy) == ([1, 2], [2]) + + +def test_missing_reducers_copy_error_type() -> None: + + class Value(msgspec.Struct): + x: int + __reduce_ex__ = None + __reduce__ = None + + for fn in (accelerated, copy.deepcopy): + with pytest.raises(copy.Error): + fn(Value(1)) + + +def test_initial_id_key_is_reused_for_outer_memo_assignment() -> None: + + def run(operation: Any) -> Any: + comparisons = [] + + @dataclasses.dataclass + class Value: + x: int + + original = Value(7) + + class Key: + def __hash__(self) -> Any: + return id(original) + + def __eq__(self, other: Any) -> Any: + comparisons.append(other) + return False + + result = operation(original, {Key(): None}) + assert len(comparisons) >= 3 + return (result.x, comparisons[0] is comparisons[-1]) + + assert run(accelerated) == run(copy.deepcopy) == (7, True) + + +def test_deep_recursion_guard_does_not_crash() -> None: + root = [] + current = root + for _ in range(100000): + child = [] + current.append(child) + current = child + for fn in [copy.deepcopy, accelerated]: + with pytest.raises(RecursionError): + fn(root) diff --git a/tests/test_native_guard.py b/tests/test_native_guard.py new file mode 100644 index 0000000..a2de5d1 --- /dev/null +++ b/tests/test_native_guard.py @@ -0,0 +1,224 @@ +"""The native guard owns policy references and preserves the reference checks.""" + +import copyreg +import dataclasses +import gc +import inspect +import types +import weakref +from typing import Any + +import msgspec +import pytest + +from msgspec_fastapi import defaults +from msgspec_fastapi._defaults import _Guard + + +def guard_arguments(fallback: Any = defaults._check_copy_graph_python) -> dict[str, Any]: + return { + 'fallback': fallback, + 'registry': copyreg.dispatch_table, + 'trusted_union_reducer': defaults._TRUSTED_UNION_REDUCER, + 'union_type': types.UnionType, + 'function_type': types.FunctionType, + 'builtin_function_type': types.BuiltinFunctionType, + 'getset_descriptor_type': types.GetSetDescriptorType, + 'member_descriptor_type': types.MemberDescriptorType, + 'method_descriptor_type': types.MethodDescriptorType, + 'wrapper_descriptor_type': types.WrapperDescriptorType, + 'struct_type': msgspec.Struct, + 'enum_type': defaults.enum.Enum, + 'atomic_ids': defaults._ATOMIC_IDS, + 'reference_ids': defaults._TYPE_REFERENCE_IDS, + 'model_brands': defaults._MODEL_BRANDS, + 'native_state_methods': defaults._NATIVE_STATE_METHODS, + 'state_names': defaults._STATE_NAMES, + 'enum_namespace': defaults._ENUM_NAMESPACE, + 'missing': defaults._MISSING_STATE_METHOD, + 'mro_descriptor': type.__dict__['__mro__'], + 'dict_descriptor': type.__dict__['__dict__'], + 'name_descriptor': type.__dict__['__name__'], + } + + +def test_native_signatures_match_named_policy_interface() -> None: + arguments = guard_arguments() + assert tuple(inspect.signature(_Guard).parameters) == tuple(arguments) + guard = _Guard(**arguments) + assert str(inspect.signature(guard.check)) == '(value, /)' + assert str(inspect.signature(guard.class_info)) == '(cls, /)' + assert gc.is_tracked(guard) + + +def test_native_uninitialized_and_reinitialization_are_safe() -> None: + guard = _Guard.__new__(_Guard) + for operation in (guard.check, guard.class_info): + with pytest.raises(TypeError, match='Uninitialized'): + operation(int) + bad = {**guard_arguments(), 'model_brands': []} + with pytest.raises(TypeError): + guard.__init__(**bad) + with pytest.raises(TypeError, match='Uninitialized'): + guard.check(None) + guard.__init__(**guard_arguments()) + assert guard.check({'items': [1, 2]}) is None + with pytest.raises(TypeError, match='reinitialized'): + guard.__init__(**guard_arguments()) + + +def test_native_fallback_globals_cycle_is_collectable() -> None: + namespace: dict[str, Any] = {} + exec('def fallback(value):\n return None\n', namespace) + reference = weakref.ref(namespace['fallback']) + guard = _Guard(**guard_arguments(namespace['fallback'])) + namespace['guard'] = guard + del namespace, guard + gc.collect() + assert reference() is None + + +def test_native_context_retains_and_releases_policy_type() -> None: + temporary = type('TemporaryPolicyType', (), {}) + reference = weakref.ref(temporary) + arguments = {**guard_arguments(), 'struct_type': temporary} + guard = _Guard(**arguments) + del temporary, arguments + gc.collect() + assert reference() is not None + del guard + gc.collect() + assert reference() is None + + +@pytest.mark.parametrize( + ('key', 'value'), + [ + ('fallback', None), + ('registry', []), + ('function_type', 1), + ('atomic_ids', {True}), + ('reference_ids', [1]), + ('model_brands', ('valid', 1)), + ('native_state_methods', (('bad', []),)), + ('state_names', []), + ('mro_descriptor', None), + ('dict_descriptor', None), + ('name_descriptor', None), + ('enum_namespace', None), + ], +) +def test_invalid_native_context_configuration_raises(key: str, value: Any) -> None: + with pytest.raises(TypeError): + _Guard(**{**guard_arguments(), key: value}) + + +def test_native_bad_descriptor_result_raises_without_partial_state_leak() -> None: + arguments = {**guard_arguments(), 'mro_descriptor': type.__dict__['__dict__']} + guard = _Guard(**arguments) + for _ in range(10): + with pytest.raises(TypeError, match='MRO'): + guard.class_info(int) + + +def test_native_guard_and_reference_reject_same_graphs() -> None: + @dataclasses.dataclass + class Record: + value: Any + + class Slotted(Record): + __slots__ = ('extra',) + + value = Slotted({'items': [1, 2]}) + guard = _Guard(**guard_arguments()) + assert guard.check(value) is None + assert defaults._check_copy_graph_python(value) is None + shared: list[Any] = [] + cycle = [shared, shared] + shared.append(cycle) + assert guard.check(cycle) is None + assert defaults._check_copy_graph_python(cycle) is None + value.extra = object() + for operation in (guard.check, defaults._check_copy_graph_python): + with pytest.raises(TypeError, match='Unsupported object'): + operation(value) + + +def test_native_fallback_error_does_not_retain_data() -> None: + class Unsupported: + pass + + value = Unsupported() + reference = weakref.ref(value) + + def fallback(item: Any) -> None: + raise ValueError('fallback failure') + + guard = _Guard(**guard_arguments(fallback)) + for _ in range(10): + # A path is a supported opaque value handled by the Python reference guard. + from pathlib import Path + + with pytest.raises(ValueError, match='fallback failure'): + guard.check(Path('/value')) + with pytest.raises(TypeError, match='Unsupported object'): + guard.check(value) + del value + gc.collect() + assert reference() is None + + +def test_reentrant_namespace_initialization_does_not_retain_old_cache() -> None: + @dataclasses.dataclass + class Outer: + value: int + + @dataclasses.dataclass + class Inner: + value: int + + class Marker: + pass + + def namespace(owner: Any) -> Any: + for item in gc.get_referents(owner): + if ( + type(item) is dict + and '__subclasshook__' in item + and '__reduce_ex__' in item + and '__doc__' in item + ): + return item + return None + + original_threshold = gc.get_threshold() + for offset in range(1, 35): + owner = _Guard(**guard_arguments()) + active = False + called = False + watched = [] + + def collect(phase: str, info: Any, owner: Any = owner) -> None: + nonlocal called + if phase == 'start' and active and not called and namespace(owner) is None: + called = True + owner.class_info(Inner) + value = namespace(owner) + if value is not None: + marker = Marker() + watched.append(weakref.ref(marker)) + value['_gc_lifetime_probe'] = marker + + gc.collect() + gc.callbacks.append(collect) + gc.set_threshold(offset, 0, 0) + active = True + try: + owner.class_info(Outer) + finally: + active = False + gc.set_threshold(*original_threshold) + gc.callbacks.remove(collect) + del owner, collect + gc.collect() + assert all(reference() is None for reference in watched) diff --git a/tests/test_native_serialization.py b/tests/test_native_serialization.py new file mode 100644 index 0000000..1fcce32 --- /dev/null +++ b/tests/test_native_serialization.py @@ -0,0 +1,135 @@ +"""The msgspec data paths must not fall back to Pydantic serialization.""" + +from collections.abc import AsyncIterator +from pathlib import Path +from typing import Annotated, Any + +import fastapi.routing +import msgspec +import pytest +from fastapi import APIRouter, Body, Depends, FastAPI, Query, Response +from fastapi.responses import JSONResponse, PlainTextResponse +from fastapi.sse import EventSourceResponse +from fastapi.testclient import TestClient +from pydantic import BaseModel, TypeAdapter + +from msgspec_fastapi import MsgspecJSONResponse, MsgspecRouter, install_openapi + + +def app_for(router: APIRouter) -> FastAPI: + app = FastAPI() + app.include_router(router) + if isinstance(router, MsgspecRouter): + install_openapi(app) + return app + + +@pytest.mark.parametrize('response_class', [MsgspecJSONResponse, JSONResponse, PlainTextResponse]) +@pytest.mark.parametrize('annotation', [None, Response]) +def test_untyped_custom_responses_never_serialize_models( + response_class: type[Response], annotation: Any +) -> None: + calls = [] + + class Private(BaseModel): + secret: str + + def model_dump(self, *args: Any, **kwargs: Any) -> Any: + calls.append('dump') + return {'secret': self.secret} + + value = Private(secret='must-not-be-serialized') + router = MsgspecRouter() + + async def endpoint(): # noqa: ANN202 - Missing annotation is part of this boundary. + return value + + if annotation is not None: + endpoint.__annotations__['return'] = annotation + router.get('/value', response_class=response_class)(endpoint) + result = TestClient(app_for(router), raise_server_exceptions=False).get('/value') + assert result.status_code == 500 + assert calls == [] + assert 'must-not-be-serialized' not in result.text + + +def test_untyped_selected_json_class_keeps_constructor_and_stdlib_encoding() -> None: + class Selected(JSONResponse): + def __init__(self, content: Any, **kwargs: Any) -> None: + super().__init__(content, **kwargs) + self.headers['x-selected'] = 'yes' + + router = MsgspecRouter() + + @router.get('/value', response_class=Selected, response_model=None) + async def endpoint(): # noqa: ANN202 - Exercise the untyped custom response path. + return {'path': Path('/tmp/value')} + + result = TestClient(app_for(router)).get('/value') + assert result.json() == {'path': '/tmp/value'} + assert result.headers['x-selected'] == 'yes' + + +def test_data_paths_work_when_pydantic_runtime_entrypoints_are_forbidden( + monkeypatch: pytest.MonkeyPatch, +) -> None: + class Payload(msgspec.Struct): + value: int + + router = MsgspecRouter() + + @router.post('/value') + async def value(body: Payload, count: Annotated[int, Query(gt=0)] = 1) -> Payload: + return Payload(body.value * count) + + @router.post('/jsonl') + async def jsonl(body: Annotated[Payload, Body()]) -> AsyncIterator[Payload]: + yield body + + @router.get('/sse', response_class=EventSourceResponse) + async def sse() -> AsyncIterator[Payload]: + yield Payload(7) + + native = APIRouter() + + @native.get('/value') + async def control() -> dict[str, int]: + return {'value': 1} + + app = app_for(router) + control_app = app_for(native) + assert TestClient(control_app).get('/value').json() == {'value': 1} + assert '/jsonl' in app.openapi()['paths'] + + def forbidden(*args: Any, **kwargs: Any) -> Any: + raise RuntimeError('A Pydantic runtime or native JSON serializer was invoked') + + for method in ('validate_python', 'validate_json', 'dump_python', 'dump_json'): + monkeypatch.setattr(TypeAdapter, method, forbidden) + for method in ('model_dump', 'model_dump_json'): + monkeypatch.setattr(BaseModel, method, forbidden) + monkeypatch.setattr(fastapi.routing, 'jsonable_encoder', forbidden) + client = TestClient(app) + assert client.post('/value?count=2', json={'value': 3}).json() == {'value': 6} + invalid = client.post('/value', json={'value': 'wrong'}) + assert invalid.status_code == 422 + assert invalid.json()['detail'][0]['loc'] == ['body', 'value'] + assert client.post('/value?count=0', json={'value': 3}).status_code == 422 + assert client.post('/jsonl', json={'value': 4}).content == b'{"value":4}\n' + assert client.get('/sse').content == b'data: {"value":7}\n\n' + with pytest.raises(RuntimeError, match='Pydantic runtime'): + TestClient(control_app).get('/value') + + +def test_dependency_json_bodies_cannot_use_native_validation() -> None: + async def dependency(value: Annotated[int, Body()]) -> int: + return value + + router = MsgspecRouter() + + async def endpoint(value: Annotated[int, Depends(dependency)]) -> int: + return value + + with pytest.raises(TypeError, match='JSON body fields must use the route msgspec decoder'): + router.post('/value')(endpoint) + app_for(router) diff --git a/tests/test_native_type_contract.py b/tests/test_native_type_contract.py new file mode 100644 index 0000000..7727c30 --- /dev/null +++ b/tests/test_native_type_contract.py @@ -0,0 +1,232 @@ +"""Declared data must remain decodable without external model behavior.""" + +import dataclasses +from typing import Annotated, Any, Generic, NamedTuple, TypeVar + +import msgspec +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel, model_validator +from pydantic.dataclasses import dataclass as validated_dataclass +from typing_extensions import TypedDict + +from msgspec_fastapi import MsgspecRouter + +T = TypeVar('T') + + +class Box(msgspec.Struct, Generic[T]): + value: T + + +class Tree(msgspec.Struct): + value: int + children: list['Tree'] + + +@dataclasses.dataclass +class DataclassTree: + value: int + children: list['DataclassTree'] + + +class DictionaryTree(TypedDict): + value: int + children: list['DictionaryTree'] + + +def register_data(annotation: Any, position: str) -> None: + router = MsgspecRouter() + if position == 'body': + + async def endpoint(value: annotation) -> None: + raise AssertionError('Registration must not execute an endpoint') + + router.post('/value')(endpoint) + else: + + async def endpoint() -> annotation: + raise AssertionError('Registration must not execute an endpoint') + + router.get('/value')(endpoint) + + +@pytest.mark.parametrize('position', ['body', 'response']) +@pytest.mark.parametrize('shape', ['struct', 'dataclass', 'dictionary', 'tuple', 'generic']) +def test_schema_override_cannot_enable_custom_decoding(shape: str, position: str) -> None: + constructed = [] + + class External: + def __new__(cls) -> 'External': + constructed.append('instance') + return super().__new__(cls) + + opaque = Annotated[External, msgspec.Meta(extra_json_schema={'type': 'object'})] + if shape == 'struct': + annotation = msgspec.defstruct('Payload', [('value', opaque)]) + elif shape == 'dataclass': + annotation = dataclasses.make_dataclass('Payload', [('value', opaque)]) + elif shape == 'dictionary': + annotation = TypedDict('Payload', {'value': opaque}) + elif shape == 'tuple': + annotation = NamedTuple('Payload', [('value', opaque)]) + else: + annotation = Box[list[opaque]] + with pytest.raises(TypeError): + register_data(annotation, position) + assert constructed == [] + + +@pytest.mark.parametrize('position', ['body', 'response']) +@pytest.mark.parametrize('shape', ['direct', 'list', 'generic']) +def test_validated_dataclass_is_rejected_without_bypassing_its_validator( + shape: str, position: str +) -> None: + calls = [] + + @validated_dataclass + class Validated: + value: int + + @model_validator(mode='after') + def positive(self) -> Any: + calls.append('validate') + if self.value < 0: + raise ValueError('negative value') + return self + + with pytest.raises(ValueError, match='negative value'): + Validated(-1) + assert calls == ['validate'] + calls.clear() + annotation = {'direct': Validated, 'list': list[Validated], 'generic': Box[Validated]}[shape] + with pytest.raises(TypeError): + register_data(annotation, position) + assert calls == [] + + +@pytest.mark.parametrize('shape', ['direct', 'list', 'struct']) +def test_model_rejection_precedes_dynamic_model_attribute_lookup(shape: str) -> None: + calls = [] + + class RecordingMeta(type(BaseModel)): + def __getattr__(cls, name: str) -> Any: + calls.append(name) + return super().__getattr__(name) + + class ExternalModel(BaseModel, metaclass=RecordingMeta): + value: int + + annotation = { + 'direct': ExternalModel, + 'list': list[ExternalModel], + 'struct': msgspec.defstruct('Payload', [('value', ExternalModel)]), + }[shape] + calls.clear() + with pytest.raises(TypeError): + register_data(annotation, 'response') + assert calls == [] + + +@pytest.mark.parametrize( + 'shape', + [ + 'dataclass', + 'dictionary', + 'tuple', + 'generic', + 'struct_tree', + 'dataclass_tree', + 'dictionary_tree', + ], +) +def test_native_nested_and_recursive_data_remains_supported(shape: str) -> None: + @dataclasses.dataclass + class Data: + value: int + + class Dictionary(TypedDict): + value: int + + class TupleData(NamedTuple): + value: int + + annotation = { + 'dataclass': Data, + 'dictionary': Dictionary, + 'tuple': TupleData, + 'generic': Box[int], + 'struct_tree': Tree, + 'dataclass_tree': DataclassTree, + 'dictionary_tree': DictionaryTree, + }[shape] + payload: Any = [7] if shape == 'tuple' else {'value': 7} + if shape.endswith('_tree'): + payload = {'value': 7, 'children': [{'value': 8, 'children': []}]} + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(value: annotation) -> annotation: + return value + + app = FastAPI() + app.include_router(router) + response = TestClient(app).post('/value', json=payload) + assert response.status_code == 200 + assert response.json() == payload + + +def test_registration_does_not_construct_dataclass_defaults() -> None: + calls = [] + + def default() -> int: + calls.append('default') + return 7 + + @dataclasses.dataclass + class Data: + value: int = dataclasses.field(default_factory=default) + + def __post_init__(self) -> None: + calls.append('post-init') + + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(value: Data) -> None: + pass + + app = FastAPI() + app.include_router(router) + assert calls == [] + response = TestClient(app).post('/value', json={}) + assert response.status_code == 200 + assert calls == ['default', 'post-init'] + + +@pytest.mark.parametrize('position', ['body', 'response']) +def test_legacy_validated_dataclass_does_not_bypass_its_validator(position: str) -> None: + from pydantic.v1 import validator + from pydantic.v1.dataclasses import dataclass as legacy_dataclass + + calls = [] + + @legacy_dataclass + class Legacy: + value: int + + @validator('value', allow_reuse=True) + def positive(cls, value: int) -> int: + calls.append('validate') + if value < 0: + raise ValueError('negative value') + return value + + with pytest.raises(ValueError, match='negative value'): + Legacy(-1) + assert calls == ['validate'] + calls.clear() + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + register_data(Legacy, position) + assert calls == [] diff --git a/tests/test_parameter_defaults.py b/tests/test_parameter_defaults.py new file mode 100644 index 0000000..b0ba0a2 --- /dev/null +++ b/tests/test_parameter_defaults.py @@ -0,0 +1,138 @@ +"""Parameter defaults and their factories must not trigger model copying.""" + +from typing import Annotated, Any + +import pytest +from fastapi import APIRouter, Cookie, Depends, FastAPI, Header, Query +from fastapi.testclient import TestClient +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter + + +@pytest.mark.parametrize('marker', [Query, Header, Cookie]) +@pytest.mark.parametrize('dependency', [False, True]) +@pytest.mark.parametrize('late', [False, True]) +def test_parameter_defaults_reject_models_before_copy( + marker: Any, dependency: bool, late: bool +) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model copy') + return super().__deepcopy__(memo) + + value = External(value=1) + default: Any = {'items': [] if late else [value]} + annotation = Annotated[Any, marker()] + router = MsgspecRouter() + + async def source(value: annotation = default) -> None: + pass + + if dependency: + + async def endpoint(value: Annotated[Any, Depends(source)]) -> None: + pass + else: + endpoint = source + + if late: + router.get('/value')(endpoint) + app = FastAPI() + app.include_router(router) + default['items'].append(value) + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + TestClient(app).get('/value') + else: + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + router.get('/value')(endpoint) + app = FastAPI() + app.include_router(router) + assert calls == [] + + +@pytest.mark.parametrize('marker', [Query, Header, Cookie]) +def test_parameter_factory_result_is_checked_before_copy(marker: Any) -> None: + calls = [] + factories = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model copy') + return super().__deepcopy__(memo) + + value = External(value=1) + + def factory() -> Any: + factories.append(True) + return {'items': [value]} + + annotation = Annotated[Any, marker(default_factory=factory)] + router = MsgspecRouter() + + @router.get('/value') + async def endpoint(value: annotation) -> None: + pass + + app = FastAPI() + app.include_router(router) + assert factories == [] + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + TestClient(app).get('/value') + assert factories == [True] and calls == [] + + +@pytest.mark.parametrize('marker', [Query, Header, Cookie]) +def test_supported_factory_keeps_native_evaluation_count(marker: Any) -> None: + def run(router_class: type[APIRouter]) -> tuple[int, int]: + calls = [] + + def factory() -> dict[str, Any]: + calls.append(True) + return {'items': [1, 2]} + + annotation = Annotated[Any, marker(default_factory=factory)] + router = router_class() + + @router.get('/value') + async def endpoint(value: annotation) -> int: + return len(value['items']) + + app = FastAPI() + app.include_router(router) + assert calls == [] + response = TestClient(app).get('/value') + assert response.status_code == 200 + return response.json(), len(calls) + + native = run(APIRouter) + assert native[0] == 2 and native[1] > 0 + assert run(MsgspecRouter) == native + + +@pytest.mark.parametrize('marker', [Query, Header, Cookie]) +def test_parameter_example_models_are_rejected_before_native_setup(marker: Any) -> None: + calls = [] + + class External(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model copy') + return super().__deepcopy__(memo) + + annotation = Annotated[int, marker(examples=[External(value=1)])] + router = MsgspecRouter() + + async def endpoint(value: annotation) -> None: + pass + + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + router.get('/value')(endpoint) + assert calls == [] diff --git a/tests/test_parameter_getter_copy.py b/tests/test_parameter_getter_copy.py new file mode 100644 index 0000000..f7ef116 --- /dev/null +++ b/tests/test_parameter_getter_copy.py @@ -0,0 +1,59 @@ +"""Copied parameter default getters retain the existing live metadata capture.""" + +from copy import copy, deepcopy +from typing import Annotated, Any + +import pytest +from fastapi import Query +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter + + +@pytest.mark.parametrize('copier', [copy, deepcopy]) +def test_copied_default_getter_retains_late_default_and_factory(copier: Any) -> None: + router = MsgspecRouter() + + @router.get('/value') + async def endpoint(value: Annotated[Any, Query()] = 7) -> None: + pass + + field = router.routes[0].dependant.query_params[0] + copied = copier(field.get_default) + field.field_info.default = [9] + value = copied() + assert value == [9] and value is not field.field_info.default + calls = [] + + def factory() -> list[int]: + calls.append('factory') + return [11] + + field.field_info.default_factory = factory + assert copied() == [11] + assert calls == ['factory'] + + +@pytest.mark.parametrize('copier', [copy, deepcopy]) +def test_copying_default_getter_does_not_copy_pydantic_default(copier: Any) -> None: + calls = [] + + class Model(BaseModel): + value: int + + def __deepcopy__(self, memo: Any = None) -> Any: + calls.append('model-copy') + return super().__deepcopy__(memo) + + router = MsgspecRouter() + + @router.get('/value') + async def endpoint(value: Annotated[Any, Query()] = 7) -> None: + pass + + field = router.routes[0].dependant.query_params[0] + field.field_info.default = Model(value=1) + copied = copier(field.get_default) + with pytest.raises(TypeError, match='Pydantic model behavior'): + copied() + assert calls == [] diff --git a/tests/test_request_response.py b/tests/test_request_response.py new file mode 100644 index 0000000..8e190f9 --- /dev/null +++ b/tests/test_request_response.py @@ -0,0 +1,1194 @@ +"""Request/response compatibility controls for improvement-plan step 3.""" + +import ipaddress +import json +import re +from collections.abc import AsyncIterator, Iterator +from decimal import Decimal +from pathlib import Path +from typing import Annotated, Any + +import msgspec +import pytest +from fastapi import APIRouter, Body, Depends, FastAPI, HTTPException, Query, Response +from fastapi.exceptions import ResponseValidationError +from fastapi.responses import JSONResponse, PlainTextResponse, StreamingResponse +from fastapi.sse import EventSourceResponse, ServerSentEvent +from fastapi.testclient import TestClient +from pydantic import BaseModel, Field, HttpUrl, SecretStr +from pydantic_core import SchemaError, SchemaValidator +from regex_rs import RegexError + +from msgspec_fastapi import MsgspecRouter, install_openapi + + +class Item(msgspec.Struct): + value: int + + +class PydItem(BaseModel): + value: int + + +def application(router: APIRouter) -> FastAPI: + app = FastAPI() + app.include_router(router) + if isinstance(router, MsgspecRouter): + install_openapi(app) + return app + + +def assert_sse_rejected( + app: FastAPI, message: str = 'Use ServerSentEvent(raw_data=...) for event data' +) -> None: + with pytest.raises((TypeError, ExceptionGroup)) as caught: + TestClient(app).get('/value') + + def leaves(error: BaseException) -> list[BaseException]: + if isinstance(error, BaseExceptionGroup): + return [leaf for child in error.exceptions for leaf in leaves(child)] + return [error] + + errors = leaves(caught.value) + assert len(errors) == 1 and isinstance(errors[0], TypeError) + assert str(errors[0]) == message + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('where', ['query', 'body', 'list', 'set', 'dict', 'nullable', 'tuple']) +def test_string_patterns_match_pydantic(router_class: type[APIRouter], where: str) -> None: + router = router_class() + pattern = Annotated[str, Field(pattern='^a+$')] + annotation = { + 'query': Annotated[str, Query(pattern='^a+$')], + 'body': Annotated[str, Body(pattern='^a+$')], + 'list': list[pattern], + 'set': set[pattern], + 'dict': dict[pattern, pattern], + 'nullable': Annotated[pattern | None, Body()], + 'tuple': tuple[pattern, pattern], + }[where] + + async def endpoint(value: annotation) -> Any: + return value + + router.add_api_route('/value', endpoint, methods=['POST']) + app = application(router) + client = TestClient(app) + if where == 'query': + result = client.post('/value', params={'value': 'a\n'}) + else: + invalid = { + 'body': 'a\n', + 'list': ['aa', 'a\n', 'bad'], + 'set': ['aa', 'bad', 'bad'], + 'dict': {'bad': 'a\n'}, + 'nullable': 'a\n', + 'tuple': ['aa', 'a\n'], + }[where] + result = client.post('/value', json=invalid) + assert result.status_code == 422 + locations = [error['loc'] for error in result.json()['detail']] + expected = { + 'query': [['query', 'value']], + 'body': [['body']], + 'list': [['body', 1], ['body', 2]], + 'set': [['body', 1], ['body', 2]], + 'dict': [['body', 'bad', '[key]'], ['body', 'bad']], + 'nullable': [['body']], + 'tuple': [['body', 1]], + }[where] + assert locations == expected + if where == 'nullable': + assert client.post('/value', json=None).status_code == 422 # An absent required body. + + +def test_plain_fastapi_compiled_pattern_keeps_explicit_python_semantics() -> None: + router = APIRouter() + + @router.get('/value') + async def endpoint(value: Annotated[str, Query(pattern=re.compile('^a$', re.I))]) -> str: + return value + + assert TestClient(application(router)).get('/value', params={'value': 'A\n'}).json() == 'A\n' + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_string_pattern_unsupported_by_rust_is_rejected(router_class: type[APIRouter]) -> None: + router = router_class() + + async def endpoint(value: Annotated[str, Query(pattern='(?<=a)b')]) -> str: + return value + + with pytest.raises((SchemaError, RegexError)): + router.add_api_route('/value', endpoint) + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_untyped_non_native_values_follow_router_policy(router_class: type[APIRouter]) -> None: + router = router_class() + + @router.get('/value') + async def endpoint(): # noqa: ANN202 - Exercise an untyped response. + return { + 'path': Path('/tmp/a'), + 'ip': ipaddress.ip_address('127.0.0.1'), + 'url': HttpUrl('https://example.com'), + 'secret': SecretStr('private'), + } + + if router_class is MsgspecRouter: + result = TestClient(application(router), raise_server_exceptions=False).get('/value') + assert result.status_code == 500 + assert 'private' not in result.text + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.json() == { + 'path': '/tmp/a', + 'ip': '127.0.0.1', + 'url': 'https://example.com/', + 'secret': '**********', + } + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_oversized_integer_error_does_not_expose_python_configuration( + router_class: type[APIRouter], +) -> None: + router = router_class() + + @router.get('/value') + async def endpoint(value: int) -> int: + return value + + result = TestClient(application(router)).get('/value', params={'value': '1' * 5000}) + assert result.status_code == 422 + assert 'sys.set_int_max_str_digits' not in result.text + + +@pytest.mark.parametrize('router_class,model', [(APIRouter, PydItem), (MsgspecRouter, Item)]) +@pytest.mark.parametrize('response_class', [JSONResponse, PlainTextResponse]) +def test_typed_custom_response_class( + router_class: type[APIRouter], model: Any, response_class: type[Response] +) -> None: + router = router_class() + options = ( + {'response_model': model} if response_class is JSONResponse else {'response_model': str} + ) + + @router.get('/value', response_class=response_class, **options) + async def endpoint(response: Response) -> Any: + response.status_code = 202 + response.headers['X-Endpoint'] = 'kept' + return {'value': 3, 'private': 'hidden'} if response_class is JSONResponse else 'ready' + + result = TestClient(application(router)).get('/value') + assert result.status_code == 202 and result.headers['X-Endpoint'] == 'kept' + if response_class is JSONResponse: + assert result.json() == {'value': 3} + else: + assert result.text == 'ready' and result.headers['Content-Type'].startswith('text/plain') + + +def test_typed_custom_response_still_validates() -> None: + router = MsgspecRouter() + + @router.get('/value', response_class=JSONResponse, response_model=Item) + async def endpoint() -> Any: + return {'value': 'wrong'} + + with pytest.raises(ResponseValidationError): + TestClient(application(router)).get('/value') + + +@pytest.mark.parametrize('router_class,model', [(APIRouter, PydItem), (MsgspecRouter, Item)]) +@pytest.mark.parametrize('asynchronous', [False, True]) +@pytest.mark.parametrize('sse', [False, True]) +@pytest.mark.parametrize('with_body', [False, True]) +def test_generator_endpoints_stream_validated_items( + router_class: type[APIRouter], model: Any, asynchronous: bool, sse: bool, with_body: bool +) -> None: + router = router_class() + closed = [] + if asynchronous: + + async def endpoint(body: model) -> AsyncIterator[model]: + try: + yield body + yield {'value': 2, 'private': 'hidden'} + finally: + closed.append(True) + else: + + def endpoint(body: model) -> Iterator[model]: + try: + yield body + yield {'value': 2, 'private': 'hidden'} + finally: + closed.append(True) + + if not with_body: + original = endpoint + if asynchronous: + + async def endpoint() -> AsyncIterator[model]: + async for item in original(model(value=1)): + yield item + else: + + def endpoint() -> Iterator[model]: + yield from original(model(value=1)) + + router.add_api_route( + '/value', + endpoint, + methods=['POST'], + **({'response_class': EventSourceResponse} if sse else {}), + ) + app = application(router) + result = TestClient(app).post('/value', **({'json': {'value': 1}} if with_body else {})) + assert result.status_code == 200 + content = result.text + rows = [ + json.loads(line[6:] if sse else line) + for line in content.splitlines() + if line and not line.startswith(':') + ] + assert rows == [{'value': 1}, {'value': 2}] and closed == [True] + assert result.headers['Content-Type'].startswith( + 'text/event-stream' if sse else 'application/jsonl' + ) + document = app.openapi() + media = document['paths']['/value']['post']['responses']['200']['content'] + schema = media['text/event-stream' if sse else 'application/jsonl']['itemSchema'] + if sse: + schema = schema['properties']['data']['contentSchema'] + assert '$ref' in schema + assert schema['$ref'].rsplit('/', 1)[-1] in document['components']['schemas'] + + +@pytest.mark.parametrize('sse', [False, True]) +def test_streaming_body_validation_precedes_headers_after_dependencies(sse: bool) -> None: + calls = [] + + def auth() -> None: + calls.append('auth') + raise HTTPException(401) + + router = MsgspecRouter() + options = {'response_class': EventSourceResponse} if sse else {} + + @router.post('/value', **options) + async def endpoint(body: Item, limit: int = 1) -> AsyncIterator[Item]: + calls.append('endpoint') + yield body + + app = application(router) + client = TestClient(app) + assert client.post('/value', json={'value': 'wrong'}).status_code == 422 + assert client.post('/value?limit=wrong', json={'value': 'wrong'}).json()['detail'][0][ + 'loc' + ] == ['query', 'limit'] + assert calls == [] + secured = MsgspecRouter(dependencies=[Depends(auth)]) + secured.add_api_route('/value', endpoint, methods=['POST'], **options) + assert ( + TestClient(application(secured)).post('/value', json={'value': 'wrong'}).status_code == 401 + ) + assert calls == ['auth'] + + +def test_sse_event_metadata_and_struct_payload() -> None: + router = MsgspecRouter() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent( + raw_data=msgspec.json.encode(Item(1)).decode('utf-8'), + id='event-1', + event='updated', + retry=123, + ) + yield ServerSentEvent(raw_data='raw', comment='kept') + + response = TestClient(application(router)).get('/value') + assert response.status_code == 200 + assert ( + 'id: event-1\n' in response.text + and 'event: updated\n' in response.text + and 'retry: 123\n' in response.text + ) + payloads = [ + line.removeprefix('data: ') + for line in response.text.splitlines() + if line.startswith('data: ') + ] + assert json.loads(payloads[0]) == {'value': 1} + assert payloads[1:] == ['raw'] and ': kept\n' in response.text + + +def test_raw_streaming_remains_raw_with_struct_body() -> None: + router = MsgspecRouter() + + @router.post('/value', response_class=StreamingResponse) + async def endpoint(body: Item) -> AsyncIterator[bytes]: + yield str(body.value).encode() + yield b' tail' + + response = TestClient(application(router)).post('/value', json={'value': 1}) + assert response.content == b'1 tail' + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_nested_backtracking_pattern_has_a_bounded_request(router_class: type[APIRouter]) -> None: + import subprocess + import sys + + code = """ +import sys +from typing import Annotated +from fastapi import APIRouter,FastAPI,Query +from fastapi.testclient import TestClient +from msgspec_fastapi import MsgspecRouter +r=MsgspecRouter() if sys.argv[1]=='msgspec' else APIRouter() +@r.get('/value') +async def endpoint(value:Annotated[str,Query(pattern='(a+)+$')])->str:return value +a=FastAPI();a.include_router(r) +assert TestClient(a).get('/value',params={'value':'a'*40+'!'}).status_code==422 +""" + subprocess.run( + [sys.executable, '-c', code, 'msgspec' if router_class is MsgspecRouter else 'pydantic'], + check=True, + capture_output=True, + timeout=10, + ) + + +@pytest.mark.parametrize('sse', [False, True]) +@pytest.mark.parametrize('asynchronous', [False, True]) +def test_disconnect_closes_stream_generator(sse: bool, asynchronous: bool) -> None: + import asyncio + + import anyio + + closed = [] + router = MsgspecRouter() + if asynchronous: + + async def endpoint() -> AsyncIterator[int]: + try: + yield 1 + await anyio.sleep_forever() + finally: + closed.append(True) + else: + + def endpoint() -> Iterator[int]: + try: + while True: + yield 1 + finally: + closed.append(True) + + router.add_api_route( + '/value', endpoint, **({'response_class': EventSourceResponse} if sse else {}) + ) + app = application(router) + + async def run() -> None: + first_body = asyncio.Event() + + async def receive() -> dict[str, Any]: + await first_body.wait() + return {'type': 'http.disconnect'} + + async def send(message: dict[str, Any]) -> None: + if message['type'] == 'http.response.body' and message.get('body'): + first_body.set() + + scope = { + 'type': 'http', + 'asgi': {'version': '3.0', 'spec_version': '2.3'}, + 'http_version': '1.1', + 'method': 'GET', + 'scheme': 'http', + 'path': '/value', + 'raw_path': b'/value', + 'query_string': b'', + 'root_path': '', + 'headers': [], + 'client': ('test', 123), + 'server': ('test', 80), + } + await asyncio.wait_for(app(scope, receive, send), timeout=3) + + asyncio.run(run()) + assert closed == [True] + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('alternative', [Any, str, Annotated[str, Field(min_length=3)]]) +@pytest.mark.parametrize('reverse', [False, True]) +def test_union_patterns_accept_other_valid_branches( + router_class: type[APIRouter], alternative: Any, reverse: bool +) -> None: + router = router_class() + pattern = Annotated[str, Field(pattern='^a$')] + target = alternative | pattern if reverse else pattern | alternative + + @router.post('/value') + async def endpoint(value: Annotated[target, Body()]) -> str: + return value + + result = TestClient(application(router)).post('/value', json='different') + assert result.status_code == 200 and result.json() == 'different' + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_union_pattern_constraint_on_the_whole_value_still_applies( + router_class: type[APIRouter], +) -> None: + router = router_class() + + @router.post('/value') + async def endpoint(value: Annotated[Any | str, Body(pattern='^a$')]) -> str: + return value + + result = TestClient(application(router)).post('/value', json='different') + assert result.status_code == 422 + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('asynchronous', [False, True]) +def test_bound_generator_methods(router_class: type[APIRouter], asynchronous: bool) -> None: + class Service: + if asynchronous: + + async def endpoint(self) -> AsyncIterator[int]: + yield 1 + else: + + def endpoint(self) -> Iterator[int]: + yield 1 + + router = router_class() + router.get('/value')(Service().endpoint) + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.headers['content-type'] == 'application/jsonl' + assert result.text == '1\n' + + +@pytest.mark.parametrize('subclass', [False, True]) +def test_custom_json_response_supports_raw_values(subclass: bool) -> None: + from msgspec_fastapi import MsgspecJSONResponse + + class SelectedResponse(MsgspecJSONResponse if subclass else JSONResponse): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.headers['x-selected'] = 'yes' + + class Payload(msgspec.Struct): + raw: msgspec.Raw + + router = MsgspecRouter() + + @router.get('/value', response_class=SelectedResponse) + async def endpoint() -> Payload: + return Payload(msgspec.Raw(b'{"a":1}')) + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 and result.json() == {'raw': {'a': 1}} + assert result.headers['x-selected'] == 'yes' + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('response_class', [JSONResponse, PlainTextResponse, Response]) +def test_custom_response_openapi_media_matches_selected_class( + router_class: type[APIRouter], response_class: type[Response] +) -> None: + router = router_class() + + @router.get('/value', response_class=response_class) + async def endpoint() -> str: + return 'hello' + + app = application(router) + result = TestClient(app).get('/value') + assert result.status_code == 200 + documented = app.openapi()['paths']['/value']['get']['responses']['200'].get('content', {}) + assert set(documented) == ({response_class.media_type} if response_class.media_type else set()) + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_stream_body_default_is_not_copied_by_the_media_check( + router_class: type[APIRouter], +) -> None: + from dataclasses import dataclass + + calls = [] + + @dataclass + class Payload: + value: int + + def __deepcopy__(self, memo: Any) -> Any: + calls.append(True) + return Payload(self.value + len(calls)) + + router = router_class() + + @router.post('/value') + async def endpoint(body: Annotated[Payload, Body()] = Payload(10)) -> AsyncIterator[int]: + yield body.value + + app = application(router) + calls.clear() + result = TestClient(app).post('/value') + assert result.status_code == 200 and result.text == '13\n' + assert len(calls) == 2 + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_union_patterns_accept_a_valid_literal_alternative(router_class: type[APIRouter]) -> None: + from typing import Literal + + router = router_class() + target = Annotated[str, Field(pattern='^a$')] | Literal['b'] + + @router.post('/value') + async def endpoint(value: Annotated[target, Body()]) -> str: + return value + + client = TestClient(application(router)) + assert client.post('/value', json='b').json() == 'b' + assert client.post('/value', json='other').status_code == 422 + + +@pytest.mark.parametrize('router_class,model', [(APIRouter, PydItem), (MsgspecRouter, Item)]) +def test_explicit_sse_event_retains_native_item_validation_bypass( + router_class: type[APIRouter], model: Any +) -> None: + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[model]: + yield ServerSentEvent(data={'value': 'invalid', 'extra': 'kept'}) + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert json.loads(result.text.removeprefix('data: ').strip()) == { + 'value': 'invalid', + 'extra': 'kept', + } + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('sse', [False, True]) +def test_nonfinite_stream_item_keeps_json_null_encoding( + router_class: type[APIRouter], sse: bool +) -> None: + router = router_class() + + async def endpoint() -> AsyncIterator[float]: + yield float('nan') + + router.add_api_route( + '/value', endpoint, **({'response_class': EventSourceResponse} if sse else {}) + ) + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == ('data: null\n\n' if sse else 'null\n') + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('value', [float('nan'), float('inf'), -float('inf'), None]) +def test_explicit_sse_event_keeps_nonfinite_payload_distinct_from_absent_data( + router_class: type[APIRouter], value: Any +) -> None: + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=value, id='event-1') + + if router_class is MsgspecRouter and value is not None: + assert_sse_rejected(application(router)) + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + if value is None: + assert result.text == 'id: event-1\n\n' + else: + # Explicit events retain native encoding across supported FastAPI versions. + encodings = {'null', json.dumps(value)} + assert result.text in {f'data: {data}\nid: event-1\n\n' for data in encodings} + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_explicit_sse_raw_data_has_precedence(router_class: type[APIRouter]) -> None: + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(raw_data='kept', id='event-1').model_copy( + update={'data': float('nan')} + ) + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == 'data: kept\nid: event-1\n\n' + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('kind', ['alias', 'custom_model', 'custom_object']) +def test_explicit_sse_payload_retains_native_json_serializer( + router_class: type[APIRouter], kind: str +) -> None: + calls = [] + + class Aliased(BaseModel): + internal: int = Field(alias='external') + + class CustomModel(BaseModel): + value: int + + def model_dump_json(self, *args: Any, **kwargs: Any) -> str: + calls.append('serialized') + return '{"wire":"custom"}' + + class CustomObject: + def model_dump_json(self) -> str: + calls.append('serialized') + return '{"wire":"custom"}' + + payload = { + 'alias': Aliased(external=7), + 'custom_model': CustomModel(value=7), + 'custom_object': CustomObject(), + }[kind] + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=payload, id='event-1') + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + assert calls == [] + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + data = '{"internal":7}' if kind == 'alias' else '{"wire":"custom"}' + assert result.text == f'data: {data}\nid: event-1\n\n' + assert calls == ([] if kind == 'alias' else ['serialized']) + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('kind', ['property', 'dynamic', 'struct_property']) +def test_explicit_sse_serializer_lookups_are_not_repeated( + router_class: type[APIRouter], kind: str +) -> None: + calls = [] + + def serializer() -> Any: + calls.append('lookup') + + def dump() -> str: + calls.append('serialized') + return json.dumps({'lookups': calls.count('lookup')}, separators=(',', ':')) + + return dump + + class PropertyData: + @property + def model_dump_json(self) -> Any: + return serializer() + + class DynamicData: + def __getattr__(self, name: str) -> Any: + if name == 'model_dump_json': + return serializer() + raise AttributeError(name) + + class StructData(msgspec.Struct): + value: int + + @property + def model_dump_json(self) -> Any: + return serializer() + + data = {'property': PropertyData(), 'dynamic': DynamicData(), 'struct_property': StructData(7)}[ + kind + ] + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=data, id='event-1') + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + assert calls == [] + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == 'data: {"lookups":2}\nid: event-1\n\n' + assert calls == ['lookup', 'lookup', 'serialized'] + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('data', [b'ab', Decimal('1.25')]) +def test_explicit_sse_payload_keeps_native_scalar_encoding( + router_class: type[APIRouter], data: Any +) -> None: + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=data) + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == ('data: "ab"\n\n' if isinstance(data, bytes) else 'data: 1.25\n\n') + + +def test_explicit_sse_msgspec_raw_null_remains_present() -> None: + router = MsgspecRouter() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent( + raw_data=msgspec.json.encode(msgspec.Raw(b'null')).decode('utf-8'), + id='raw', + ) + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 and result.text == 'data: null\nid: raw\n\n' + + +@pytest.mark.parametrize('router_class,model', [(APIRouter, PydItem), (MsgspecRouter, Item)]) +@pytest.mark.parametrize('shape', ['list', 'dict']) +def test_explicit_sse_nested_payloads( + router_class: type[APIRouter], model: Any, shape: str +) -> None: + router = router_class() + raw = msgspec.Raw(b'null') if router_class is MsgspecRouter else None + data = [model(value=1), raw] if shape == 'list' else {'item': model(value=1), 'raw': raw} + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + if router_class is MsgspecRouter: + yield ServerSentEvent(raw_data=msgspec.json.encode(data).decode('utf-8')) + else: + yield ServerSentEvent(data=data) + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + expected = [{'value': 1}, None] if shape == 'list' else {'item': {'value': 1}, 'raw': None} + assert json.loads(result.text.removeprefix('data: ').strip()) == expected + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('callback', ['model_copy', '__copy__']) +def test_explicit_sse_does_not_invoke_event_copy_callbacks( + router_class: type[APIRouter], callback: str +) -> None: + calls = [] + + class Payload(msgspec.Struct): + value: int + + def model_dump_json(self) -> str: + return '{"value":7}' + + class Event(ServerSentEvent): + if callback == 'model_copy': + + def model_copy(self, **kwargs: Any) -> Any: + calls.append('model_copy') + result = super().model_copy(**kwargs) + result.id = 'copied' + return result + else: + + def __copy__(self) -> Any: + calls.append('__copy__') + result = super().__copy__() + result.id = 'copied' + return result + + original = Event(data=Payload(7), id='original', event='updated', retry=5, comment='keep') + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield original + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router), 'ServerSentEvent subclasses are not supported') + assert calls == [] + assert original.id == 'original' and original.raw_data is None + assert original.data.value == 7 + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == ': keep\nevent: updated\ndata: {"value":7}\nid: original\nretry: 5\n\n' + assert calls == [] + assert original.id == 'original' and original.raw_data is None + assert original.data.value == 7 + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +def test_native_sse_payload_does_not_invoke_numeric_hooks( + router_class: type[APIRouter], +) -> None: + calls = [] + + class Value: + def __init__(self) -> None: + self.value = 7 + + def __int__(self) -> int: + calls.append('int') + return 7 + + def __float__(self) -> float: + calls.append('float') + return 7.0 + + def __bool__(self) -> bool: + calls.append('bool') + return True + + def __str__(self) -> str: + calls.append('str') + return 'changed' + + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=[Value()]) + + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + assert calls == [] + return + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert json.loads(result.text.removeprefix('data: ').strip()) == [{'value': 7}] + assert calls == [] + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('shape', ['direct', 'list', 'dict']) +def test_native_sse_payload_does_not_invoke_metaclass_comparisons( + router_class: type[APIRouter], shape: str +) -> None: + calls = [] + + class Meta(type): + def __eq__(cls, other: Any) -> bool: + calls.append('comparison') + return False + + __hash__ = type.__hash__ + + class Value(metaclass=Meta): + def model_dump_json(self) -> str: + return json.dumps({'comparisons': len(calls)}, separators=(',', ':')) + + value = Value() + data = {'direct': value, 'list': [value], 'dict': {'item': value}}[shape] + router = router_class() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield ServerSentEvent(data=data) + + app = application(router) + calls.clear() + if router_class is MsgspecRouter: + assert_sse_rejected(application(router)) + assert calls == [] + return + + result = TestClient(app).get('/value') + assert result.status_code == 200 + expected = {'direct': {'comparisons': 0}, 'list': [{}], 'dict': {'item': {}}}[shape] + assert json.loads(result.text.removeprefix('data: ').strip()) == expected + assert calls == [] + + +@pytest.mark.parametrize('where', ['query', 'body', 'nested', 'response']) +def test_compiled_python_patterns_are_rejected(where: str) -> None: + pattern = re.compile('(a+)+$', re.I) + target = Annotated[str, Field(pattern=pattern)] + annotation = { + 'query': Annotated[str, Query(pattern=pattern)], + 'body': Annotated[str, Body(pattern=pattern)], + 'nested': list[target], + 'response': str, + }[where] + + async def endpoint(value: annotation) -> Any: + return value + + with pytest.raises(TypeError, match='compiled Python patterns are not supported'): + MsgspecRouter().add_api_route( + '/value', + endpoint, + methods=['POST'], + **({'response_model': target} if where == 'response' else {}), + ) + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize( + 'pattern,value', + [ + (r'^\w+$', 'é١𐐀'), + (r'^\w+$', 'a\u0301'), + (r'^\d+$', '١٢٣'), + (r'^\p{Greek}+$', 'αβγ'), + ], +) +def test_string_patterns_keep_unicode_classes( + router_class: type[APIRouter], + pattern: str, + value: str, +) -> None: + router = router_class() + + @router.get('/value') + async def endpoint(value: Annotated[str, Query(pattern=pattern)]) -> str: + return value + + client = TestClient(application(router)) + assert client.get('/value', params={'value': value}).json() == value + assert client.get('/value', params={'value': '!'}).status_code == 422 + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('mode', ['ordinary', 'jsonl', 'sse']) +def test_patterned_msgspec_requests_do_not_run_pydantic_validation( + router_class: type[APIRouter], + mode: str, +) -> None: + import asyncio + import sys + + import httpx + + pattern = Annotated[str, Field(pattern='^a+$')] + router = router_class() + + async def endpoint( + value: list[pattern], + query: Annotated[str, Query(pattern='^a+$')], + ) -> list[pattern]: + return value + + async def stream_endpoint( + value: list[pattern], + query: Annotated[str, Query(pattern='^a+$')], + ) -> AsyncIterator[list[pattern]]: + yield value + + router.add_api_route( + '/value', + endpoint if mode == 'ordinary' else stream_endpoint, + methods=['POST'], + **({'response_class': EventSourceResponse} if mode == 'sse' else {}), + ) + app = application(router) + observed = [] + + def profile(frame: Any, event: str, arg: Any) -> None: + if ( + event == 'c_call' + and getattr(arg, '__name__', '') == 'validate_python' + and isinstance(getattr(arg, '__self__', None), SchemaValidator) + ): + observed.append(arg) + + async def check() -> None: + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=app), + base_url='http://local', + ) as client: + for body, query, status in [ + (['aa'], 'aa', 200), + (['bad'], 'aa', 422), + (['aa'], 'bad', 422), + ]: + observed.clear() + previous = sys.getprofile() + try: + sys.setprofile(profile) + response = await client.post('/value', json=body, params={'query': query}) + finally: + sys.setprofile(previous) + assert response.status_code == status + if status == 200: + if mode == 'ordinary': + assert response.json() == ['aa'] + else: + assert response.text == ( + 'data: ["aa"]\n\n' if mode == 'sse' else '["aa"]\n' + ) + if router_class is MsgspecRouter: + assert observed == [] + else: + assert observed, 'The plain FastAPI control must exercise the profiler' + + asyncio.run(check()) + + +@pytest.mark.parametrize('shape', ['list', 'tuple', 'nested']) +@pytest.mark.parametrize('embedded', [False, True]) +def test_strict_body_pattern_errors_keep_every_original_position( + shape: str, embedded: bool +) -> None: + pattern = Annotated[str, Field(pattern='^a+$')] + annotation = { + 'list': list[pattern], + 'tuple': tuple[pattern, ...], + 'nested': list[list[pattern]], + }[shape] + if embedded: + annotation = Annotated[annotation, Body(embed=True, alias='values')] + router = MsgspecRouter() + + @router.post('/value') + async def endpoint(value: annotation) -> Any: + return value + + test_client = TestClient(application(router)) + payload = ['bad', 'aa', 'a\n', 'bad'] + if shape == 'nested': + payload = [payload] + response = test_client.post('/value', json={'values': payload} if embedded else payload) + assert response.status_code == 422 + prefix = ['body'] + (['values'] if embedded else []) + ([0] if shape == 'nested' else []) + assert [error['loc'] for error in response.json()['detail']] == [ + prefix + [index] for index in (0, 2, 3) + ] + assert all( + error['type'] == 'string_pattern_mismatch' and error['ctx'] == {'pattern': '^a+$'} + for error in response.json()['detail'] + ) + valid = [['a', 'aa']] if shape == 'nested' else ['a', 'aa'] + assert test_client.post('/value', json={'values': valid} if embedded else valid).json() == valid + + +@pytest.mark.parametrize('mode', ['response', 'jsonl', 'sse']) +def test_stdlib_path_and_ip_values_keep_explicit_encoding(mode: str) -> None: + payload = { + 'path': Path('/tmp/a'), + 'ip': ipaddress.ip_address('::1'), + 'interface': ipaddress.ip_interface('127.0.0.1/8'), + 'network': ipaddress.ip_network('127.0.0.0/8'), + } + router = MsgspecRouter() + if mode == 'response': + + async def endpoint(): # noqa: ANN202 - Exercise the untyped response encoder. + return payload + else: + + async def endpoint() -> AsyncIterator[Any]: + yield payload + + router.add_api_route( + '/value', + endpoint, + response_model=None, + **({'response_class': EventSourceResponse} if mode == 'sse' else {}), + ) + response = TestClient(application(router)).get('/value') + assert response.status_code == 200 + content = response.text.removeprefix('data: ').strip() if mode == 'sse' else response.text + assert json.loads(content) == { + 'path': '/tmp/a', + 'ip': '::1', + 'interface': '127.0.0.1/8', + 'network': '127.0.0.0/8', + } + if mode != 'response': + assert response.text.endswith('\n\n' if mode == 'sse' else '\n') + + +@pytest.mark.parametrize('kind', ['url', 'secret']) +@pytest.mark.parametrize('nested', [False, True]) +def test_untyped_pydantic_scalars_are_rejected_without_extraction( + kind: str, nested: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + calls = [] + scalar_class = SecretStr if kind == 'secret' else HttpUrl + original = scalar_class.__str__ + + def stringify(value: Any) -> str: + calls.append('str') + return original(value) + + monkeypatch.setattr(scalar_class, '__str__', stringify) + value = ( + SecretStr('must-not-leak') + if kind == 'secret' + else HttpUrl('https://example.com/private-credential') + ) + router = MsgspecRouter() + + @router.get('/value') + async def endpoint(): # noqa: ANN202 - Exercise the untyped response encoder. + return {'items': [value]} if nested else value + + calls.clear() + response = TestClient(application(router), raise_server_exceptions=False).get('/value') + assert response.status_code == 500 + assert calls == [] + assert 'must-not-leak' not in response.text and 'private-credential' not in response.text + + +def test_explicit_raw_sse_keeps_metadata_without_model_operations( + monkeypatch: pytest.MonkeyPatch, +) -> None: + calls = [] + value = ServerSentEvent( + raw_data='{"value":7}', id='original', event='updated', retry=5, comment='keep' + ) + + def unexpected(*args: Any, **kwargs: Any) -> Any: + calls.append('model operation') + raise RuntimeError('Unexpected Pydantic model operation') + + for name in ('model_dump', 'model_dump_json', 'model_copy', '__copy__'): + monkeypatch.setattr(ServerSentEvent, name, unexpected) + router = MsgspecRouter() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[ServerSentEvent]: + yield value + + result = TestClient(application(router)).get('/value') + assert result.status_code == 200 + assert result.text == ': keep\nevent: updated\ndata: {"value":7}\nid: original\nretry: 5\n\n' + assert calls == [] + assert value.id == 'original' and value.raw_data == '{"value":7}' and value.data is None diff --git a/tests/test_router.py b/tests/test_router.py index c3d3e87..c0bb40f 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -213,23 +213,36 @@ def test_untyped_bodyless_status_keeps_empty_body(status: int) -> None: @pytest.mark.parametrize('nested', [False, True]) -def test_untyped_pydantic_values_use_public_json_aliases(nested: bool) -> None: - value = AliasModel(external=1) +def test_untyped_pydantic_values_are_rejected_without_serialization(nested: bool) -> None: + calls = [] + + class PrivateModel(BaseModel): + internal: int = Field(alias='external') + secret: str + + def model_dump(self, *args: Any, **kwargs: Any) -> Any: + calls.append('model_dump') + return {'secret': self.secret} + + def model_dump_json(self, *args: Any, **kwargs: Any) -> str: + calls.append('model_dump_json') + return self.secret + + value = PrivateModel(external=1, secret='must-not-leak') result = response(setup({'items': [value]} if nested else value)) - assert result.status_code == 200 - assert result.json() == ({'items': [{'external': 1}]} if nested else {'external': 1}) + assert result.status_code == 500 + assert calls == [] + assert 'must-not-leak' not in result.text -def test_annotated_pydantic_route_is_still_delegated() -> None: +def test_annotated_pydantic_response_is_rejected_at_registration() -> None: router = MsgspecRouter() - @router.get('/value') async def endpoint() -> PydCity: return {'population': -1} - app = FastAPI() - app.include_router(router) - assert response(app).status_code == 500 + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + router.get('/value')(endpoint) @pytest.mark.parametrize('model', [None, OMITTED]) @@ -560,7 +573,9 @@ def test_body_validation_errors_are_reported_after_dependencies( assert calls == [None] result = client.post('/value', headers={'token': 'secret'}, **options) assert result.status_code == 422 - assert result.json()['detail'][0]['loc'] == ['body'] + assert result.json()['detail'][0]['loc'] == ( + ['body'] if body is None else ['body', 'population'] + ) def test_malformed_body_is_still_rejected_before_dependencies() -> None: diff --git a/tests/test_runtime_codec_boundary.py b/tests/test_runtime_codec_boundary.py new file mode 100644 index 0000000..abd3877 --- /dev/null +++ b/tests/test_runtime_codec_boundary.py @@ -0,0 +1,96 @@ +"""Observe real request validation and encoding, including a native positive control.""" + +import asyncio +import sys +from ipaddress import IPv4Address +from pathlib import Path +from typing import Annotated, Any + +import httpx +import msgspec +from fastapi import APIRouter, FastAPI, Query +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter + + +class Input(msgspec.Struct): + value: int + + +class NativeInput(BaseModel): + value: int + + +async def observed_request(app: FastAPI, payload: Any) -> tuple[httpx.Response, list[str]]: + calls = [] + + def observe(frame: Any, event: str, arg: Any) -> None: + if event == 'c_call': + owner = getattr(arg, '__self__', None) + if type(owner).__module__.startswith('pydantic_core'): + name = getattr(arg, '__name__', '') + if name in ( + 'validate_python', + 'validate_json', + 'validate_strings', + 'to_python', + 'to_json', + ): + calls.append(name) + elif ( + event == 'call' + and frame.f_code.co_name == 'jsonable_encoder' + and frame.f_code.co_filename.endswith('/fastapi/encoders.py') + ): + calls.append('jsonable_encoder') + + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=app), base_url='http://test' + ) as client: + previous = sys.getprofile() + try: + sys.setprofile(observe) + response = await client.post('/items?q=2', json=payload) + finally: + sys.setprofile(previous) + return response, calls + + +def test_native_control_exercises_validation_and_serialization_observer() -> None: + router = APIRouter() + + @router.post('/items') + async def endpoint(value: NativeInput) -> NativeInput: + return value + + app = FastAPI() + app.include_router(router) + response, calls = asyncio.run(observed_request(app, {'value': 7})) + assert response.status_code == 200 and response.json() == {'value': 7} + assert 'validate_python' in calls + assert any(name in calls for name in ('to_python', 'to_json')) + + +def test_msgspec_request_and_stdlib_return_use_no_external_model_codec() -> None: + router = MsgspecRouter() + + @router.post('/items', response_model=None) + async def endpoint(value: Input, q: Annotated[int, Query(gt=0)]) -> dict[str, Any]: + return { + 'value': value.value, + 'q': q, + 'path': Path('file.txt'), + 'ip': IPv4Address('127.0.0.1'), + } + + app = FastAPI() + app.include_router(router) + response, calls = asyncio.run(observed_request(app, {'value': 7})) + assert response.status_code == 200 + assert response.json() == {'value': 7, 'q': 2, 'path': 'file.txt', 'ip': '127.0.0.1'} + assert calls == [] + response, calls = asyncio.run(observed_request(app, {'value': 'bad'})) + assert response.status_code == 422 + assert response.json()['detail'][0]['loc'] == ['body', 'value'] + assert calls == [] diff --git a/tests/test_sse_boundary.py b/tests/test_sse_boundary.py new file mode 100644 index 0000000..48fc3a6 --- /dev/null +++ b/tests/test_sse_boundary.py @@ -0,0 +1,147 @@ +"""Only native SSE metadata objects bypass the msgspec item codec.""" + +from collections.abc import AsyncIterator, Iterator +from typing import Any + +import pytest +from fastapi import APIRouter, FastAPI +from fastapi.sse import EventSourceResponse, ServerSentEvent +from fastapi.testclient import TestClient + +from msgspec_fastapi import MsgspecRouter, install_openapi + + +def test_runtime_event_check_avoids_unrelated_metaclass_hooks() -> None: + calls = [] + + class Hostile(type): + __hash__ = type.__hash__ + + def __getattribute__(cls, name: str) -> Any: + if name in ('__mro__', '__bases__', '__dict__'): + calls.append(name) + raise RuntimeError(name) + return type.__getattribute__(cls, name) + + def __eq__(cls, other: Any) -> bool: + calls.append('equality') + raise RuntimeError('Metaclass equality executed') + + class Payload(metaclass=Hostile): + pass + + value = Payload() + router = MsgspecRouter() + + @router.get('/value', response_class=EventSourceResponse) + async def endpoint() -> AsyncIterator[Any]: + yield value + + app = FastAPI() + app.include_router(router) + with pytest.raises((TypeError, ExceptionGroup)): + TestClient(app).get('/value') + assert calls == [] + + +def test_event_inheritance_check_avoids_pydantic_metaclass( + monkeypatch: pytest.MonkeyPatch, +) -> None: + calls = [] + metaclass = type(ServerSentEvent) + original = metaclass.__subclasscheck__ + + class Event(ServerSentEvent): + pass + + def observed(cls: type, candidate: type) -> bool: + if cls is ServerSentEvent: + calls.append('subclasscheck') + return original(cls, candidate) + + monkeypatch.setattr(metaclass, '__subclasscheck__', observed) + assert issubclass(Event, ServerSentEvent) and calls == ['subclasscheck'] + calls.clear() + + async def endpoint() -> AsyncIterator[Event]: + yield Event() + + with pytest.raises(TypeError, match='subclasses'): + MsgspecRouter().get('/value', response_class=EventSourceResponse)(endpoint) + assert calls == [] + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('annotation', ['missing', 'base', 'any']) +@pytest.mark.parametrize('asynchronous', [False, True]) +def test_event_subclass_is_rejected_before_field_getters( + router_class: type[APIRouter], annotation: str, asynchronous: bool +) -> None: + reads = [] + closed = [] + + class Event(ServerSentEvent): + def __getattribute__(self, name: str) -> Any: + if name == 'raw_data': + reads.append(name) + if len(reads) > 2: + raise RuntimeError('Additional field access') + return super().__getattribute__(name) + + event = Event(raw_data='literal', id='kept') + if asynchronous: + + async def endpoint(): # noqa: ANN202 - Test absent, base and Any stream annotations. + try: + yield event + finally: + closed.append(True) + else: + + def endpoint(): # noqa: ANN202 - Test absent, base and Any stream annotations. + try: + yield event + finally: + closed.append(True) + + if annotation != 'missing': + item = ServerSentEvent if annotation == 'base' else Any + endpoint.__annotations__['return'] = AsyncIterator[item] if asynchronous else Iterator[item] + router = router_class() + router.get('/value', response_class=EventSourceResponse)(endpoint) + app = FastAPI() + app.include_router(router) + if router_class is MsgspecRouter: + install_openapi(app) + reads.clear() + if router_class is APIRouter: + response = TestClient(app).get('/value') + assert response.content == b'data: literal\nid: kept\n\n' + assert len(reads) == 2 + else: + with pytest.raises((TypeError, ExceptionGroup)) as caught: + TestClient(app).get('/value') + + def leaves(error: BaseException) -> list[BaseException]: + if isinstance(error, BaseExceptionGroup): + return [leaf for child in error.exceptions for leaf in leaves(child)] + return [error] + + errors = leaves(caught.value) + assert len(errors) == 1 and type(errors[0]) is TypeError + assert str(errors[0]) == 'ServerSentEvent subclasses are not supported' + assert reads == [] + assert closed == [True] + + +@pytest.mark.parametrize('explicit_model', [False, True]) +def test_declared_event_subclass_is_rejected_at_registration(explicit_model: bool) -> None: + class Event(ServerSentEvent): + pass + + async def endpoint() -> AsyncIterator[Event]: + yield Event(raw_data='value') + + options = {'response_model': None} if explicit_model else {} + with pytest.raises(TypeError, match='ServerSentEvent subclasses are not supported'): + MsgspecRouter().get('/value', response_class=EventSourceResponse, **options)(endpoint) diff --git a/tests/test_stream_adapter.py b/tests/test_stream_adapter.py new file mode 100644 index 0000000..84c60b2 --- /dev/null +++ b/tests/test_stream_adapter.py @@ -0,0 +1,120 @@ +"""Additional native-field serialization and Raw safety regression controls.""" + +from collections.abc import AsyncIterator, Iterator +from typing import Any + +import msgspec +import pytest +from fastapi import APIRouter, FastAPI +from fastapi.sse import EventSourceResponse +from fastapi.testclient import TestClient +from pydantic import BaseModel + +from msgspec_fastapi import MsgspecRouter, install_openapi + + +class Payload(msgspec.Struct): + raw: msgspec.Raw + + +class Item(msgspec.Struct): + value: int + + +class PydItem(BaseModel): + value: int + + +def client(router: APIRouter) -> TestClient: + app = FastAPI() + app.include_router(router) + if isinstance(router, MsgspecRouter): + install_openapi(app) + return TestClient(app) + + +@pytest.mark.parametrize('sse', [False, True]) +def test_untyped_msgspec_stream_keeps_struct_serialization(sse: bool) -> None: + router = MsgspecRouter() + + @router.get( + '/items', response_model=None, **({'response_class': EventSourceResponse} if sse else {}) + ) + async def endpoint() -> AsyncIterator[Any]: + yield Item(7) + + result = client(router).get('/items') + assert result.status_code == 200 + assert result.text == ('data: {"value":7}\n\n' if sse else '{"value":7}\n') + + +@pytest.mark.parametrize('trusted', [False, True]) +def test_pretty_raw_is_compacted_to_one_jsonl_item(trusted: bool) -> None: + router = MsgspecRouter() + pretty = msgspec.Raw(b'{\n "x": 1,\n "values": [1, 2]\n}') + + async def endpoint() -> AsyncIterator[Any]: + yield Payload(pretty) if trusted else pretty + + router.add_api_route('/items', endpoint, response_model=Payload if trusted else None) + result = client(router).get('/items') + assert result.status_code == 200 + assert result.text == ( + '{"raw":{"x":1,"values":[1,2]}}\n' if trusted else '{"x":1,"values":[1,2]}\n' + ) + assert len(result.text.splitlines()) == 1 + + +@pytest.mark.parametrize('trusted', [False, True]) +@pytest.mark.parametrize( + 'raw,error', + [ + (b'{', msgspec.DecodeError), + (b'1 2', msgspec.DecodeError), + (b'"\xff"', UnicodeDecodeError), + (b'"\\ud800"', msgspec.DecodeError), + (b'"\\udfff"', msgspec.DecodeError), + (b'1e999', msgspec.ValidationError), + (b'9' * 5000, msgspec.ValidationError), + (b'{"v":"\\ud800"}', msgspec.DecodeError), + (b'{"v":1e999}', msgspec.ValidationError), + (b'{"v":' + b'9' * 5000 + b'}', msgspec.ValidationError), + ], +) +def test_malformed_raw_does_not_bypass_stream_json_validation( + trusted: bool, raw: bytes, error: type[Exception] +) -> None: + router = MsgspecRouter() + + async def endpoint() -> AsyncIterator[Any]: + yield Payload(msgspec.Raw(raw)) if trusted else msgspec.Raw(raw) + + router.add_api_route('/items', endpoint, response_model=Payload if trusted else None) + with pytest.raises(error): + client(router).get('/items') + + +@pytest.mark.parametrize('router_class', [APIRouter, MsgspecRouter]) +@pytest.mark.parametrize('sync', [False, True]) +def test_pydantic_jsonl_still_uses_native_model_projection( + router_class: type[APIRouter], sync: bool +) -> None: + router = router_class() + if sync: + + def endpoint() -> Iterator[PydItem]: + yield {'value': 7, 'private': 'hidden'} + else: + + async def endpoint() -> AsyncIterator[PydItem]: + yield {'value': 7, 'private': 'hidden'} + + if router_class is MsgspecRouter: + with pytest.raises(TypeError, match='Pydantic model behavior is not supported'): + router.get('/items')(endpoint) + return + + router.get('/items')(endpoint) + result = client(router).get('/items') + assert result.status_code == 200 and result.headers['content-type'] == 'application/jsonl' + assert result.text == '{"value":7}\n' From c3a7156f5967cdbc6bb6d5e5d876a46e74bdf292 Mon Sep 17 00:00:00 2001 From: Tseluiko Aleksandr Date: Mon, 16 Nov 2026 09:14:34 +0300 Subject: [PATCH 2/2] Make tests portable across platforms --- pyproject.toml | 4 ++-- tests/test_native_serialization.py | 2 +- tests/test_request_response.py | 4 ++-- tests/test_router.py | 18 +++++++++++++++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 666fb39..fa44690 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ Source = "https://github.com/Siyet/msgspec-fastapi" Issues = "https://github.com/Siyet/msgspec-fastapi/issues" [dependency-groups] -test = ["pytest", "httpx", "httpx2", "python-multipart"] +test = ["pytest", "httpx", "httpx2", "python-multipart", "tzdata; sys_platform == 'win32'"] lint = ["ruff==0.17.0", "pre-commit>=4.5,<5"] typing = ["mypy==2.4.1"] @@ -43,7 +43,7 @@ msgspec_fastapi = ["*.pyi", "py.typed"] [tool.cibuildwheel] build = "cp3{11,12,13,14}-*" skip = "*-manylinux_i686 *-musllinux_i686 *-win32" -test-requires = ["pytest", "httpx", "httpx2", "python-multipart"] +test-requires = ["pytest", "httpx", "httpx2", "python-multipart", "tzdata; sys_platform == 'win32'"] test-command = "pytest {project}/tests" [tool.pytest.ini_options] diff --git a/tests/test_native_serialization.py b/tests/test_native_serialization.py index 1fcce32..8dd9f51 100644 --- a/tests/test_native_serialization.py +++ b/tests/test_native_serialization.py @@ -66,7 +66,7 @@ async def endpoint(): # noqa: ANN202 - Exercise the untyped custom response pat return {'path': Path('/tmp/value')} result = TestClient(app_for(router)).get('/value') - assert result.json() == {'path': '/tmp/value'} + assert result.json() == {'path': str(Path('/tmp/value'))} assert result.headers['x-selected'] == 'yes' diff --git a/tests/test_request_response.py b/tests/test_request_response.py index 8e190f9..72c00fb 100644 --- a/tests/test_request_response.py +++ b/tests/test_request_response.py @@ -146,7 +146,7 @@ async def endpoint(): # noqa: ANN202 - Exercise an untyped response. result = TestClient(application(router)).get('/value') assert result.status_code == 200 assert result.json() == { - 'path': '/tmp/a', + 'path': str(Path('/tmp/a')), 'ip': '127.0.0.1', 'url': 'https://example.com/', 'secret': '**********', @@ -1126,7 +1126,7 @@ async def endpoint() -> AsyncIterator[Any]: assert response.status_code == 200 content = response.text.removeprefix('data: ').strip() if mode == 'sse' else response.text assert json.loads(content) == { - 'path': '/tmp/a', + 'path': str(Path('/tmp/a')), 'ip': '::1', 'interface': '127.0.0.1/8', 'network': '127.0.0.0/8', diff --git a/tests/test_router.py b/tests/test_router.py index c0bb40f..ec30f44 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -1,5 +1,6 @@ """Tests for MsgspecRouter request decoding, parameter validation and response encoding.""" +import asyncio from typing import Annotated, Any, Generic, TypeVar import httpx @@ -472,9 +473,20 @@ async def endpoint(value: annotation): # noqa: ANN202 - Test an endpoint withou app = FastAPI() app.include_router(router) depth = 200_000 - result = TestClient(app, raise_server_exceptions=False).post( - '/value', content=b'[' * depth + b']' * depth, headers={'Content-Type': 'application/json'} - ) + + # A small TestClient worker stack can overflow in msgspec before RecursionError. + async def post() -> httpx.Response: + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=app, raise_app_exceptions=False), + base_url='http://test', + ) as client: + return await client.post( + '/value', + content=b'[' * depth + b']' * depth, + headers={'Content-Type': 'application/json'}, + ) + + result = asyncio.run(post()) assert result.status_code == 400