Skip to content

Commit 7ab9364

Browse files
authored
[Rust] Configurable default features from reqwest dependency (#22041)
* fix(rust): Remove default features from reqwest dependency This change removes the default features (specifically native-tls) from the reqwest dependency in the Rust client generator. This allows users to explicitly choose their TLS backend without automatically including openssl-sys as a transitive dependency. Users can now explicitly enable TLS backends using feature flags: - native-tls - rustls-tls - default-tls Fixes #21933 * feat(rust): Add reqwestDefaultFeatures option with backward-compatible default This change adds a new generator option `reqwestDefaultFeatures` that allows users to configure the default Cargo features for the reqwest dependency. The option accepts: - An array of strings in YAML config: reqwestDefaultFeatures: ["native-tls"] - A comma-separated string via CLI: --additional-properties=reqwestDefaultFeatures=native-tls - An empty value for no defaults: reqwestDefaultFeatures: [] Default value: ["native-tls"] (maintains backward compatibility) This addresses the feedback in #21933 to make the change opt-in rather than breaking existing users. Users can now: - Keep the current behavior (default) - Opt-out: reqwestDefaultFeatures: [] - Use alternative TLS: reqwestDefaultFeatures: ["rustls-tls"] - Combine features: reqwestDefaultFeatures: ["native-tls", "cookies"] Fixes #21933
1 parent 28e7e7f commit 7ab9364

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

docs/generators/rust.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2727
|packageName|Rust package name (convention: lowercase).| |openapi|
2828
|packageVersion|Rust package version.| |1.0.0|
2929
|preferUnsignedInt|Prefer unsigned integers where minimum value is >= 0| |false|
30+
|reqwestDefaultFeatures|Default features for the reqwest dependency (comma-separated). Use empty for no defaults. This option is for 'reqwest' and 'reqwest-trait' library only.| |native-tls|
3031
|supportAsync|If set, generate async function call instead. This option is for 'reqwest' library only| |true|
3132
|supportMiddleware|If set, add support for reqwest-middleware. This option is for 'reqwest' and 'reqwest-trait' library only| |false|
3233
|supportMultipleResponses|If set, return type wraps an enum of all possible 2xx schemas. This option is for 'reqwest' and 'reqwest-trait' library only| |false|

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
5858
@Setter private boolean preferUnsignedInt = false;
5959
@Setter private boolean bestFitInt = false;
6060
@Setter private boolean avoidBoxedModels = false;
61+
private List<String> reqwestDefaultFeatures = Arrays.asList("native-tls");
6162

6263
public static final String PACKAGE_NAME = "packageName";
6364
public static final String EXTERN_CRATE_NAME = "externCrateName";
@@ -77,6 +78,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
7778
public static final String TOP_LEVEL_API_CLIENT = "topLevelApiClient";
7879
public static final String MOCKALL = "mockall";
7980
public static final String BON_BUILDER = "useBonBuilder";
81+
public static final String REQWEST_DEFAULT_FEATURES = "reqwestDefaultFeatures";
8082

8183
@Setter protected String packageName = "openapi";
8284
@Setter protected String packageVersion = "1.0.0";
@@ -227,6 +229,8 @@ public RustClientCodegen() {
227229
.defaultValue(Boolean.FALSE.toString()));
228230
cliOptions.add(new CliOption(BON_BUILDER, "Use the bon crate for building parameter types. This option is for the 'reqwest-trait' library only", SchemaTypeUtil.BOOLEAN_TYPE)
229231
.defaultValue(Boolean.FALSE.toString()));
232+
cliOptions.add(new CliOption(REQWEST_DEFAULT_FEATURES, "Default features for the reqwest dependency (comma-separated). Use empty for no defaults. This option is for 'reqwest' and 'reqwest-trait' library only.")
233+
.defaultValue("native-tls"));
230234

231235
supportedLibraries.put(HYPER_LIBRARY, "HTTP client: Hyper (v1.x).");
232236
supportedLibraries.put(HYPER0X_LIBRARY, "HTTP client: Hyper (v0.x).");
@@ -431,6 +435,21 @@ public void processOpts() {
431435
}
432436
writePropertyBack(AVOID_BOXED_MODELS, getAvoidBoxedModels());
433437

438+
if (additionalProperties.containsKey(REQWEST_DEFAULT_FEATURES)) {
439+
Object value = additionalProperties.get(REQWEST_DEFAULT_FEATURES);
440+
if (value instanceof List) {
441+
reqwestDefaultFeatures = (List<String>) value;
442+
} else if (value instanceof String) {
443+
String str = (String) value;
444+
if (str.isEmpty()) {
445+
reqwestDefaultFeatures = new ArrayList<>();
446+
} else {
447+
reqwestDefaultFeatures = Arrays.asList(str.split(",\\s*"));
448+
}
449+
}
450+
}
451+
additionalProperties.put(REQWEST_DEFAULT_FEATURES, reqwestDefaultFeatures);
452+
434453
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
435454
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
436455
additionalProperties.put(EXTERN_CRATE_NAME, getExternCrateName());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ google-cloud-token = "^0.1"
8888
{{/supportAsync}}
8989

9090
[features]
91-
default = ["native-tls"]
91+
default = [{{#reqwestDefaultFeatures}}"{{.}}"{{^-last}}, {{/-last}}{{/reqwestDefaultFeatures}}]
9292
native-tls = ["reqwest/native-tls"]
9393
rustls-tls = ["reqwest/rustls-tls"]
9494
{{/reqwest}}
@@ -109,7 +109,7 @@ mockall = { version = "^0.13", optional = true}
109109
bon = { version = "2.3", optional = true }
110110
{{/useBonBuilder}}
111111
[features]
112-
default = ["native-tls"]
112+
default = [{{#reqwestDefaultFeatures}}"{{.}}"{{^-last}}, {{/-last}}{{/reqwestDefaultFeatures}}]
113113
native-tls = ["reqwest/native-tls"]
114114
rustls-tls = ["reqwest/rustls-tls"]
115115
{{#mockall}}

0 commit comments

Comments
 (0)