Skip to content

Commit 4e602b6

Browse files
sransaraFoorack
andauthored
[Rust][Server] Upgrade to tokio v1 (#11873)
* [Rust][Server] Upgrade hyper from 0.13 to 0.14, swagger-rs from 5.0.2 to 6.1.0 * Also upgrade tokio from 0.2 to 1.14 * Re-add JavaClientCodegenTest.java * Fix incorrect slashes due to wrong generation * Spelling fix: failuare -> failure * Upgrade more packages * Commit generated code * Fix typo Co-authored-by: Foorack / Max Faxälv <max@foorack.com>
1 parent 941d484 commit 4e602b6

39 files changed

Lines changed: 677 additions & 656 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-
6161

6262
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))'.dependencies]
6363
native-tls = { version = "0.2", optional = true }
64-
hyper-tls = { version = "0.4", optional = true }
64+
hyper-tls = { version = "0.5", optional = true }
6565

6666
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dependencies]
67-
hyper-openssl = { version = "0.8", optional = true }
67+
hyper-openssl = { version = "0.9", optional = true }
6868
openssl = {version = "0.10", optional = true }
6969

7070
[dependencies]
7171
# Common
7272
async-trait = "0.1.24"
7373
chrono = { version = "0.4", features = ["serde"] }
7474
futures = "0.3"
75-
swagger = "5.0.2"
75+
swagger = { version = "6.1", features = ["serdejson", "server", "client", "tls", "tcp"] }
7676
log = "0.4.0"
7777
mime = "0.3"
7878

@@ -96,7 +96,7 @@ uuid = {version = "0.8", features = ["serde", "v4"]}
9696
{{/apiUsesUuid}}
9797

9898
# Common between server and client features
99-
hyper = {version = "0.13", optional = true}
99+
hyper = {version = "0.14", features = ["full"], optional = true}
100100
{{#apiUsesMultipartRelated}}
101101
mime_multipart = {version = "0.5", optional = true}
102102
hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true}
@@ -124,12 +124,11 @@ frunk-enum-core = { version = "0.2.0", optional = true }
124124
[dev-dependencies]
125125
clap = "2.25"
126126
env_logger = "0.7"
127-
tokio = { version = "0.2", features = ["rt-threaded", "macros", "stream"] }
127+
tokio = { version = "1.14", features = ["full"] }
128128
native-tls = "0.2"
129-
tokio-tls = "0.3"
130129

131130
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dev-dependencies]
132-
tokio-openssl = "0.4"
131+
tokio-openssl = "0.6"
133132
openssl = "0.10"
134133

135134
[[example]]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::{{{operationId}}}Response;
4747
/// Request parser for `Api`.
4848
pub struct ApiRequestParser;
4949
impl<T> RequestParser<T> for ApiRequestParser {
50-
fn parse_operation_id(request: &Request<T>) -> Result<&'static str, ()> {
50+
fn parse_operation_id(request: &Request<T>) -> Option<&'static str> {
5151
let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path());
5252
match request.method() {
5353
{{#apiInfo}}
@@ -58,15 +58,15 @@ impl<T> RequestParser<T> for ApiRequestParser {
5858
{{#urls}}
5959
{{#requests}}
6060
// {{{operationId}}} - {{{httpMethod}}} {{{path}}}
61-
&hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Ok("{{{operationId}}}"),
61+
&hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Some("{{{operationId}}}"),
6262
{{/requests}}
6363
{{/urls}}
6464
{{/callbacks}}
6565
{{/operation}}
6666
{{/operations}}
6767
{{/apis}}
6868
{{/apiInfo}}
69-
_ => Err(()),
69+
_ => None,
7070
}
7171
}
7272
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@
418418
let body = response.into_body();
419419
{{#dataType}}
420420
let body = body
421-
.to_raw()
421+
.into_raw()
422422
.map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?;
423423
{{#vendorExtensions}}
424424
{{#x-produces-bytes}}
@@ -434,7 +434,9 @@
434434
.map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?;
435435
{{/x-produces-xml}}
436436
{{#x-produces-json}}
437-
let body = serde_json::from_str::<{{{dataType}}}>(body)?;
437+
let body = serde_json::from_str::<{{{dataType}}}>(body).map_err(|e| {
438+
ApiError(format!("Response body did not match the schema: {}", e))
439+
})?;
438440
{{/x-produces-json}}
439441
{{#x-produces-plain-text}}
440442
let body = body.to_string();
@@ -477,7 +479,7 @@
477479
let headers = response.headers().clone();
478480
let body = response.into_body()
479481
.take(100)
480-
.to_raw().await;
482+
.into_raw().await;
481483
Err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
482484
code,
483485
headers,

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use futures::{future, Stream, StreamExt, TryFutureExt, TryStreamExt};
77
use hyper::server::conn::Http;
88
use hyper::service::Service;
99
use log::info;
10-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
11-
use openssl::ssl::SslAcceptorBuilder;
1210
use std::future::Future;
1311
use std::marker::PhantomData;
1412
use std::net::SocketAddr;
@@ -20,7 +18,7 @@ use swagger::EmptyContext;
2018
use tokio::net::TcpListener;
2119

2220
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
23-
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
21+
use openssl::ssl::{Ssl, SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod};
2422

2523
use {{{externCrateName}}}::models;
2624

@@ -54,26 +52,25 @@ pub async fn create(addr: &str, https: bool) {
5452
ssl.set_certificate_chain_file("examples/server-chain.pem").expect("Failed to set certificate chain");
5553
ssl.check_private_key().expect("Failed to check private key");
5654
57-
let tls_acceptor = Arc::new(ssl.build());
58-
let mut tcp_listener = TcpListener::bind(&addr).await.unwrap();
59-
let mut incoming = tcp_listener.incoming();
55+
let tls_acceptor = ssl.build();
56+
let tcp_listener = TcpListener::bind(&addr).await.unwrap();
6057
61-
while let (Some(tcp), rest) = incoming.into_future().await {
62-
if let Ok(tcp) = tcp {
58+
loop {
59+
if let Ok((tcp, _)) = tcp_listener.accept().await {
60+
let ssl = Ssl::new(tls_acceptor.context()).unwrap();
6361
let addr = tcp.peer_addr().expect("Unable to get remote address");
6462
let service = service.call(addr);
65-
let tls_acceptor = Arc::clone(&tls_acceptor);
6663
6764
tokio::spawn(async move {
68-
let tls = tokio_openssl::accept(&*tls_acceptor, tcp).await.map_err(|_| ())?;
69-
65+
let tls = tokio_openssl::SslStream::new(ssl, tcp).map_err(|_| ())?;
7066
let service = service.await.map_err(|_| ())?;
7167
72-
Http::new().serve_connection(tls, service).await.map_err(|_| ())
68+
Http::new()
69+
.serve_connection(tls, service)
70+
.await
71+
.map_err(|_| ())
7372
});
7473
}
75-
76-
incoming = rest;
7774
}
7875
}
7976
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
{
1616
let context = context.clone();
1717
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());
18-
Err("Generic failure".into())
18+
Err(ApiError("Generic failure".into()))
1919
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<C> Client<DropContextService<hyper::client::Client<hyper::client::HttpConne
100100
}
101101

102102
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
103-
type HttpConnector = hyper_tls::HttpsConnector<hyper::client::HttpConnector>;
103+
type HttpsConnector = hyper_tls::HttpsConnector<hyper::client::HttpConnector>;
104104

105105
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
106106
type HttpsConnector = hyper_openssl::HttpsConnector<hyper::client::HttpConnector>;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ pub mod callbacks;
2626
/// Request parser for `Api`.
2727
pub struct ApiRequestParser;
2828
impl<T> RequestParser<T> for ApiRequestParser {
29-
fn parse_operation_id(request: &Request<T>) -> Result<&'static str, ()> {
29+
fn parse_operation_id(request: &Request<T>) -> Option<&'static str> {
3030
let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path());
3131
match request.method() {
3232
{{#apiInfo}}
3333
{{#apis}}
3434
{{#operations}}
3535
{{#operation}}
3636
// {{{operationId}}} - {{{httpMethod}}} {{{path}}}
37-
&hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Ok("{{{operationId}}}"),
37+
&hyper::Method::{{{vendorExtensions.x-http-method}}} if path.matched(paths::ID_{{{vendorExtensions.x-path-id}}}) => Some("{{{operationId}}}"),
3838
{{/operation}}
3939
{{/operations}}
4040
{{/apis}}
4141
{{/apiInfo}}
42-
_ => Err(()),
42+
_ => None,
4343
}
4444
}
4545
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
// Body parameters (note that non-required body parameters will ignore garbage
210210
// values, rather than causing a 400 response). Produce warning header and logs for
211211
// any unused fields.
212-
let result = body.to_raw().await;
212+
let result = body.into_raw().await;
213213
match result {
214214
Ok(body) => {
215215
{{#vendorExtensions}}
@@ -279,7 +279,7 @@
279279
// Form Body parameters (note that non-required body parameters will ignore garbage
280280
// values, rather than causing a 400 response). Produce warning header and logs for
281281
// any unused fields.
282-
let result = body.to_raw();
282+
let result = body.into_raw();
283283
match result.await {
284284
Ok(body) => {
285285
use std::io::Read;
@@ -368,7 +368,7 @@
368368
// Body parameters (note that non-required body parameters will ignore garbage
369369
// values, rather than causing a 400 response). Produce warning header and logs for
370370
// any unused fields.
371-
let result = body.to_raw();
371+
let result = body.into_raw();
372372
match result.await {
373373
Ok(body) => {
374374
let mut unused_elements: Vec<String> = vec![];

samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-
2424

2525
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))'.dependencies]
2626
native-tls = { version = "0.2", optional = true }
27-
hyper-tls = { version = "0.4", optional = true }
27+
hyper-tls = { version = "0.5", optional = true }
2828

2929
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dependencies]
30-
hyper-openssl = { version = "0.8", optional = true }
30+
hyper-openssl = { version = "0.9", optional = true }
3131
openssl = {version = "0.10", optional = true }
3232

3333
[dependencies]
3434
# Common
3535
async-trait = "0.1.24"
3636
chrono = { version = "0.4", features = ["serde"] }
3737
futures = "0.3"
38-
swagger = "5.0.2"
38+
swagger = { version = "6.1", features = ["serdejson", "server", "client", "tls", "tcp"] }
3939
log = "0.4.0"
4040
mime = "0.3"
4141

@@ -47,7 +47,7 @@ mime_0_2 = { package = "mime", version = "0.2.6", optional = true }
4747
multipart = { version = "0.16", default-features = false, optional = true }
4848

4949
# Common between server and client features
50-
hyper = {version = "0.13", optional = true}
50+
hyper = {version = "0.14", features = ["full"], optional = true}
5151
mime_multipart = {version = "0.5", optional = true}
5252
hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true}
5353
serde_ignored = {version = "0.1.1", optional = true}
@@ -70,12 +70,11 @@ frunk-enum-core = { version = "0.2.0", optional = true }
7070
[dev-dependencies]
7171
clap = "2.25"
7272
env_logger = "0.7"
73-
tokio = { version = "0.2", features = ["rt-threaded", "macros", "stream"] }
73+
tokio = { version = "1.14", features = ["full"] }
7474
native-tls = "0.2"
75-
tokio-tls = "0.3"
7675

7776
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dev-dependencies]
78-
tokio-openssl = "0.4"
77+
tokio-openssl = "0.6"
7978
openssl = "0.10"
8079

8180
[[example]]

samples/server/petstore/rust-server/output/multipart-v3/examples/server/server.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use futures::{future, Stream, StreamExt, TryFutureExt, TryStreamExt};
77
use hyper::server::conn::Http;
88
use hyper::service::Service;
99
use log::info;
10-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
11-
use openssl::ssl::SslAcceptorBuilder;
1210
use std::future::Future;
1311
use std::marker::PhantomData;
1412
use std::net::SocketAddr;
@@ -20,7 +18,7 @@ use swagger::EmptyContext;
2018
use tokio::net::TcpListener;
2119

2220
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
23-
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
21+
use openssl::ssl::{Ssl, SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod};
2422

2523
use multipart_v3::models;
2624

@@ -54,26 +52,25 @@ pub async fn create(addr: &str, https: bool) {
5452
ssl.set_certificate_chain_file("examples/server-chain.pem").expect("Failed to set certificate chain");
5553
ssl.check_private_key().expect("Failed to check private key");
5654

57-
let tls_acceptor = Arc::new(ssl.build());
58-
let mut tcp_listener = TcpListener::bind(&addr).await.unwrap();
59-
let mut incoming = tcp_listener.incoming();
55+
let tls_acceptor = ssl.build();
56+
let tcp_listener = TcpListener::bind(&addr).await.unwrap();
6057

61-
while let (Some(tcp), rest) = incoming.into_future().await {
62-
if let Ok(tcp) = tcp {
58+
loop {
59+
if let Ok((tcp, _)) = tcp_listener.accept().await {
60+
let ssl = Ssl::new(tls_acceptor.context()).unwrap();
6361
let addr = tcp.peer_addr().expect("Unable to get remote address");
6462
let service = service.call(addr);
65-
let tls_acceptor = Arc::clone(&tls_acceptor);
6663

6764
tokio::spawn(async move {
68-
let tls = tokio_openssl::accept(&*tls_acceptor, tcp).await.map_err(|_| ())?;
69-
65+
let tls = tokio_openssl::SslStream::new(ssl, tcp).map_err(|_| ())?;
7066
let service = service.await.map_err(|_| ())?;
7167

72-
Http::new().serve_connection(tls, service).await.map_err(|_| ())
68+
Http::new()
69+
.serve_connection(tls, service)
70+
.await
71+
.map_err(|_| ())
7372
});
7473
}
75-
76-
incoming = rest;
7774
}
7875
}
7976
} else {
@@ -116,7 +113,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString> + Send + Sync
116113
{
117114
let context = context.clone();
118115
info!("multipart_related_request_post({:?}, {:?}, {:?}) - X-Span-ID: {:?}", required_binary_field, object_field, optional_binary_field, context.get().0.clone());
119-
Err("Generic failure".into())
116+
Err(ApiError("Generic failure".into()))
120117
}
121118

122119
async fn multipart_request_post(
@@ -129,7 +126,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString> + Send + Sync
129126
{
130127
let context = context.clone();
131128
info!("multipart_request_post(\"{}\", {:?}, {:?}, {:?}) - X-Span-ID: {:?}", string_field, binary_field, optional_string_field, object_field, context.get().0.clone());
132-
Err("Generic failure".into())
129+
Err(ApiError("Generic failure".into()))
133130
}
134131

135132
async fn multiple_identical_mime_types_post(
@@ -140,7 +137,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString> + Send + Sync
140137
{
141138
let context = context.clone();
142139
info!("multiple_identical_mime_types_post({:?}, {:?}) - X-Span-ID: {:?}", binary1, binary2, context.get().0.clone());
143-
Err("Generic failure".into())
140+
Err(ApiError("Generic failure".into()))
144141
}
145142

146143
}

0 commit comments

Comments
 (0)