Browse Source

🎨 Format code and add NotImplementedError fallback

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.
pull/15584/head
valbort 2 weeks ago
parent
commit
750285ecbe
  1. 18
      fastapi/dependencies/utils.py

18
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

Loading…
Cancel
Save