feat: [rust] Dynamically add uuid crate only if used (#20619)

This commit is contained in:
Ross Sullivan 2025-02-10 12:04:09 +09:00 committed by GitHub
parent c2c161eb26
commit 79f70dcc8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 49 additions and 16 deletions

View File

@ -38,11 +38,10 @@ import org.openapitools.codegen.CodegenConfig;
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 class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
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 class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
}
}
for (var param : operation.allParams) {
if (!hasUUIDs && param.isUuid) {
hasUUIDs = true;
break;
}
}
// http method verb conversion, depending on client library (e.g. Hyper: PUT => Put, Reqwest: PUT => put)
if (HYPER_LIBRARY.equals(getLibrary())) {
operation.httpMethod = StringUtils.camelize(operation.httpMethod.toLowerCase(Locale.ROOT));
@ -705,9 +716,43 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
}*/
}
if (!hasUUIDs) {
for (var map : allModels) {
CodegenModel m = map.getModel();
if (m.getIsUuid() || hasUuidInProperties(m.vars)) {
hasUUIDs = true;
LOGGER.debug("found UUID in model: " + m.name);
break;
}
}
}
this.additionalProperties.put("hasUUIDs", hasUUIDs);
return objs;
}
/**
* Recursively searches for a model's properties for a UUID type field.
*/
private boolean hasUuidInProperties(List<CodegenProperty> 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) {

View File

@ -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"] }

View File

@ -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" }

View File

@ -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" }

View File

@ -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" }

View File

@ -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" }

View File

@ -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" }

View File

@ -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" }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }

View File

@ -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"] }