From 628cc9703466a0d285eb261b35a8d6a72127aeb0 Mon Sep 17 00:00:00 2001 From: Ross Sullivan Date: Sat, 8 Feb 2025 19:07:07 +0900 Subject: [PATCH] feat: [rust] Dynamically add uuid crate only if used --- .../codegen/languages/RustClientCodegen.java | 49 ++++++++++++++++++- .../src/main/resources/rust/Cargo.mustache | 2 + .../rust/hyper/api-with-ref-param/Cargo.toml | 1 - .../rust/hyper/composed-oneof/Cargo.toml | 1 - .../others/rust/hyper/emptyObject/Cargo.toml | 1 - .../rust/hyper/oneOf-array-map/Cargo.toml | 1 - .../rust/hyper/oneOf-reuseRef/Cargo.toml | 1 - .../client/others/rust/hyper/oneOf/Cargo.toml | 1 - .../rust/reqwest-regression-16119/Cargo.toml | 1 - .../reqwest/api-with-ref-param/Cargo.toml | 1 - .../rust/reqwest/composed-oneof/Cargo.toml | 1 - .../rust/reqwest/emptyObject/Cargo.toml | 1 - .../rust/reqwest/oneOf-array-map/Cargo.toml | 1 - .../rust/reqwest/oneOf-reuseRef/Cargo.toml | 1 - .../others/rust/reqwest/oneOf/Cargo.toml | 1 - .../rust/reqwest/name-mapping/Cargo.toml | 1 - 16 files changed, 49 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java index 3f8ba7bb3b5b..9a287f7a3fd0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java @@ -38,11 +38,10 @@ import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; -import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.CodegenParameter; +import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.SupportingFile; -import org.openapitools.codegen.VendorExtension; import org.openapitools.codegen.meta.features.ClientModificationFeature; import org.openapitools.codegen.meta.features.DocumentationFeature; import org.openapitools.codegen.meta.features.GlobalFeature; @@ -109,6 +108,10 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon protected String modelDocPath = "docs/"; protected String apiFolder = "src/apis"; protected String modelFolder = "src/models"; + // The API has at least one UUID type. + // If the API does not contain any UUIDs we do not need depend on the `uuid` crate + private boolean hasUUIDs = false; + @Override public CodegenType getTag() { @@ -253,6 +256,7 @@ public RustClientCodegen() { supportedLibraries.put(REQWEST_LIBRARY, "HTTP client: Reqwest."); supportedLibraries.put(REQWEST_TRAIT_LIBRARY, "HTTP client: Reqwest (trait based)."); + CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use."); libraryOption.setEnum(supportedLibraries); // set reqwest as the default @@ -642,6 +646,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List Put, Reqwest: PUT => put) if (HYPER_LIBRARY.equals(getLibrary())) { operation.httpMethod = StringUtils.camelize(operation.httpMethod.toLowerCase(Locale.ROOT)); @@ -705,9 +716,43 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List properties) { + for (CodegenProperty property : properties) { + if (property.isUuid) { + return true; + } + // Check nested properties + if (property.items != null && hasUuidInProperties(Collections.singletonList(property.items))) { + return true; + } + if (property.additionalProperties != null && hasUuidInProperties(Collections.singletonList(property.additionalProperties))) { + return true; + } + if (property.vars != null && hasUuidInProperties(property.vars)) { + return true; + } + } + return false; + } + @Override public String toDefaultValue(Schema p) { if (p.getDefault() != null) { diff --git a/modules/openapi-generator/src/main/resources/rust/Cargo.mustache b/modules/openapi-generator/src/main/resources/rust/Cargo.mustache index 55d75560fd75..6a9676d733f1 100644 --- a/modules/openapi-generator/src/main/resources/rust/Cargo.mustache +++ b/modules/openapi-generator/src/main/resources/rust/Cargo.mustache @@ -39,7 +39,9 @@ serde_with = { version = "^3.8", default-features = false, features = ["base64", serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" +{{#hasUUIDs}} uuid = { version = "^1.8", features = ["serde", "v4"] } +{{/hasUUIDs}} {{#hyper}} {{#hyper0x}} hyper = { version = "~0.14", features = ["full"] } diff --git a/samples/client/others/rust/hyper/api-with-ref-param/Cargo.toml b/samples/client/others/rust/hyper/api-with-ref-param/Cargo.toml index 00f46b1c7d3c..ca96e317ff6a 100644 --- a/samples/client/others/rust/hyper/api-with-ref-param/Cargo.toml +++ b/samples/client/others/rust/hyper/api-with-ref-param/Cargo.toml @@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/hyper/composed-oneof/Cargo.toml b/samples/client/others/rust/hyper/composed-oneof/Cargo.toml index 4c0a00557c55..b19d8e7b6fac 100644 --- a/samples/client/others/rust/hyper/composed-oneof/Cargo.toml +++ b/samples/client/others/rust/hyper/composed-oneof/Cargo.toml @@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/hyper/emptyObject/Cargo.toml b/samples/client/others/rust/hyper/emptyObject/Cargo.toml index 1efb624b65df..1b58d3838fc2 100644 --- a/samples/client/others/rust/hyper/emptyObject/Cargo.toml +++ b/samples/client/others/rust/hyper/emptyObject/Cargo.toml @@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/hyper/oneOf-array-map/Cargo.toml b/samples/client/others/rust/hyper/oneOf-array-map/Cargo.toml index 1bb7aa7bb54d..a9d454165554 100644 --- a/samples/client/others/rust/hyper/oneOf-array-map/Cargo.toml +++ b/samples/client/others/rust/hyper/oneOf-array-map/Cargo.toml @@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml b/samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml index 07b8f6cf6d8c..5367809843db 100644 --- a/samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml +++ b/samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml @@ -11,7 +11,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/hyper/oneOf/Cargo.toml b/samples/client/others/rust/hyper/oneOf/Cargo.toml index fb78d69e9100..d9569e6067cb 100644 --- a/samples/client/others/rust/hyper/oneOf/Cargo.toml +++ b/samples/client/others/rust/hyper/oneOf/Cargo.toml @@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } hyper = { version = "^1.3.1", features = ["full"] } hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } http-body-util = { version = "0.1.2" } diff --git a/samples/client/others/rust/reqwest-regression-16119/Cargo.toml b/samples/client/others/rust/reqwest-regression-16119/Cargo.toml index a1e41446d882..0712b88be4fc 100644 --- a/samples/client/others/rust/reqwest-regression-16119/Cargo.toml +++ b/samples/client/others/rust/reqwest-regression-16119/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/others/rust/reqwest/api-with-ref-param/Cargo.toml b/samples/client/others/rust/reqwest/api-with-ref-param/Cargo.toml index e4edc99b719c..7b36f89b9407 100644 --- a/samples/client/others/rust/reqwest/api-with-ref-param/Cargo.toml +++ b/samples/client/others/rust/reqwest/api-with-ref-param/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "multipart"] } diff --git a/samples/client/others/rust/reqwest/composed-oneof/Cargo.toml b/samples/client/others/rust/reqwest/composed-oneof/Cargo.toml index f06effdcb109..2a7c3a43bb72 100644 --- a/samples/client/others/rust/reqwest/composed-oneof/Cargo.toml +++ b/samples/client/others/rust/reqwest/composed-oneof/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/others/rust/reqwest/emptyObject/Cargo.toml b/samples/client/others/rust/reqwest/emptyObject/Cargo.toml index d35bad9e88dd..bf65d42d4cc5 100644 --- a/samples/client/others/rust/reqwest/emptyObject/Cargo.toml +++ b/samples/client/others/rust/reqwest/emptyObject/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/others/rust/reqwest/oneOf-array-map/Cargo.toml b/samples/client/others/rust/reqwest/oneOf-array-map/Cargo.toml index 5f2d99d18047..509f9c6d885d 100644 --- a/samples/client/others/rust/reqwest/oneOf-array-map/Cargo.toml +++ b/samples/client/others/rust/reqwest/oneOf-array-map/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/others/rust/reqwest/oneOf-reuseRef/Cargo.toml b/samples/client/others/rust/reqwest/oneOf-reuseRef/Cargo.toml index 64b0d4bf92f2..58b7a46e0454 100644 --- a/samples/client/others/rust/reqwest/oneOf-reuseRef/Cargo.toml +++ b/samples/client/others/rust/reqwest/oneOf-reuseRef/Cargo.toml @@ -11,5 +11,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/others/rust/reqwest/oneOf/Cargo.toml b/samples/client/others/rust/reqwest/oneOf/Cargo.toml index 8966bd1292c5..0af49f661581 100644 --- a/samples/client/others/rust/reqwest/oneOf/Cargo.toml +++ b/samples/client/others/rust/reqwest/oneOf/Cargo.toml @@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] } diff --git a/samples/client/petstore/rust/reqwest/name-mapping/Cargo.toml b/samples/client/petstore/rust/reqwest/name-mapping/Cargo.toml index 172f74f4e1ff..99c54a81fd21 100644 --- a/samples/client/petstore/rust/reqwest/name-mapping/Cargo.toml +++ b/samples/client/petstore/rust/reqwest/name-mapping/Cargo.toml @@ -11,5 +11,4 @@ serde = { version = "^1.0", features = ["derive"] } serde_json = "^1.0" serde_repr = "^0.1" url = "^2.5" -uuid = { version = "^1.8", features = ["serde", "v4"] } reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] }