Skip to content

[rust-server] Fix panic on binary request bodies coerced to UTF-8#24116

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
twistali:fix/rust-server-binary-request-body-utf8-panic
Jun 24, 2026
Merged

[rust-server] Fix panic on binary request bodies coerced to UTF-8#24116
wing328 merged 2 commits into
OpenAPITools:masterfrom
twistali:fix/rust-server-binary-request-body-utf8-panic

Conversation

@twistali

@twistali twistali commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #24094

Problem

For an endpoint with a format: binary (or format: byte) request body,
the generated rust-server client coerced the body bytes through
String::from_utf8(param_body.0).expect("Body was not valid UTF8") before
placing them in the HTTP request. This panics at runtime on any payload
containing a byte > 127 — i.e. any real binary content (gzip, protobuf,
images, encrypted blobs).

The parameter type is swagger::ByteArray (wrapping Vec<u8>), which
correctly models arbitrary octets — only the request serialization path
assumed text encoding. The response path already handled binary correctly,
and the older rust-server-deprecated (swagger 5/6) templates already emit
Body::from(body), so the defect was isolated to the current rust-server
client request template.

Fix

  • client-request-body-instance.mustache: for isBinary/isByteArray
    bodies, pass the raw Vec<u8> through with body_from_bytes(param.0)
    instead of String::from_utf8(...).expect(...).
  • client-mod.mustache: add a body_from_bytes(b: Vec<u8>) helper
    mirroring the existing body_from_string.

Tests

  • Added a /required_binary_stream (format: binary) endpoint to the
    openapi-v3 rust-server test spec.
  • Added RustServerCodegenTest.testBinaryRequestBodyNotCoercedToUtf8,
    asserting the generated client uses body_from_bytes and no longer
    contains the UTF-8 coercion. (Fails against old templates, passes with
    the fix.)
  • Regenerated sample compiles cleanly with cargo build.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes a runtime panic in rust-server clients by sending raw bytes for binary request bodies instead of coercing to UTF-8. Real binary payloads (gzip, protobuf, images, etc.) now work without crashing.

  • Bug Fixes

    • For isBinary/isByteArray request bodies, use body_from_bytes(param.0) in client-request-body-instance.mustache instead of String::from_utf8(...).
    • Add body_from_bytes(b: Vec<u8>) helper in client-mod.mustache, mirroring body_from_string.
    • Explicitly set request bodies for text and XML via body_from_string(...) for consistency.
    • Regenerate samples across rust-server (and rust-server-deprecated) to reflect the fix.
  • Tests

    • Add /required_binary_stream endpoint (format: binary) to the Rust server test spec.
    • Add RustServerCodegenTest.testBinaryRequestBodyNotCoercedToUtf8 to assert body_from_bytes is used and UTF-8 coercion is gone.
    • Regenerated samples compile with cargo build.

Written for commit 7034d01. Summary will update on new commits.

Review in cubic

@twistali

Copy link
Copy Markdown
Contributor Author

Tagging rust-server technical committee for review: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteele

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 28 files

Re-trigger cubic

@wing328

wing328 commented Jun 24, 2026

Copy link
Copy Markdown
Member

https://github.com/OpenAPITools/openapi-generator/actions/runs/28086139450/job/83154058834?pr=24116 can be ignored.

update: tests for rust-server-deprecated removed via #24117

@wing328 wing328 merged commit 2d65ec3 into OpenAPITools:master Jun 24, 2026
19 checks passed
@wing328

wing328 commented Jun 24, 2026

Copy link
Copy Markdown
Member

thanks for the PR which has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants