Skip to content

fix(coder): decode cached JSON as the annotated return type (#576) - #4

Merged
agustin18 merged 8 commits into
stablefrom
fix/pydantic-decode
Nov 13, 2026
Merged

agustin18 merged 8 commits into
stablefrom
fix/pydantic-decode

Conversation

@agustin18

@agustin18 agustin18 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Keep Coder.decode_as_type and JsonCoder on the legacy decoded behavior.
  • Add opt-in TypedJsonCoder for Pydantic v2 typed restoration.
  • Detect Pydantic v1 models in composite annotations (List[V1Item], Optional[V1Item], unions) and fall back to decoded JSON.
  • Restrict the adapter-construction fallback to expected schema errors; let ValidationError and validator TypeError from Pydantic v2 propagate.
  • Do not force pydantic>=2; the package keeps the legacy dependency range.
  • Add regression tests for typed decode, Pydantic v1 composites, Response/JSONResponse HITs, missing TypeAdapter, custom coders, and a v2 validator raising TypeError.
  • Stabilize the datetime expiry test.

Motivation

Pydantic v2 removed ModelField, so cache hits returned raw JSON. The improvement is useful, but it must not change the default behavior of existing applications. The default JsonCoder therefore stays legacy; typed restoration is an explicit opt-in. This is upstream long2ice#249/long2ice#576, with credit to bezff.

Compatibility policy

  • Migration from fastapi-cache2 does not require application code changes for valid legacy use cases.
  • Default JsonCoder: legacy decoded behavior.
  • Opt-in TypedJsonCoder: Pydantic v2 typed restoration, with validation errors propagated.
  • Pydantic v1, Response/JSONResponse and unsupported annotations: legacy decoded behavior.
  • Intentional bug fixes are documented in MIGRATION.md.
  • A legacy compatibility matrix (Python 3.8/3.9, Pydantic v1/v2) is planned before 0.3.0a1.

Enable typed restoration globally:

from fastapi_cache import FastAPICache
from fastapi_cache.coder import TypedJsonCoder

FastAPICache.init(backend, coder=TypedJsonCoder)

Or per endpoint with @cache(coder=TypedJsonCoder).

Tests

  • 45 passed, 0 xfailed.
  • diff-cover: 100% on 29 changed runtime lines.
  • Response/JSONResponse endpoint tests use a counter to prove MISS → HIT.
  • Pydantic v1 direct and composite (List[V1Item], Optional[V1Item]) fall back to decoded values.
  • A Pydantic v2 field_validator raising TypeError propagates instead of returning a dict.
  • Custom coder test confirms the legacy decode_as_type path.
  • Wheel smoke: default JsonCoder returns dict; TypedJsonCoder restores Pydantic v2 models; v1/Response fallbacks return dict/list.
  • Ruff, pre-commit, changed-files mypy, build, twine check, artifact inspection: passed.

Migration notes

See the “Typed JSON decode” row in MIGRATION.md. No code changes are required unless you opt into TypedJsonCoder.

Checklist

  • Tests added/updated
  • Changelog fragment
  • Docs updated
  • No secrets
  • Backward compatibility reviewed
  • No unrelated runtime changes

Follow-ups

Related

…#576)

Use a cached Pydantic v2 TypeAdapter to restore annotated models, dataclasses, tuples and lists on cache hits. Remove the obsolete ModelField stub. Add the List[PDItem] case, activate the four previously xfailed codec tests, and update the migration/compatibility status. Adapted from upstream PR long2ice#576 by bezff.
Sleep past the integer-second TTL boundary so the expiry assertion is deterministic.
TypeAdapter is Pydantic v2-only; make the dependency explicit and refresh the lockfile.
@agustin18 agustin18 added bug Something isn't working compatibility Compatibility contract and migration labels Nov 13, 2026
GitHub assigned #4 to the Pydantic decode PR because a Dependabot PR took #3.
Fall back to the decoded JSON for Pydantic v1 models, Response/JSONResponse annotations, and environments without TypeAdapter. Keep typed restoration on Pydantic v2. Add regression tests for both fallbacks and endpoint annotations.
Keep legacy environment compatibility; TypeAdapter is optional and Pydantic v1 uses the decoded fallback.
Keep Coder.decode_as_type and JsonCoder on the legacy decoded behavior. Add TypedJsonCoder for Pydantic v2 typed restoration, with fallbacks for Pydantic v1, Response/JSONResponse and missing TypeAdapter. Add regression tests for v1 composites, Response HITs and custom coders.
Detect Pydantic v1 models in composite annotations before building a TypeAdapter. Restrict the adapter-construction fallback to expected schema errors. Let TypeError and ValidationError from validate_python propagate. Add a regression test for a v2 field_validator raising TypeError.
@agustin18
agustin18 merged commit b85aadf into stable Nov 13, 2026
12 checks passed
@agustin18
agustin18 deleted the fix/pydantic-decode branch November 13, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working compatibility Compatibility contract and migration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant