From 46292e5005ce7c7b387bc7047461f2a6f37ae568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 21 Feb 2026 12:28:23 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20JSON=20Schema?= =?UTF-8?q?=20for=20bytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/_compat/v2.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 777a22281..3936cd248 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -44,7 +44,12 @@ evaluate_forwardref = eval_type_lenient class GenerateJsonSchema(_GenerateJsonSchema): def bytes_schema(self, schema: CoreSchema) -> JsonSchemaValue: json_schema = {"type": "string", "contentMediaType": "application/octet-stream"} - if self._config.ser_json_bytes == "base64": + bytes_mode = ( + self._config.ser_json_bytes + if self.mode == "serialization" + else self._config.val_json_bytes + ) + if bytes_mode == "base64": json_schema["contentEncoding"] = "base64" self.update_with_validations(json_schema, schema, self.ValidationsMapping.bytes) return json_schema