Skip to content

Commit

Permalink
[Rust Server] Use swagger/multipart_related support (OpenAPITools#19354)
Browse files Browse the repository at this point in the history
* [Rust Server] Use swagger/multipart_related support

* Update samples
  • Loading branch information
richardwhiuk authored Aug 15, 2024
1 parent 38ebf0b commit be09f8a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ client = [
"multipart", "multipart/client", "swagger/multipart_form",
{{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}}
"hyper_0_10", "mime_multipart",
"hyper_0_10", "mime_multipart", "swagger/multipart_related",
{{/apiUsesMultipartRelated}}
{{#usesUrlEncodedForm}}
"serde_urlencoded",
Expand All @@ -60,7 +60,7 @@ server = [
"multipart", "multipart/server", "swagger/multipart_form",
{{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}}
"hyper_0_10", "mime_multipart",
"hyper_0_10", "mime_multipart", "swagger/multipart_related",
{{/apiUsesMultipartRelated}}
{{#hasCallbacks}}
"native-tls", "hyper-openssl", "hyper-tls", "openssl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use multipart::client::lazy::Multipart;
{{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}}
use hyper_0_10::header::{Headers, ContentType};
use mime_multipart::{Node, Part, generate_boundary, write_multipart};
use mime_multipart::{Node, Part, write_multipart};
{{/apiUsesMultipartRelated}}

use crate::models;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{{#-first}}
// Construct the Body for a multipart/related request. The mime 0.2.6 library
// does not parse quoted-string parameters correctly. The boundary doesn't
// need to be a quoted string if it does not contain a '/', hence ensure
// no such boundary is used.
let mut boundary = generate_boundary();
for b in boundary.iter_mut() {
if b == &(b'/') {
*b = b'=';
}
}
let boundary = swagger::multipart::related::generate_boundary();
let mut body_parts = vec![];

{{/-first}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
// Get multipart chunks.

// Extract the top-level content type header.
let content_type_mime = headers
.get(CONTENT_TYPE)
.ok_or_else(|| "Missing content-type header".to_string())
.and_then(|v| v.to_str().map_err(|e| format!("Couldn't read content-type header value for {{operationId}}: {}", e)))
.and_then(|v| v.parse::<Mime2>().map_err(|_e| "Couldn't parse content-type header value for {{operationId}}".to_string()));

// Insert top-level content type header into a Headers object.
let mut multi_part_headers = Headers::new();
match content_type_mime {
Ok(content_type_mime) => {
multi_part_headers.set(ContentType(content_type_mime));
},
// Create headers from top-level content type header.
let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
Ok(headers) => headers,
Err(e) => {
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for {{operationId}}"));
}
}
};

// &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice.
let nodes = match read_multipart_body(&mut&*body, &multi_part_headers, false) {
let nodes = match read_multipart_body(&mut&*body, &multipart_headers, false) {
Ok(nodes) => nodes,
Err(e) => {
return Ok(Response::builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ default = ["client", "server"]
client = [
"mime_0_2",
"multipart", "multipart/client", "swagger/multipart_form",
"hyper_0_10", "mime_multipart",
"hyper_0_10", "mime_multipart", "swagger/multipart_related",
"hyper", "hyper-openssl", "hyper-tls", "native-tls", "openssl", "url"
]
server = [
"mime_0_2",
"multipart", "multipart/server", "swagger/multipart_form",
"hyper_0_10", "mime_multipart",
"hyper_0_10", "mime_multipart", "swagger/multipart_related",
"serde_ignored", "hyper", "regex", "percent-encoding", "url", "lazy_static"
]
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use mime::Mime;
use std::io::Cursor;
use multipart::client::lazy::Multipart;
use hyper_0_10::header::{Headers, ContentType};
use mime_multipart::{Node, Part, generate_boundary, write_multipart};
use mime_multipart::{Node, Part, write_multipart};

use crate::models;
use crate::header;
Expand Down Expand Up @@ -424,16 +424,7 @@ impl<S, C> Api<C> for Client<S, C> where
};

// Consumes multipart/related body
// Construct the Body for a multipart/related request. The mime 0.2.6 library
// does not parse quoted-string parameters correctly. The boundary doesn't
// need to be a quoted string if it does not contain a '/', hence ensure
// no such boundary is used.
let mut boundary = generate_boundary();
for b in boundary.iter_mut() {
if b == &(b'/') {
*b = b'=';
}
}
let boundary = swagger::multipart::related::generate_boundary();
let mut body_parts = vec![];

if let Some(object_field) = param_object_field {
Expand Down Expand Up @@ -720,16 +711,7 @@ impl<S, C> Api<C> for Client<S, C> where
};

// Consumes multipart/related body
// Construct the Body for a multipart/related request. The mime 0.2.6 library
// does not parse quoted-string parameters correctly. The boundary doesn't
// need to be a quoted string if it does not contain a '/', hence ensure
// no such boundary is used.
let mut boundary = generate_boundary();
for b in boundary.iter_mut() {
if b == &(b'/') {
*b = b'=';
}
}
let boundary = swagger::multipart::related::generate_boundary();
let mut body_parts = vec![];

if let Some(binary1) = param_binary1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,30 +164,20 @@ impl<T, C> hyper::service::Service<(Request<Body>, C)> for Service<T, C> where
let mut unused_elements : Vec<String> = vec![];
// Get multipart chunks.

// Extract the top-level content type header.
let content_type_mime = headers
.get(CONTENT_TYPE)
.ok_or_else(|| "Missing content-type header".to_string())
.and_then(|v| v.to_str().map_err(|e| format!("Couldn't read content-type header value for MultipartRelatedRequestPost: {}", e)))
.and_then(|v| v.parse::<Mime2>().map_err(|_e| "Couldn't parse content-type header value for MultipartRelatedRequestPost".to_string()));

// Insert top-level content type header into a Headers object.
let mut multi_part_headers = Headers::new();
match content_type_mime {
Ok(content_type_mime) => {
multi_part_headers.set(ContentType(content_type_mime));
},
// Create headers from top-level content type header.
let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
Ok(headers) => headers,
Err(e) => {
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for MultipartRelatedRequestPost"));
}
}
};

// &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice.
let nodes = match read_multipart_body(&mut&*body, &multi_part_headers, false) {
let nodes = match read_multipart_body(&mut&*body, &multipart_headers, false) {
Ok(nodes) => nodes,
Err(e) => {
return Ok(Response::builder()
Expand Down Expand Up @@ -465,30 +455,20 @@ impl<T, C> hyper::service::Service<(Request<Body>, C)> for Service<T, C> where
let mut unused_elements : Vec<String> = vec![];
// Get multipart chunks.

// Extract the top-level content type header.
let content_type_mime = headers
.get(CONTENT_TYPE)
.ok_or_else(|| "Missing content-type header".to_string())
.and_then(|v| v.to_str().map_err(|e| format!("Couldn't read content-type header value for MultipleIdenticalMimeTypesPost: {}", e)))
.and_then(|v| v.parse::<Mime2>().map_err(|_e| "Couldn't parse content-type header value for MultipleIdenticalMimeTypesPost".to_string()));

// Insert top-level content type header into a Headers object.
let mut multi_part_headers = Headers::new();
match content_type_mime {
Ok(content_type_mime) => {
multi_part_headers.set(ContentType(content_type_mime));
},
// Create headers from top-level content type header.
let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
Ok(headers) => headers,
Err(e) => {
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for MultipleIdenticalMimeTypesPost"));
}
}
};

// &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice.
let nodes = match read_multipart_body(&mut&*body, &multi_part_headers, false) {
let nodes = match read_multipart_body(&mut&*body, &multipart_headers, false) {
Ok(nodes) => nodes,
Err(e) => {
return Ok(Response::builder()
Expand Down

0 comments on commit be09f8a

Please sign in to comment.