Bug Report Checklist
Description
For an endpoint with a format: binary request body (e.g. application/x-gzip), the generated Rust client coerces the body bytes through String::from_utf8(...).expect("Body was not valid UTF8") before placing them in the HTTP request. This causes a runtime panic on any payload containing non-UTF-8 bytes — i.e., any real binary content (gzip, protobuf, encrypted blobs, images).
The generated parameter type is swagger::ByteArray (wrapping Vec), which correctly models arbitrary octets. The bug is that the serialization path incorrectly assumes text encoding. The defect is asymmetric: the response path in the same generated crate correctly handles format: binary as raw bytes — only the request body path panics.
openapi-generator version
Present across all 7.x releases
OpenAPI declaration file content or url
openapi: "3.0.3"
info:
title: Binary Upload Example
version: "1.0.0"
paths:
/files/{id}/upload:
put:
operationId: uploadFile
parameters:
- name: id
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
application/x-gzip:
schema:
type: string
format: binary
responses:
'200':
description: File uploaded successfully
content:
application/json:
schema:
type: object
properties:
message:
type: string
Any endpoint with format: binary on a request body triggers this bug.
Generation Details
No special options required — the default rust-server generator with any format: binary request body reproduces the issue.
Steps to reproduce
- Generate a Rust server/client from the above spec using the
rust-server generator
- Open the generated
src/client/mod.rs and locate the upload_file method
- Observe the body serialization:
let body = String::from_utf8(param_body.0).expect("Body was not valid UTF8");
*request.body_mut() = body_from_string(body);
- Call the method with a
swagger::ByteArray containing any byte > 127 (e.g. any real gzip file)
- Panic:
thread panicked at 'Body was not valid UTF8: FromUtf8Error { bytes: [...], error: Utf8Error { valid_up_to: 0, error_len: Some(1) } }'
Expected: Raw bytes should be passed directly without UTF-8 coercion, as was done in swagger-5/6:
let body = param_body.0;
*request.body_mut() = Body::from(body);
Related issues/PRs
Suggest a fix
The Mustache template for client request body serialization needs to distinguish format: binary body parameters. When the schema format is binary, the template should emit:
let body = param_body.0;
*request.body_mut() = Body::from(body);
instead of the current:
let body = String::from_utf8(param_body.0).expect("Body was not valid UTF8");
*request.body_mut() = body_from_string(body);
The condition likely needs to check the isBinary or format == "binary" template variable and skip the String::from_utf8 path. Note that the response-handling templates already do this correctly — only the request serialization template is affected.
Bug Report Checklist
Description
For an endpoint with a format: binary request body (e.g. application/x-gzip), the generated Rust client coerces the body bytes through String::from_utf8(...).expect("Body was not valid UTF8") before placing them in the HTTP request. This causes a runtime panic on any payload containing non-UTF-8 bytes — i.e., any real binary content (gzip, protobuf, encrypted blobs, images).
The generated parameter type is swagger::ByteArray (wrapping Vec), which correctly models arbitrary octets. The bug is that the serialization path incorrectly assumes text encoding. The defect is asymmetric: the response path in the same generated crate correctly handles format: binary as raw bytes — only the request body path panics.
openapi-generator version
Present across all 7.x releases
OpenAPI declaration file content or url
Any endpoint with
format: binaryon a request body triggers this bug.Generation Details
No special options required — the default
rust-servergenerator with anyformat: binaryrequest body reproduces the issue.Steps to reproduce
rust-servergeneratorsrc/client/mod.rsand locate theupload_filemethodswagger::ByteArraycontaining any byte > 127 (e.g. any real gzip file)thread panicked at 'Body was not valid UTF8: FromUtf8Error { bytes: [...], error: Utf8Error { valid_up_to: 0, error_len: Some(1) } }'Expected: Raw bytes should be passed directly without UTF-8 coercion, as was done in swagger-5/6:
Related issues/PRs
[BUG] [RUST-SERVER] binary strings should not be base64 encoded(same code area, different symptom — base64 vs UTF-8 coercion)rust-serverwithapplication/octet-streamgenerates string instead ofVec<u8>(type mapping, closely related)filetype? #538 —[rust-server] how to cope with loss of file type?(design discussion on binary/byte/file mapping in OpenAPI 3)Suggest a fix
The Mustache template for client request body serialization needs to distinguish
format: binarybody parameters. When the schema format isbinary, the template should emit:instead of the current:
The condition likely needs to check the
isBinaryorformat == "binary"template variable and skip theString::from_utf8path. Note that the response-handling templates already do this correctly — only the request serialization template is affected.