From be09f8a8683438fbeb995865bf1a03158075e68e Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Thu, 15 Aug 2024 10:03:59 +0100 Subject: [PATCH] [Rust Server] Use swagger/multipart_related support (#19354) * [Rust Server] Use swagger/multipart_related support * Update samples --- .../main/resources/rust-server/Cargo.mustache | 4 +- .../rust-server/client-imports.mustache | 2 +- .../generate-multipart-related.mustache | 11 +---- ...er-request-body-multipart-related.mustache | 20 +++------- .../output/multipart-v3/Cargo.toml | 4 +- .../output/multipart-v3/src/client/mod.rs | 24 ++--------- .../output/multipart-v3/src/server/mod.rs | 40 +++++-------------- 7 files changed, 24 insertions(+), 81 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache b/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache index 1cd664417e0e..b499b7159637 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache @@ -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", @@ -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", diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache index f4a0bbd93cc1..d2a4b3fcc252 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-imports.mustache @@ -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; diff --git a/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache b/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache index ff2330c07d4d..94ab9db18c24 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache index 83f18f2df0c1..b9dc330ef3f8 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache @@ -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::().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() diff --git a/samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml b/samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml index 2dec3ee847fa..38e6d825b77e 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml +++ b/samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml @@ -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"] diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs index 2d52eec0a593..9f6583b96c4f 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs @@ -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; @@ -424,16 +424,7 @@ impl Api for Client 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 { @@ -720,16 +711,7 @@ impl Api for Client 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 { diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs index d4ca550e5ea0..a87d3ba90749 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs @@ -164,30 +164,20 @@ impl hyper::service::Service<(Request, C)> for Service where let mut unused_elements : Vec = 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::().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() @@ -465,30 +455,20 @@ impl hyper::service::Service<(Request, C)> for Service where let mut unused_elements : Vec = 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::().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()