Skip to content

[BUG] rust-server codegen: format: binary request body incorrectly coerced to UTF-8 via String::from_utf8(...).expect() — runtime panic on any non-UTF-8 payload #24094

Description

@twistali

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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
  1. Generate a Rust server/client from the above spec using the rust-server generator
  2. Open the generated src/client/mod.rs and locate the upload_file method
  3. 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);
  4. Call the method with a swagger::ByteArray containing any byte > 127 (e.g. any real gzip file)
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions