|
| 1 | +# Copyright (c) The OGX Contributors. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in |
| 5 | +# the root directory of this source tree. |
| 6 | + |
| 7 | +"""OpenAPI transforms related to multipart/form-data request schemas.""" |
| 8 | + |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +def normalize_multipart_binary_fields(openapi_schema: dict[str, Any]) -> dict[str, Any]: |
| 13 | + """Normalize multipart binary fields to OpenAPI's ``format: binary`` style. |
| 14 | +
|
| 15 | + FastAPI/Pydantic may emit JSON Schema 2020-12 style binary strings as: |
| 16 | + ``{"type": "string", "contentMediaType": "application/octet-stream"}``. |
| 17 | + Our v1 compatibility checks expect OpenAPI's legacy representation: |
| 18 | + ``{"type": "string", "format": "binary"}``. |
| 19 | +
|
| 20 | + Apply this conversion only for multipart/form-data request schemas. |
| 21 | + """ |
| 22 | + |
| 23 | + components = openapi_schema.get("components", {}).get("schemas", {}) |
| 24 | + paths = openapi_schema.get("paths", {}) |
| 25 | + if not isinstance(paths, dict): |
| 26 | + return openapi_schema |
| 27 | + |
| 28 | + ref_prefix = "#/components/schemas/" |
| 29 | + |
| 30 | + for path_item in paths.values(): |
| 31 | + if not isinstance(path_item, dict): |
| 32 | + continue |
| 33 | + for method in ("post", "put", "patch"): |
| 34 | + operation = path_item.get(method) |
| 35 | + if not isinstance(operation, dict): |
| 36 | + continue |
| 37 | + |
| 38 | + request_body = operation.get("requestBody", {}) |
| 39 | + if not isinstance(request_body, dict): |
| 40 | + continue |
| 41 | + content = request_body.get("content", {}) |
| 42 | + if not isinstance(content, dict): |
| 43 | + continue |
| 44 | + |
| 45 | + multipart = content.get("multipart/form-data") |
| 46 | + if not isinstance(multipart, dict): |
| 47 | + continue |
| 48 | + schema = multipart.get("schema") |
| 49 | + if not isinstance(schema, dict): |
| 50 | + continue |
| 51 | + |
| 52 | + targets: list[dict[str, Any]] = [] |
| 53 | + schema_ref = schema.get("$ref") |
| 54 | + if isinstance(schema_ref, str) and schema_ref.startswith(ref_prefix): |
| 55 | + schema_name = schema_ref[len(ref_prefix) :] |
| 56 | + component_schema = components.get(schema_name) |
| 57 | + if isinstance(component_schema, dict): |
| 58 | + targets.append(component_schema) |
| 59 | + else: |
| 60 | + targets.append(schema) |
| 61 | + |
| 62 | + for target_schema in targets: |
| 63 | + properties = target_schema.get("properties") |
| 64 | + if not isinstance(properties, dict): |
| 65 | + continue |
| 66 | + for field_schema in properties.values(): |
| 67 | + if not isinstance(field_schema, dict): |
| 68 | + continue |
| 69 | + if ( |
| 70 | + field_schema.get("type") == "string" |
| 71 | + and field_schema.get("contentMediaType") == "application/octet-stream" |
| 72 | + ): |
| 73 | + field_schema.pop("contentMediaType", None) |
| 74 | + field_schema["format"] = "binary" |
| 75 | + |
| 76 | + return openapi_schema |
0 commit comments