From 750285ecbe245907f2b38f8a7b5f4982327528fc Mon Sep 17 00:00:00 2001 From: valbort Date: Fri, 22 May 2026 14:10:23 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Format=20code=20and=20add=20NotI?= =?UTF-8?q?mplementedError=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a fallback to json.loads when Pydantic's validate_json throws NotImplementedError. This fixes compatibility issues with older Pydantic versions in CI testing for endpoints utilizing arbitrary types (like Missing). Also ran Ruff to fix formatting and linting issues reported by pre-commit hooks. --- fastapi/dependencies/utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index b02640088f..dcda3e6689 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -755,7 +755,23 @@ def _validate_value_with_model_field( and not _is_json_field(field) ): if isinstance(value, FastAPIOptimizedJsonBytes): - return field.validate_json(value, values, loc=loc) + try: + return field.validate_json(value, values, loc=loc) + except NotImplementedError: + try: + import json + + value = json.loads(value) + except json.JSONDecodeError as e: + return None, [ + { + "type": "json_invalid", + "loc": loc, + "msg": "JSON decode error", + "input": {}, + "ctx": {"error": e.msg}, + } + ] return field.validate(value, values, loc=loc) # If it's a scalar and we have bytes, we MUST decode it first because Pydantic's