Skip to content

Commit 33a13a4

Browse files
committed
feat: Switch default features so a user must select SSL backend
1 parent dd58147 commit 33a13a4

32 files changed

Lines changed: 608 additions & 226 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ homepage = "{{.}}"
3232
{{/homePageUrl}}
3333

3434
[features]
35-
default = ["client", "server", "client-tls", "client-openssl"]
35+
default = ["client", "server"]
3636
client = [
3737
{{#apiUsesMultipartFormData}}
3838
"multipart", "multipart/client", "swagger/multipart_form",
@@ -69,7 +69,6 @@ server = [
6969
"mime_multipart", "swagger/multipart_related",
7070
{{/apiUsesMultipartRelated}}
7171
{{#hasCallbacks}}
72-
"client-tls",
7372
{{/hasCallbacks}}
7473
{{! Anything added to the list below, should probably be added to the callbacks list above }}
7574
"serde_ignored", "hyper", "percent-encoding", "url",

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ The generated library has a few optional features that can be activated through
127127
* This defaults to enabled and creates the basic skeleton of a client implementation based on hyper
128128
* The constructed client implements the API trait by making remote API call.
129129
* `client-tls`
130-
* This defaults to enabled and provides HTTPS support using native-tls (macOS/Windows/iOS).
131-
* Enable this feature for TLS support on Apple and Windows platforms.
130+
* Optional feature that provides HTTPS support using native-tls + hyper-tls.
131+
* **Use this for macOS, Windows, and iOS platforms.**
132+
* Not enabled by default to avoid requiring platform-specific dependencies.
132133
* `client-openssl`
133-
* This defaults to enabled and provides HTTPS support using OpenSSL (Linux and other platforms).
134-
* Enable this feature for TLS support on Linux and other Unix-like platforms.
134+
* Optional feature that provides HTTPS support using OpenSSL + hyper-openssl.
135+
* **Use this for Linux and other Unix-like platforms.**
136+
* Not enabled by default to avoid requiring OpenSSL build dependencies.
135137
* `conversions`
136138
* This defaults to disabled and creates extra derives on models to allow "transmogrification" between objects of structurally similar types.
137139
* `cli`
@@ -140,20 +142,30 @@ The generated library has a few optional features that can be activated through
140142
* This defaults to disabled and allows JSON Schema validation of received data using `MakeService::set_validation` or `Service::set_validation`.
141143
* Note, enabling validation will have a performance penalty, especially if the API heavily uses regex based checks.
142144

143-
### Minimal dependencies (no TLS)
145+
### Enabling HTTPS/TLS Support
144146

145-
If you only need HTTP support and want to minimize dependencies (e.g., to avoid OpenSSL build requirements), you can disable the default TLS features:
147+
By default, only HTTP support is included. To enable HTTPS, add the appropriate TLS feature for your platform:
146148

149+
**For macOS/Windows/iOS:**
147150
```toml
148151
[dependencies]
149-
{{{packageName}}} = { version = "{{{packageVersion}}}", default-features = false, features = ["server"] }
152+
{{{packageName}}} = { version = "{{{packageVersion}}}", features = ["client-tls"] }
150153
```
151154

152-
Or for client-only without TLS:
155+
**For Linux/Unix:**
156+
```toml
157+
[dependencies]
158+
{{{packageName}}} = { version = "{{{packageVersion}}}", features = ["client-openssl"] }
159+
```
153160

161+
**For server with callbacks that need HTTPS:**
154162
```toml
155163
[dependencies]
156-
{{{packageName}}} = { version = "{{{packageVersion}}}", default-features = false, features = ["client"] }
164+
# On Linux/Unix
165+
{{{packageName}}} = { version = "{{{packageVersion}}}", features = ["server", "client-openssl"] }
166+
167+
# On macOS/Windows/iOS
168+
{{{packageName}}} = { version = "{{{packageVersion}}}", features = ["server", "client-tls"] }
157169
```
158170

159171
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section for how to use features in your `Cargo.toml`.

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ struct Cli {
4545
server_address: String,
4646
4747
/// Path to the client private key if using client-side TLS authentication
48-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
48+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
4949
#[clap(long, requires_all(&["client_certificate", "server_certificate"]))]
5050
client_key: Option<String>,
5151
5252
/// Path to the client's public certificate associated with the private key
53-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
53+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
5454
#[clap(long, requires_all(&["client_key", "server_certificate"]))]
5555
client_certificate: Option<String>,
5656
5757
/// Path to CA certificate used to authenticate the server
58-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
58+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
5959
#[clap(long)]
6060
server_certificate: Option<String>,
6161
@@ -130,7 +130,8 @@ enum Operation {
130130
{{/apiInfo}}
131131
}
132132

133-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
133+
// OpenSSL-based TLS (Linux/Unix platforms)
134+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
134135
fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
135136
if args.client_certificate.is_some() {
136137
debug!("Using mutual TLS");
@@ -156,10 +157,22 @@ fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoCont
156157
}
157158
}
158159

159-
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
160-
fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
160+
// native-tls-based TLS (macOS/Windows/iOS)
161+
#[cfg(all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")))]
162+
fn create_client(_args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
163+
let client =
164+
Client::try_new(&_args.server_address).context("Failed to create HTTP(S) client")?;
165+
Ok(Box::new(client.with_context(context)))
166+
}
167+
168+
// No TLS support enabled
169+
#[cfg(not(any(
170+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
171+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
172+
)))]
173+
fn create_client(_args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
161174
let client =
162-
Client::try_new(&args.server_address).context("Failed to create HTTP(S) client")?;
175+
Client::try_new(&_args.server_address).context("Failed to create HTTP(S) client")?;
163176
Ok(Box::new(client.with_context(context)))
164177
}
165178

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,40 @@ fn main() {
115115
let context: ClientContext =
116116
swagger::make_context!(ContextBuilder, EmptyContext, auth_data, XSpanIdString::default());
117117

118-
let mut client : Box<dyn ApiNoContext<ClientContext>> = if is_https {
119-
// Using Simple HTTPS
120-
let client = Box::new(Client::try_new_https(&base_url)
121-
.expect("Failed to create HTTPS client"));
122-
Box::new(client.with_context(context))
123-
} else {
124-
// Using HTTP
125-
let client = Box::new(Client::try_new_http(
126-
&base_url)
127-
.expect("Failed to create HTTP client"));
128-
Box::new(client.with_context(context))
118+
let mut client : Box<dyn ApiNoContext<ClientContext>> = {
119+
#[cfg(any(
120+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
121+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
122+
))]
123+
{
124+
if is_https {
125+
// Using HTTPS with platform-appropriate TLS backend
126+
let client = Box::new(Client::try_new_https(&base_url)
127+
.expect("Failed to create HTTPS client"));
128+
Box::new(client.with_context(context))
129+
} else {
130+
// Using HTTP
131+
let client = Box::new(Client::try_new_http(&base_url)
132+
.expect("Failed to create HTTP client"));
133+
Box::new(client.with_context(context))
134+
}
135+
}
136+
137+
#[cfg(not(any(
138+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
139+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
140+
)))]
141+
{
142+
if is_https {
143+
panic!("HTTPS requested but TLS support not enabled. \
144+
Enable the appropriate TLS feature for your platform: \
145+
'client-tls' for macOS/Windows/iOS or 'client-openssl' for Linux/Unix");
146+
}
147+
// Using HTTP only
148+
let client = Box::new(Client::try_new_http(&base_url)
149+
.expect("Failed to create HTTP client"));
150+
Box::new(client.with_context(context))
151+
}
129152
};
130153

131154
let mut rt = tokio::runtime::Runtime::new().unwrap();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "Unlicense"
88
edition = "2018"
99

1010
[features]
11-
default = ["client", "server", "client-tls", "client-openssl"]
11+
default = ["client", "server"]
1212
client = [
1313
"multipart", "multipart/client", "swagger/multipart_form",
1414
"mime_multipart", "swagger/multipart_related",

samples/server/petstore/rust-server/output/multipart-v3/README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ The generated library has a few optional features that can be activated through
112112
* This defaults to enabled and creates the basic skeleton of a client implementation based on hyper
113113
* The constructed client implements the API trait by making remote API call.
114114
* `client-tls`
115-
* This defaults to enabled and provides HTTPS support using native-tls (macOS/Windows/iOS).
116-
* Enable this feature for TLS support on Apple and Windows platforms.
115+
* Optional feature that provides HTTPS support using native-tls + hyper-tls.
116+
* **Use this for macOS, Windows, and iOS platforms.**
117+
* Not enabled by default to avoid requiring platform-specific dependencies.
117118
* `client-openssl`
118-
* This defaults to enabled and provides HTTPS support using OpenSSL (Linux and other platforms).
119-
* Enable this feature for TLS support on Linux and other Unix-like platforms.
119+
* Optional feature that provides HTTPS support using OpenSSL + hyper-openssl.
120+
* **Use this for Linux and other Unix-like platforms.**
121+
* Not enabled by default to avoid requiring OpenSSL build dependencies.
120122
* `conversions`
121123
* This defaults to disabled and creates extra derives on models to allow "transmogrification" between objects of structurally similar types.
122124
* `cli`
@@ -125,20 +127,30 @@ The generated library has a few optional features that can be activated through
125127
* This defaults to disabled and allows JSON Schema validation of received data using `MakeService::set_validation` or `Service::set_validation`.
126128
* Note, enabling validation will have a performance penalty, especially if the API heavily uses regex based checks.
127129

128-
### Minimal dependencies (no TLS)
130+
### Enabling HTTPS/TLS Support
129131

130-
If you only need HTTP support and want to minimize dependencies (e.g., to avoid OpenSSL build requirements), you can disable the default TLS features:
132+
By default, only HTTP support is included. To enable HTTPS, add the appropriate TLS feature for your platform:
131133

134+
**For macOS/Windows/iOS:**
132135
```toml
133136
[dependencies]
134-
multipart-v3 = { version = "1.0.7", default-features = false, features = ["server"] }
137+
multipart-v3 = { version = "1.0.7", features = ["client-tls"] }
135138
```
136139

137-
Or for client-only without TLS:
140+
**For Linux/Unix:**
141+
```toml
142+
[dependencies]
143+
multipart-v3 = { version = "1.0.7", features = ["client-openssl"] }
144+
```
138145

146+
**For server with callbacks that need HTTPS:**
139147
```toml
140148
[dependencies]
141-
multipart-v3 = { version = "1.0.7", default-features = false, features = ["client"] }
149+
# On Linux/Unix
150+
multipart-v3 = { version = "1.0.7", features = ["server", "client-openssl"] }
151+
152+
# On macOS/Windows/iOS
153+
multipart-v3 = { version = "1.0.7", features = ["server", "client-tls"] }
142154
```
143155

144156
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section for how to use features in your `Cargo.toml`.

samples/server/petstore/rust-server/output/multipart-v3/bin/cli.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ struct Cli {
3535
server_address: String,
3636

3737
/// Path to the client private key if using client-side TLS authentication
38-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
38+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
3939
#[clap(long, requires_all(&["client_certificate", "server_certificate"]))]
4040
client_key: Option<String>,
4141

4242
/// Path to the client's public certificate associated with the private key
43-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
43+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
4444
#[clap(long, requires_all(&["client_key", "server_certificate"]))]
4545
client_certificate: Option<String>,
4646

4747
/// Path to CA certificate used to authenticate the server
48-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
48+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
4949
#[clap(long)]
5050
server_certificate: Option<String>,
5151

@@ -83,7 +83,8 @@ enum Operation {
8383
},
8484
}
8585

86-
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
86+
// OpenSSL-based TLS (Linux/Unix platforms)
87+
#[cfg(all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios"))))]
8788
fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
8889
if args.client_certificate.is_some() {
8990
debug!("Using mutual TLS");
@@ -109,10 +110,22 @@ fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoCont
109110
}
110111
}
111112

112-
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
113-
fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
113+
// native-tls-based TLS (macOS/Windows/iOS)
114+
#[cfg(all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")))]
115+
fn create_client(_args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
116+
let client =
117+
Client::try_new(&_args.server_address).context("Failed to create HTTP(S) client")?;
118+
Ok(Box::new(client.with_context(context)))
119+
}
120+
121+
// No TLS support enabled
122+
#[cfg(not(any(
123+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
124+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
125+
)))]
126+
fn create_client(_args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoContext<ClientContext>>> {
114127
let client =
115-
Client::try_new(&args.server_address).context("Failed to create HTTP(S) client")?;
128+
Client::try_new(&_args.server_address).context("Failed to create HTTP(S) client")?;
116129
Ok(Box::new(client.with_context(context)))
117130
}
118131

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,40 @@ fn main() {
8989
let context: ClientContext =
9090
swagger::make_context!(ContextBuilder, EmptyContext, auth_data, XSpanIdString::default());
9191

92-
let mut client : Box<dyn ApiNoContext<ClientContext>> = if is_https {
93-
// Using Simple HTTPS
94-
let client = Box::new(Client::try_new_https(&base_url)
95-
.expect("Failed to create HTTPS client"));
96-
Box::new(client.with_context(context))
97-
} else {
98-
// Using HTTP
99-
let client = Box::new(Client::try_new_http(
100-
&base_url)
101-
.expect("Failed to create HTTP client"));
102-
Box::new(client.with_context(context))
92+
let mut client : Box<dyn ApiNoContext<ClientContext>> = {
93+
#[cfg(any(
94+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
95+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
96+
))]
97+
{
98+
if is_https {
99+
// Using HTTPS with platform-appropriate TLS backend
100+
let client = Box::new(Client::try_new_https(&base_url)
101+
.expect("Failed to create HTTPS client"));
102+
Box::new(client.with_context(context))
103+
} else {
104+
// Using HTTP
105+
let client = Box::new(Client::try_new_http(&base_url)
106+
.expect("Failed to create HTTP client"));
107+
Box::new(client.with_context(context))
108+
}
109+
}
110+
111+
#[cfg(not(any(
112+
all(feature = "client-tls", any(target_os = "macos", target_os = "windows", target_os = "ios")),
113+
all(feature = "client-openssl", not(any(target_os = "macos", target_os = "windows", target_os = "ios")))
114+
)))]
115+
{
116+
if is_https {
117+
panic!("HTTPS requested but TLS support not enabled. \
118+
Enable the appropriate TLS feature for your platform: \
119+
'client-tls' for macOS/Windows/iOS or 'client-openssl' for Linux/Unix");
120+
}
121+
// Using HTTP only
122+
let client = Box::new(Client::try_new_http(&base_url)
123+
.expect("Failed to create HTTP client"));
124+
Box::new(client.with_context(context))
125+
}
103126
};
104127

105128
let mut rt = tokio::runtime::Runtime::new().unwrap();

samples/server/petstore/rust-server/output/no-example-v3/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "Unlicense"
88
edition = "2018"
99

1010
[features]
11-
default = ["client", "server", "client-tls", "client-openssl"]
11+
default = ["client", "server"]
1212
client = [
1313
"hyper", "percent-encoding", "hyper-util/http1", "hyper-util/http2", "url"
1414
]

0 commit comments

Comments
 (0)