From 83a12d24b0d4679ccea94d1fef3b941677624f05 Mon Sep 17 00:00:00 2001 From: Jarrod Overson Date: Fri, 20 Aug 2021 13:31:52 -0400 Subject: [PATCH] merged PR #9919 and updated dependencies --- .../main/resources/rust-server/Cargo.mustache | 21 +++--- .../rust-server/client-operation.mustache | 6 +- .../example-server-common.mustache | 68 ++++++++++++------- .../example-server-operation.mustache | 3 +- .../resources/rust-server/server-mod.mustache | 6 +- .../rust-server/server-operation.mustache | 6 +- .../typescript-fetch/runtime.mustache | 2 +- 7 files changed, 65 insertions(+), 47 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 a771f5c4090f..3729bb93223b 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache @@ -61,10 +61,10 @@ conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk- [target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))'.dependencies] native-tls = { version = "0.2", optional = true } -hyper-tls = { version = "0.4", optional = true } +hyper-tls = { version = "0.5", optional = true } [target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dependencies] -hyper-openssl = { version = "0.8", optional = true } +hyper-openssl = { version = "0.9", optional = true } openssl = {version = "0.10", optional = true } [dependencies] @@ -72,7 +72,7 @@ openssl = {version = "0.10", optional = true } async-trait = "0.1.24" chrono = { version = "0.4", features = ["serde"] } futures = "0.3" -swagger = "5.0.2" +swagger = { version = "6.1", features = ["client", "tcp", "tls"] } log = "0.4.0" mime = "0.3" @@ -96,7 +96,7 @@ uuid = {version = "0.8", features = ["serde", "v4"]} {{/apiUsesUuid}} # Common between server and client features -hyper = {version = "0.13", optional = true} +hyper = {version = "0.14", optional = true} {{#apiUsesMultipartRelated}} mime_multipart = {version = "0.5", optional = true} hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true} @@ -115,21 +115,22 @@ percent-encoding = {version = "2.1.0", optional = true} regex = {version = "1.3", optional = true} # Conversion -frunk = { version = "0.3.0", optional = true } -frunk_derives = { version = "0.3.0", optional = true } -frunk_core = { version = "0.3.0", optional = true } +frunk = { version = "0.4.0", optional = true } +frunk_derives = { version = "0.4.0", optional = true } +frunk_core = { version = "0.4.0", optional = true } frunk-enum-derive = { version = "0.2.0", optional = true } frunk-enum-core = { version = "0.2.0", optional = true } [dev-dependencies] clap = "2.25" -env_logger = "0.7" -tokio = { version = "0.2", features = ["rt-threaded", "macros", "stream"] } +env_logger = "0.9" +tokio = { version = "1.10.0", features = ["rt-multi-thread", "net", "macros"] } +tokio-stream = "0.1.7" native-tls = "0.2" tokio-tls = "0.3" [target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dev-dependencies] -tokio-openssl = "0.4" +tokio-openssl = "0.6.2" openssl = "0.10" [[example]] diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache index a4720abcc518..3e3250ab4f3d 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache @@ -418,7 +418,7 @@ let body = response.into_body(); {{#dataType}} let body = body - .to_raw() + .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; {{#vendorExtensions}} {{#x-produces-bytes}} @@ -434,7 +434,7 @@ .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; {{/x-produces-xml}} {{#x-produces-json}} - let body = serde_json::from_str::<{{{dataType}}}>(body)?; + let body = serde_json::from_str::<{{{dataType}}}>(body).map_err(|e| ApiError(format!("Could not de serialize {{{dataType}}}: {}", e)))?; {{/x-produces-json}} {{#x-produces-plain-text}} let body = body.to_string(); @@ -477,7 +477,7 @@ let headers = response.headers().clone(); let body = response.into_body() .take(100) - .to_raw().await; + .into_raw().await; Err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}", code, headers, diff --git a/modules/openapi-generator/src/main/resources/rust-server/example-server-common.mustache b/modules/openapi-generator/src/main/resources/rust-server/example-server-common.mustache index 14545b26867f..10c170ac7c12 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/example-server-common.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/example-server-common.mustache @@ -2,25 +2,41 @@ #![allow(unused_imports)] +use std::future::Future; +use std::marker::PhantomData; +use std::net::SocketAddr; +use std::pin::Pin; +use std::sync::{ + Arc, + Mutex, +}; +use std::task::{ + Context, + Poll, +}; + use async_trait::async_trait; -use futures::{future, Stream, StreamExt, TryFutureExt, TryStreamExt}; +use futures::{ + future, + Stream, + StreamExt, + TryFutureExt, + TryStreamExt, +}; use hyper::server::conn::Http; use hyper::service::Service; use log::info; -#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))] -use openssl::ssl::SslAcceptorBuilder; -use std::future::Future; -use std::marker::PhantomData; -use std::net::SocketAddr; -use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll}; -use swagger::{Has, XSpanIdString}; +use openssl::ssl::Ssl; use swagger::auth::MakeAllowAllAuthenticator; -use swagger::EmptyContext; +use swagger::{ + EmptyContext, + Has, + XSpanIdString, +}; use tokio::net::TcpListener; #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))] -use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; +use openssl::ssl::{SslAcceptorBuilder, SslAcceptor, SslFiletype, SslMethod}; use {{{externCrateName}}}::models; @@ -55,25 +71,27 @@ pub async fn create(addr: &str, https: bool) { ssl.check_private_key().expect("Failed to check private key"); let tls_acceptor = Arc::new(ssl.build()); - let mut tcp_listener = TcpListener::bind(&addr).await.unwrap(); - let mut incoming = tcp_listener.incoming(); + let tcp_listener = TcpListener::bind(&addr).await.unwrap(); - while let (Some(tcp), rest) = incoming.into_future().await { - if let Ok(tcp) = tcp { - let addr = tcp.peer_addr().expect("Unable to get remote address"); - let service = service.call(addr); - let tls_acceptor = Arc::clone(&tls_acceptor); + while let Ok((tcp, _)) = tcp_listener.accept().await { + let addr = tcp.peer_addr().expect("Unable to get remote address"); + let service = service.call(addr); + let tls_acceptor = Arc::clone(&tls_acceptor); - tokio::spawn(async move { - let tls = tokio_openssl::accept(&*tls_acceptor, tcp).await.map_err(|_| ())?; + tokio::spawn(async move { + let ssl = Ssl::new(tls_acceptor.context()).expect("Failed to create async Ssl instance"); + let mut stream = + tokio_openssl::SslStream::new(ssl, tcp).expect("Failed to create async SslStream"); - let service = service.await.map_err(|_| ())?; + Pin::new(&mut stream).accept().await.map_err(|_| ())?; - Http::new().serve_connection(tls, service).await.map_err(|_| ()) - }); - } + let service = service.await.map_err(|_| ())?; - incoming = rest; + Http::new() + .serve_connection(stream, service) + .await + .map_err(|_| ()) + }); } } } else { diff --git a/modules/openapi-generator/src/main/resources/rust-server/example-server-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/example-server-operation.mustache index fbdc6f844796..43b66ae59e37 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/example-server-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/example-server-operation.mustache @@ -13,7 +13,6 @@ {{/allParams}} context: &C) -> Result<{{{operationId}}}Response, ApiError> { - let context = context.clone(); info!("{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}({{#allParams}}{{#vendorExtensions}}{{{x-format-string}}}{{/vendorExtensions}}{{^-last}}, {{/-last}}{{/allParams}}) - X-Span-ID: {:?}"{{#allParams}}, {{{paramName}}}{{/allParams}}, context.get().0.clone()); - Err("Generic failuare".into()) + Err(ApiError("Generic failure".into())) } diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-mod.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-mod.mustache index 609db38999e1..d38f3b5b3dae 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-mod.mustache @@ -26,7 +26,7 @@ pub mod callbacks; /// Request parser for `Api`. pub struct ApiRequestParser; impl RequestParser for ApiRequestParser { - fn parse_operation_id(request: &Request) -> Result<&'static str, ()> { + fn parse_operation_id(request: &Request) -> Option<&'static str> { let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path()); match request.method() { {{#apiInfo}} @@ -34,12 +34,12 @@ impl RequestParser for ApiRequestParser { {{#operations}} {{#operation}} // {{{operationId}}} - {{{httpMethod}}} {{{path}}} - &hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Ok("{{{operationId}}}"), + &hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Some("{{{operationId}}}"), {{/operation}} {{/operations}} {{/apis}} {{/apiInfo}} - _ => Err(()), + _ => None, } } } diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache index cbb812e4f26d..0560d72e7dbe 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache @@ -209,7 +209,7 @@ // Body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.to_raw().await; + let result = body.into_raw().await; match result { Ok(body) => { {{#vendorExtensions}} @@ -279,7 +279,7 @@ // Form Body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.to_raw(); + let result = body.into_raw(); match result.await { Ok(body) => { use std::io::Read; @@ -368,7 +368,7 @@ // Body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.to_raw(); + let result = body.into_raw(); match result.await { Ok(body) => { let mut unused_elements: Vec = vec![]; diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache index fb23ec788815..07ea3e2b3204 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache @@ -137,7 +137,7 @@ export class Configuration { return this.configuration.basePath != null ? this.configuration.basePath : BASE_PATH; } - get fetchApi(): FetchAPI { + get fetchApi(): FetchAPI | undefined{ return this.configuration.fetchApi; }