Skip to content

Commit 41f97d8

Browse files
authored
fix: Rust-server bytes response fixed to not attempt string conversion (#22471)
1 parent 5a2503d commit 41f97d8

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ client = [
4444
"serde_urlencoded",
4545
{{/usesUrlEncodedForm}}
4646
{{#hasCallbacks}}
47-
"serde_ignored", "percent-encoding", {{^apiUsesByteArray}}"lazy_static", "regex"{{/apiUsesByteArray}}
47+
"serde_ignored", "percent-encoding", {{^apiUsesByteArray}}"lazy_static", "regex",{{/apiUsesByteArray}}
4848
{{/hasCallbacks}}
4949
{{! Anything added to the list below, should probably be added to the callbacks list below }}
5050
"hyper", "hyper-util/http1", "hyper-util/http2", "hyper-openssl", "hyper-tls", "native-tls", "openssl", "url"

modules/openapi-generator/src/main/resources/rust-server/client-request-body-multipart-form.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let (body_string, multipart_header) = {
1+
let (body_bytes, multipart_header) = {
22
let mut multipart = Multipart::new();
33
44
{{#exts}}
@@ -43,9 +43,9 @@
4343
Err(err) => return Err(ApiError(format!("Unable to build request: {err}"))),
4444
};
4545

46-
let mut body_string = String::new();
46+
let mut body_bytes = Vec::new();
4747

48-
match fields.read_to_string(&mut body_string) {
48+
match fields.read_to_end(&mut body_bytes) {
4949
Ok(_) => (),
5050
Err(err) => return Err(ApiError(format!("Unable to build body: {err}"))),
5151
}
@@ -54,10 +54,10 @@
5454

5555
let multipart_header = format!("multipart/form-data;boundary={boundary}");
5656

57-
(body_string, multipart_header)
58-
};
57+
(body_bytes, multipart_header)
58+
};
5959

60-
*request.body_mut() = body_from_string(body_string);
60+
*request.body_mut() = BoxBody::new(Full::new(Bytes::from(body_bytes)));
6161

6262
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(&multipart_header) {
6363
Ok(h) => h,

modules/openapi-generator/src/main/resources/rust-server/server-response-body-instance.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{{/x-produces-json}}
2424
{{#x-produces-bytes}}
2525
// Binary Body
26-
let body = String::from_utf8(body.0).expect("Error converting octet stream to string");
26+
*response.body_mut() = BoxBody::new(Full::new(Bytes::from(body.0)));
2727
{{/x-produces-bytes}}
2828
{{#x-produces-plain-text}}
2929
// Plain text Body
@@ -42,7 +42,12 @@
4242
&["multipart/related; boundary=".as_bytes(), &boundary].concat())
4343
.expect("Unable to create Content-Type header for multipart/related"));
4444
{{/formParams}}
45+
*response.body_mut() = BoxBody::new(Full::new(Bytes::from(body.0)));
4546
{{/x-produces-multipart-related}}
46-
{{/exts}}
47+
{{^x-produces-bytes}}
48+
{{^x-produces-multipart-related}}
4749
*response.body_mut() = body_from_string(body);
50+
{{/x-produces-multipart-related}}
51+
{{/x-produces-bytes}}
52+
{{/exts}}
4853
{{/dataType}}

samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ impl<S, C, B> Api<C> for Client<S, C> where
590590
};
591591

592592
// Consumes multipart/form body
593-
let (body_string, multipart_header) = {
593+
let (body_bytes, multipart_header) = {
594594
let mut multipart = Multipart::new();
595595

596596
// For each parameter, encode as appropriate and add to the multipart body as a stream.
@@ -649,9 +649,9 @@ impl<S, C, B> Api<C> for Client<S, C> where
649649
Err(err) => return Err(ApiError(format!("Unable to build request: {err}"))),
650650
};
651651

652-
let mut body_string = String::new();
652+
let mut body_bytes = Vec::new();
653653

654-
match fields.read_to_string(&mut body_string) {
654+
match fields.read_to_end(&mut body_bytes) {
655655
Ok(_) => (),
656656
Err(err) => return Err(ApiError(format!("Unable to build body: {err}"))),
657657
}
@@ -660,10 +660,10 @@ impl<S, C, B> Api<C> for Client<S, C> where
660660

661661
let multipart_header = format!("multipart/form-data;boundary={boundary}");
662662

663-
(body_string, multipart_header)
664-
};
663+
(body_bytes, multipart_header)
664+
};
665665

666-
*request.body_mut() = body_from_string(body_string);
666+
*request.body_mut() = BoxBody::new(Full::new(Bytes::from(body_bytes)));
667667

668668
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(&multipart_header) {
669669
Ok(h) => h,

samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,7 @@ impl<T, C, ReqBody> hyper::service::Service<(Request<ReqBody>, C)> for Service<T
795795
CONTENT_TYPE,
796796
HeaderValue::from_static("application/octet-stream"));
797797
// Binary Body
798-
let body = String::from_utf8(body.0).expect("Error converting octet stream to string");
799-
*response.body_mut() = body_from_string(body);
798+
*response.body_mut() = BoxBody::new(Full::new(Bytes::from(body.0)));
800799

801800
},
802801
MultigetGetResponse::StringRsp

samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,7 +2650,7 @@ impl<S, C, B> Api<C> for Client<S, C> where
26502650
};
26512651

26522652
// Consumes multipart/form body
2653-
let (body_string, multipart_header) = {
2653+
let (body_bytes, multipart_header) = {
26542654
let mut multipart = Multipart::new();
26552655

26562656
// For each parameter, encode as appropriate and add to the multipart body as a stream.
@@ -2684,9 +2684,9 @@ impl<S, C, B> Api<C> for Client<S, C> where
26842684
Err(err) => return Err(ApiError(format!("Unable to build request: {err}"))),
26852685
};
26862686

2687-
let mut body_string = String::new();
2687+
let mut body_bytes = Vec::new();
26882688

2689-
match fields.read_to_string(&mut body_string) {
2689+
match fields.read_to_end(&mut body_bytes) {
26902690
Ok(_) => (),
26912691
Err(err) => return Err(ApiError(format!("Unable to build body: {err}"))),
26922692
}
@@ -2695,10 +2695,10 @@ impl<S, C, B> Api<C> for Client<S, C> where
26952695

26962696
let multipart_header = format!("multipart/form-data;boundary={boundary}");
26972697

2698-
(body_string, multipart_header)
2699-
};
2698+
(body_bytes, multipart_header)
2699+
};
27002700

2701-
*request.body_mut() = body_from_string(body_string);
2701+
*request.body_mut() = BoxBody::new(Full::new(Bytes::from(body_bytes)));
27022702

27032703
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(&multipart_header) {
27042704
Ok(h) => h,

0 commit comments

Comments
 (0)