Skip to content

Commit b705972

Browse files
wing328blazzy
andauthored
Upgrade rust-hyper to use hyper 1.0 (OpenAPITools#19115)
* Upgrade to rust-hyper to use hyper 1.0 * Update rust-hyper samples for hyper 1.0 upgrade * update cargo * update samples * update samples * update doc * Default client configuration for rust-hyper --------- Co-authored-by: Krishna Rajendran <[email protected]>
1 parent 897a4e7 commit b705972

File tree

117 files changed

+4014
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+4014
-278
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: rust
2+
outputDir: samples/client/petstore/rust/hyper0x/petstore
3+
library: hyper0x
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/rust
6+
additionalProperties:
7+
supportAsync: "false"
8+
packageName: petstore-hyper0x

docs/generators/rust.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2222
|bestFitInt|Use best fitting integer type where minimum or maximum is set| |false|
2323
|enumNameSuffix|Suffix that will be appended to all enum names.| ||
2424
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
25-
|library|library template (sub-template) to use.|<dl><dt>**hyper**</dt><dd>HTTP client: Hyper.</dd><dt>**reqwest**</dt><dd>HTTP client: Reqwest.</dd></dl>|reqwest|
25+
|library|library template (sub-template) to use.|<dl><dt>**hyper**</dt><dd>HTTP client: Hyper (v1.x).</dd><dt>**hyper0x**</dt><dd>HTTP client: Hyper (v0.x).</dd><dt>**reqwest**</dt><dd>HTTP client: Reqwest.</dd></dl>|reqwest|
2626
|packageName|Rust package name (convention: lowercase).| |openapi|
2727
|packageVersion|Rust package version.| |1.0.0|
2828
|preferUnsignedInt|Prefer unsigned integers where minimum value is &gt;= 0| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
5757
public static final String PACKAGE_NAME = "packageName";
5858
public static final String PACKAGE_VERSION = "packageVersion";
5959
public static final String HYPER_LIBRARY = "hyper";
60+
public static final String HYPER0X_LIBRARY = "hyper0x";
6061
public static final String REQWEST_LIBRARY = "reqwest";
6162
public static final String SUPPORT_ASYNC = "supportAsync";
6263
public static final String SUPPORT_MIDDLEWARE = "supportMiddleware";
@@ -202,7 +203,8 @@ public RustClientCodegen() {
202203
cliOptions.add(new CliOption(AVOID_BOXED_MODELS, "If set, `Box<T>` will not be used for models", SchemaTypeUtil.BOOLEAN_TYPE)
203204
.defaultValue(Boolean.FALSE.toString()));
204205

205-
supportedLibraries.put(HYPER_LIBRARY, "HTTP client: Hyper.");
206+
supportedLibraries.put(HYPER_LIBRARY, "HTTP client: Hyper (v1.x).");
207+
supportedLibraries.put(HYPER0X_LIBRARY, "HTTP client: Hyper (v0.x).");
206208
supportedLibraries.put(REQWEST_LIBRARY, "HTTP client: Reqwest.");
207209

208210
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use.");
@@ -371,6 +373,9 @@ public void processOpts() {
371373

372374
if (HYPER_LIBRARY.equals(getLibrary())) {
373375
additionalProperties.put(HYPER_LIBRARY, "true");
376+
} else if (HYPER0X_LIBRARY.equals(getLibrary())) {
377+
additionalProperties.put(HYPER_LIBRARY, "true");
378+
additionalProperties.put(HYPER0X_LIBRARY, "true");
374379
} else if (REQWEST_LIBRARY.equals(getLibrary())) {
375380
additionalProperties.put(REQWEST_LIBRARY, "true");
376381
} else {

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

+7
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ serde_json = "^1.0"
4040
url = "^2.5"
4141
uuid = { version = "^1.8", features = ["serde", "v4"] }
4242
{{#hyper}}
43+
{{#hyper0x}}
4344
hyper = { version = "~0.14", features = ["full"] }
4445
hyper-tls = "~0.5"
46+
{{/hyper0x}}
47+
{{^hyper0x}}
48+
hyper = { version = "^1.3.1", features = ["full"] }
49+
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
50+
http-body-util = { version = "0.1.2" }
51+
{{/hyper0x}}
4552
http = "~0.2"
4653
base64 = "~0.7.0"
4754
futures = "^0.3"

modules/openapi-generator/src/main/resources/rust/hyper/api.mustache

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ use std::pin::Pin;
66
use std::option::Option;
77

88
use hyper;
9+
use hyper_util::client::legacy::connect::Connect;
910
use futures::Future;
1011

1112
use crate::models;
1213
use super::{Error, configuration};
1314
use super::request as __internal_request;
1415

15-
pub struct {{{classname}}}Client<C: hyper::client::connect::Connect>
16+
pub struct {{{classname}}}Client<C: Connect>
1617
where C: Clone + std::marker::Send + Sync + 'static {
1718
configuration: Rc<configuration::Configuration<C>>,
1819
}
1920

20-
impl<C: hyper::client::connect::Connect> {{{classname}}}Client<C>
21+
impl<C: Connect> {{{classname}}}Client<C>
2122
where C: Clone + std::marker::Send + Sync {
2223
pub fn new(configuration: Rc<configuration::Configuration<C>>) -> {{{classname}}}Client<C> {
2324
{{{classname}}}Client {
@@ -34,7 +35,7 @@ pub trait {{{classname}}} {
3435
{{/operations}}
3536
}
3637

37-
impl<C: hyper::client::connect::Connect>{{{classname}}} for {{{classname}}}Client<C>
38+
impl<C: Connect>{{{classname}}} for {{{classname}}}Client<C>
3839
where C: Clone + std::marker::Send + Sync {
3940
{{#operations}}
4041
{{#operation}}

modules/openapi-generator/src/main/resources/rust/hyper/api_mod.mustache

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
use http;
1+
use std::fmt;
2+
use std::fmt::Debug;
3+
24
use hyper;
5+
use hyper::http;
6+
use hyper_util::client::legacy::connect::Connect;
37
use serde_json;
48

59
#[derive(Debug)]
610
pub enum Error {
711
Api(ApiError),
8-
Header(hyper::http::header::InvalidHeaderValue),
12+
Header(http::header::InvalidHeaderValue),
913
Http(http::Error),
1014
Hyper(hyper::Error),
15+
HyperClient(hyper_util::client::legacy::Error),
1116
Serde(serde_json::Error),
1217
UriError(http::uri::InvalidUri),
1318
}
1419

15-
#[derive(Debug)]
1620
pub struct ApiError {
1721
pub code: hyper::StatusCode,
18-
pub body: hyper::body::Body,
22+
pub body: hyper::body::Incoming,
23+
}
24+
25+
impl Debug for ApiError {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
f.debug_struct("ApiError")
28+
.field("code", &self.code)
29+
.field("body", &"hyper::body::Incoming")
30+
.finish()
31+
}
1932
}
2033
21-
impl From<(hyper::StatusCode, hyper::body::Body)> for Error {
22-
fn from(e: (hyper::StatusCode, hyper::body::Body)) -> Self {
34+
impl From<(hyper::StatusCode, hyper::body::Incoming)> for Error {
35+
fn from(e: (hyper::StatusCode, hyper::body::Incoming)) -> Self {
2336
Error::Api(ApiError {
2437
code: e.0,
2538
body: e.1,
@@ -33,6 +46,12 @@ impl From<http::Error> for Error {
3346
}
3447
}
3548
49+
impl From<hyper_util::client::legacy::Error> for Error {
50+
fn from(e: hyper_util::client::legacy::Error) -> Self {
51+
Error::HyperClient(e)
52+
}
53+
}
54+
3655
impl From<hyper::Error> for Error {
3756
fn from(e: hyper::Error) -> Self {
3857
Error::Hyper(e)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::rc::Rc;
22

33
use hyper;
4+
use hyper_util::client::legacy::connect::Connect;
45
use super::configuration::Configuration;
56

67
pub struct APIClient {
@@ -18,7 +19,7 @@ pub struct APIClient {
1819
}
1920

2021
impl APIClient {
21-
pub fn new<C: hyper::client::connect::Connect>(configuration: Configuration<C>) -> APIClient
22+
pub fn new<C: Connect>(configuration: Configuration<C>) -> APIClient
2223
where C: Clone + std::marker::Send + Sync + 'static {
2324
let rc = Rc::new(configuration);
2425

modules/openapi-generator/src/main/resources/rust/hyper/configuration.mustache

+47-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{{>partial_header}}
22
use hyper;
3+
use hyper_util::client::legacy::Client;
4+
use hyper_util::client::legacy::connect::Connect;
5+
use hyper_util::client::legacy::connect::HttpConnector;
6+
use hyper_util::rt::TokioExecutor;
37

4-
pub struct Configuration<C: hyper::client::connect::Connect>
8+
pub struct Configuration<C: Connect = HttpConnector>
59
where C: Clone + std::marker::Send + Sync + 'static {
610
pub base_path: String,
711
pub user_agent: Option<String>,
8-
pub client: hyper::client::Client<C>,
12+
pub client: Client<C, String>,
913
pub basic_auth: Option<BasicAuth>,
1014
pub oauth_access_token: Option<String>,
1115
pub api_key: Option<ApiKey>,
@@ -19,9 +23,41 @@ pub struct ApiKey {
1923
pub key: String,
2024
}
2125

22-
impl<C: hyper::client::connect::Connect> Configuration<C>
26+
impl Configuration<HttpConnector> {
27+
/// Construct a default [`Configuration`](Self) with a hyper client using a default
28+
/// [`HttpConnector`](hyper_util::client::legacy::connect::HttpConnector).
29+
///
30+
/// Use [`with_client`](Configuration<T>::with_client) to construct a Configuration with a
31+
/// custom hyper client.
32+
///
33+
/// # Example
34+
///
35+
/// ```
36+
/// let api_config = {
37+
/// api_key: "my-api-key",
38+
/// ...Configuration::new()
39+
/// }
40+
/// ```
41+
pub fn new() -> Configuration<HttpConnector> {
42+
Configuration::default()
43+
}
44+
}
45+
46+
impl<C: Connect> Configuration<C>
2347
where C: Clone + std::marker::Send + Sync {
24-
pub fn new(client: hyper::client::Client<C>) -> Configuration<C> {
48+
49+
/// Construct a new Configuration with a custom hyper client.
50+
///
51+
/// # Example
52+
///
53+
/// ```
54+
/// let client = Client::builder(TokioExecutor::new())
55+
/// .pool_idle_timeout(Duration::from_secs(30))
56+
/// .build_http();
57+
///
58+
/// let api_config = Configuration::with_client(client);
59+
/// ```
60+
pub fn with_client(client: Client<C, String>) -> Configuration<C> {
2561
Configuration {
2662
base_path: "{{{basePath}}}".to_owned(),
2763
user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}Some("OpenAPI-Generator/{{{version}}}/rust".to_owned()){{/httpUserAgent}},
@@ -32,3 +68,10 @@ impl<C: hyper::client::connect::Connect> Configuration<C>
3268
}
3369
}
3470
}
71+
72+
impl Default for Configuration<HttpConnector> {
73+
fn default() -> Self {
74+
let client = Client::builder(TokioExecutor::new()).build_http();
75+
Configuration::with_client(client)
76+
}
77+
}

0 commit comments

Comments
 (0)