[Rust Server] Use swagger/multipart_related support (#19354)

* [Rust Server] Use swagger/multipart_related support

* Update samples
This commit is contained in:
Richard Whitehouse 2024-08-15 10:03:59 +01:00 committed by GitHub
parent 38ebf0bb4e
commit be09f8a868
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 24 additions and 81 deletions

View File

@ -41,7 +41,7 @@ client = [
"multipart", "multipart/client", "swagger/multipart_form", "multipart", "multipart/client", "swagger/multipart_form",
{{/apiUsesMultipartFormData}} {{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}} {{#apiUsesMultipartRelated}}
"hyper_0_10", "mime_multipart", "hyper_0_10", "mime_multipart", "swagger/multipart_related",
{{/apiUsesMultipartRelated}} {{/apiUsesMultipartRelated}}
{{#usesUrlEncodedForm}} {{#usesUrlEncodedForm}}
"serde_urlencoded", "serde_urlencoded",
@ -60,7 +60,7 @@ server = [
"multipart", "multipart/server", "swagger/multipart_form", "multipart", "multipart/server", "swagger/multipart_form",
{{/apiUsesMultipartFormData}} {{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}} {{#apiUsesMultipartRelated}}
"hyper_0_10", "mime_multipart", "hyper_0_10", "mime_multipart", "swagger/multipart_related",
{{/apiUsesMultipartRelated}} {{/apiUsesMultipartRelated}}
{{#hasCallbacks}} {{#hasCallbacks}}
"native-tls", "hyper-openssl", "hyper-tls", "openssl", "native-tls", "hyper-openssl", "hyper-tls", "openssl",

View File

@ -26,7 +26,7 @@ use multipart::client::lazy::Multipart;
{{/apiUsesMultipartFormData}} {{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}} {{#apiUsesMultipartRelated}}
use hyper_0_10::header::{Headers, ContentType}; use hyper_0_10::header::{Headers, ContentType};
use mime_multipart::{Node, Part, generate_boundary, write_multipart}; use mime_multipart::{Node, Part, write_multipart};
{{/apiUsesMultipartRelated}} {{/apiUsesMultipartRelated}}
use crate::models; use crate::models;

View File

@ -1,14 +1,5 @@
{{#-first}} {{#-first}}
// Construct the Body for a multipart/related request. The mime 0.2.6 library let boundary = swagger::multipart::related::generate_boundary();
// 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 mut body_parts = vec![]; let mut body_parts = vec![];
{{/-first}} {{/-first}}

View File

@ -1,29 +1,19 @@
// Get multipart chunks. // Get multipart chunks.
// Extract the top-level content type header. // Create headers from top-level content type header.
let content_type_mime = headers let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
.get(CONTENT_TYPE) Ok(headers) => headers,
.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));
},
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST) .status(StatusCode::BAD_REQUEST)
.body(Body::from(e)) .body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for {{operationId}}")); .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 // &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice. // 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, Ok(nodes) => nodes,
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()

View File

@ -12,13 +12,13 @@ default = ["client", "server"]
client = [ client = [
"mime_0_2", "mime_0_2",
"multipart", "multipart/client", "swagger/multipart_form", "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" "hyper", "hyper-openssl", "hyper-tls", "native-tls", "openssl", "url"
] ]
server = [ server = [
"mime_0_2", "mime_0_2",
"multipart", "multipart/server", "swagger/multipart_form", "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" "serde_ignored", "hyper", "regex", "percent-encoding", "url", "lazy_static"
] ]
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"] conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]

View File

@ -23,7 +23,7 @@ use mime::Mime;
use std::io::Cursor; use std::io::Cursor;
use multipart::client::lazy::Multipart; use multipart::client::lazy::Multipart;
use hyper_0_10::header::{Headers, ContentType}; 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::models;
use crate::header; use crate::header;
@ -424,16 +424,7 @@ impl<S, C> Api<C> for Client<S, C> where
}; };
// Consumes multipart/related body // Consumes multipart/related body
// Construct the Body for a multipart/related request. The mime 0.2.6 library let boundary = swagger::multipart::related::generate_boundary();
// 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 mut body_parts = vec![]; let mut body_parts = vec![];
if let Some(object_field) = param_object_field { if let Some(object_field) = param_object_field {
@ -720,16 +711,7 @@ impl<S, C> Api<C> for Client<S, C> where
}; };
// Consumes multipart/related body // Consumes multipart/related body
// Construct the Body for a multipart/related request. The mime 0.2.6 library let boundary = swagger::multipart::related::generate_boundary();
// 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 mut body_parts = vec![]; let mut body_parts = vec![];
if let Some(binary1) = param_binary1 { if let Some(binary1) = param_binary1 {

View File

@ -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![]; let mut unused_elements : Vec<String> = vec![];
// Get multipart chunks. // Get multipart chunks.
// Extract the top-level content type header. // Create headers from top-level content type header.
let content_type_mime = headers let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
.get(CONTENT_TYPE) Ok(headers) => headers,
.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));
},
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST) .status(StatusCode::BAD_REQUEST)
.body(Body::from(e)) .body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for MultipartRelatedRequestPost")); .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 // &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice. // 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, Ok(nodes) => nodes,
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()
@ -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![]; let mut unused_elements : Vec<String> = vec![];
// Get multipart chunks. // Get multipart chunks.
// Extract the top-level content type header. // Create headers from top-level content type header.
let content_type_mime = headers let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
.get(CONTENT_TYPE) Ok(headers) => headers,
.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));
},
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST) .status(StatusCode::BAD_REQUEST)
.body(Body::from(e)) .body(Body::from(e))
.expect("Unable to create Bad Request response due to unable to read content-type header for MultipleIdenticalMimeTypesPost")); .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 // &*body expresses the body as a byteslice, &mut provides a
// mutable reference to that byteslice. // 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, Ok(nodes) => nodes,
Err(e) => { Err(e) => {
return Ok(Response::builder() return Ok(Response::builder()