Skip to content

Commit 79f70dc

Browse files
authored
feat: [rust] Dynamically add uuid crate only if used (#20619)
1 parent c2c161e commit 79f70dc

File tree

16 files changed

+49
-16
lines changed

16 files changed

+49
-16
lines changed

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
import org.openapitools.codegen.CodegenConstants;
3939
import org.openapitools.codegen.CodegenModel;
4040
import org.openapitools.codegen.CodegenOperation;
41-
import org.openapitools.codegen.CodegenProperty;
4241
import org.openapitools.codegen.CodegenParameter;
42+
import org.openapitools.codegen.CodegenProperty;
4343
import org.openapitools.codegen.CodegenType;
4444
import org.openapitools.codegen.SupportingFile;
45-
import org.openapitools.codegen.VendorExtension;
4645
import org.openapitools.codegen.meta.features.ClientModificationFeature;
4746
import org.openapitools.codegen.meta.features.DocumentationFeature;
4847
import org.openapitools.codegen.meta.features.GlobalFeature;
@@ -109,6 +108,10 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
109108
protected String modelDocPath = "docs/";
110109
protected String apiFolder = "src/apis";
111110
protected String modelFolder = "src/models";
111+
// The API has at least one UUID type.
112+
// If the API does not contain any UUIDs we do not need depend on the `uuid` crate
113+
private boolean hasUUIDs = false;
114+
112115

113116
@Override
114117
public CodegenType getTag() {
@@ -253,6 +256,7 @@ public RustClientCodegen() {
253256
supportedLibraries.put(REQWEST_LIBRARY, "HTTP client: Reqwest.");
254257
supportedLibraries.put(REQWEST_TRAIT_LIBRARY, "HTTP client: Reqwest (trait based).");
255258

259+
256260
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use.");
257261
libraryOption.setEnum(supportedLibraries);
258262
// set reqwest as the default
@@ -642,6 +646,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
642646
}
643647
}
644648

649+
for (var param : operation.allParams) {
650+
if (!hasUUIDs && param.isUuid) {
651+
hasUUIDs = true;
652+
break;
653+
}
654+
}
655+
645656
// http method verb conversion, depending on client library (e.g. Hyper: PUT => Put, Reqwest: PUT => put)
646657
if (HYPER_LIBRARY.equals(getLibrary())) {
647658
operation.httpMethod = StringUtils.camelize(operation.httpMethod.toLowerCase(Locale.ROOT));
@@ -705,9 +716,43 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
705716
}*/
706717
}
707718

719+
if (!hasUUIDs) {
720+
for (var map : allModels) {
721+
CodegenModel m = map.getModel();
722+
if (m.getIsUuid() || hasUuidInProperties(m.vars)) {
723+
hasUUIDs = true;
724+
LOGGER.debug("found UUID in model: " + m.name);
725+
break;
726+
}
727+
}
728+
}
729+
730+
this.additionalProperties.put("hasUUIDs", hasUUIDs);
708731
return objs;
709732
}
710733

734+
/**
735+
* Recursively searches for a model's properties for a UUID type field.
736+
*/
737+
private boolean hasUuidInProperties(List<CodegenProperty> properties) {
738+
for (CodegenProperty property : properties) {
739+
if (property.isUuid) {
740+
return true;
741+
}
742+
// Check nested properties
743+
if (property.items != null && hasUuidInProperties(Collections.singletonList(property.items))) {
744+
return true;
745+
}
746+
if (property.additionalProperties != null && hasUuidInProperties(Collections.singletonList(property.additionalProperties))) {
747+
return true;
748+
}
749+
if (property.vars != null && hasUuidInProperties(property.vars)) {
750+
return true;
751+
}
752+
}
753+
return false;
754+
}
755+
711756
@Override
712757
public String toDefaultValue(Schema p) {
713758
if (p.getDefault() != null) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ serde_with = { version = "^3.8", default-features = false, features = ["base64",
3939
serde_json = "^1.0"
4040
serde_repr = "^0.1"
4141
url = "^2.5"
42+
{{#hasUUIDs}}
4243
uuid = { version = "^1.8", features = ["serde", "v4"] }
44+
{{/hasUUIDs}}
4345
{{#hyper}}
4446
{{#hyper0x}}
4547
hyper = { version = "~0.14", features = ["full"] }

samples/client/others/rust/hyper/api-with-ref-param/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
hyper = { version = "^1.3.1", features = ["full"] }
1716
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1817
http-body-util = { version = "0.1.2" }

samples/client/others/rust/hyper/composed-oneof/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
hyper = { version = "^1.3.1", features = ["full"] }
1716
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1817
http-body-util = { version = "0.1.2" }

samples/client/others/rust/hyper/emptyObject/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
hyper = { version = "^1.3.1", features = ["full"] }
1716
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1817
http-body-util = { version = "0.1.2" }

samples/client/others/rust/hyper/oneOf-array-map/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
hyper = { version = "^1.3.1", features = ["full"] }
1716
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1817
http-body-util = { version = "0.1.2" }

samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ serde = { version = "^1.0", features = ["derive"] }
1111
serde_json = "^1.0"
1212
serde_repr = "^0.1"
1313
url = "^2.5"
14-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1514
hyper = { version = "^1.3.1", features = ["full"] }
1615
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1716
http-body-util = { version = "0.1.2" }

samples/client/others/rust/hyper/oneOf/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
hyper = { version = "^1.3.1", features = ["full"] }
1716
hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] }
1817
http-body-util = { version = "0.1.2" }

samples/client/others/rust/reqwest-regression-16119/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
reqwest = { version = "^0.12", features = ["json", "blocking", "multipart"] }

samples/client/others/rust/reqwest/api-with-ref-param/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ serde = { version = "^1.0", features = ["derive"] }
1212
serde_json = "^1.0"
1313
serde_repr = "^0.1"
1414
url = "^2.5"
15-
uuid = { version = "^1.8", features = ["serde", "v4"] }
1615
reqwest = { version = "^0.12", features = ["json", "multipart"] }

0 commit comments

Comments
 (0)