Repository navigation
fix(coder): decode cached JSON as the annotated return type (#576) - #4
Merged
Merged
Conversation
…#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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Coder.decode_as_typeandJsonCoderon the legacy decoded behavior.TypedJsonCoderfor Pydantic v2 typed restoration.List[V1Item],Optional[V1Item], unions) and fall back to decoded JSON.ValidationErrorand validatorTypeErrorfrom Pydantic v2 propagate.pydantic>=2; the package keeps the legacy dependency range.Response/JSONResponseHITs, missingTypeAdapter, custom coders, and a v2 validator raisingTypeError.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 defaultJsonCodertherefore stays legacy; typed restoration is an explicit opt-in. This is upstream long2ice#249/long2ice#576, with credit to bezff.Compatibility policy
fastapi-cache2does not require application code changes for valid legacy use cases.JsonCoder: legacy decoded behavior.TypedJsonCoder: Pydantic v2 typed restoration, with validation errors propagated.Response/JSONResponseand unsupported annotations: legacy decoded behavior.MIGRATION.md.0.3.0a1.Enable typed restoration globally:
Or per endpoint with
@cache(coder=TypedJsonCoder).Tests
diff-cover: 100% on 29 changed runtime lines.Response/JSONResponseendpoint tests use a counter to prove MISS → HIT.List[V1Item],Optional[V1Item]) fall back to decoded values.field_validatorraisingTypeErrorpropagates instead of returning a dict.decode_as_typepath.JsonCoderreturns dict;TypedJsonCoderrestores Pydantic v2 models; v1/Response fallbacks return dict/list.twine check, artifact inspection: passed.Migration notes
See the “Typed JSON decode” row in
MIGRATION.md. No code changes are required unless you opt intoTypedJsonCoder.Checklist
Follow-ups
0.3.0a1.certifiPR chore(deps): bump certifi from 2024.7.4 to 2026.10.18 #3 is separate; avoid incidental lockfile format migration.Related