[rust-server] Fix panic on binary request bodies coerced to UTF-8#24116
Merged
wing328 merged 2 commits intoJun 24, 2026
Merged
Conversation
Contributor
Author
|
Tagging rust-server technical committee for review: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro @dsteele |
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 |
…-server-binary-request-body-utf8-panic
Member
|
thanks for the PR which has been merged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #24094
Problem
For an endpoint with a
format: binary(orformat: 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")beforeplacing 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(wrappingVec<u8>), whichcorrectly 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 emitBody::from(body), so the defect was isolated to the current rust-serverclient request template.
Fix
client-request-body-instance.mustache: forisBinary/isByteArraybodies, pass the raw
Vec<u8>through withbody_from_bytes(param.0)instead of
String::from_utf8(...).expect(...).client-mod.mustache: add abody_from_bytes(b: Vec<u8>)helpermirroring the existing
body_from_string.Tests
/required_binary_stream(format: binary) endpoint to theopenapi-v3rust-server test spec.RustServerCodegenTest.testBinaryRequestBodyNotCoercedToUtf8,asserting the generated client uses
body_from_bytesand no longercontains the UTF-8 coercion. (Fails against old templates, passes with
the fix.)
cargo build.PR checklist
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.
Summary by cubic
Fixes a runtime panic in
rust-serverclients 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
isBinary/isByteArrayrequest bodies, usebody_from_bytes(param.0)inclient-request-body-instance.mustacheinstead ofString::from_utf8(...).body_from_bytes(b: Vec<u8>)helper inclient-mod.mustache, mirroringbody_from_string.body_from_string(...)for consistency.rust-server(andrust-server-deprecated) to reflect the fix.Tests
/required_binary_streamendpoint (format: binary) to the Rust server test spec.RustServerCodegenTest.testBinaryRequestBodyNotCoercedToUtf8to assertbody_from_bytesis used and UTF-8 coercion is gone.cargo build.Written for commit 7034d01. Summary will update on new commits.