Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -642,6 +646,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}
}

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));
Expand Down Expand Up @@ -705,9 +716,43 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}*/
}

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: add a docstring explaining what this function does

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd want a sample where the condition procs true too, then? To make sure the code paths are correct.

@ranger-ross ranger-ross Feb 8, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" }
Expand Down
1 change: 0 additions & 1 deletion samples/client/others/rust/hyper/composed-oneof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion samples/client/others/rust/hyper/emptyObject/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion samples/client/others/rust/hyper/oneOf-reuseRef/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion samples/client/others/rust/hyper/oneOf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 0 additions & 1 deletion samples/client/others/rust/reqwest/emptyObject/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 0 additions & 1 deletion samples/client/others/rust/reqwest/oneOf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }