Skip to content

Commit c45a093

Browse files
committed
Functional Change
1 parent 05dd7fb commit c45a093

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.math.BigDecimal;
4646
import java.math.BigInteger;
4747
import java.util.*;
48+
import java.util.function.Function;
4849
import java.util.stream.Collectors;
4950

5051
public class RustClientCodegen extends AbstractRustCodegen implements CodegenConfig {
@@ -91,6 +92,9 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon
9192
// The API has at least one UUID type.
9293
// If the API does not contain any UUIDs we do not need depend on the `uuid` crate
9394
private boolean hasUUIDs = false;
95+
// The API has at least one Chrono type.
96+
// If the API does not contain any Dates or DateTimes we do not need to depend on the `chrono` crate
97+
private boolean hasChronoTypes = false;
9498

9599
@Override
96100
public CodegenType getTag() {
@@ -182,8 +186,8 @@ public RustClientCodegen() {
182186
typeMapping.put("map", "std::collections::HashMap");
183187
typeMapping.put("UUID", "uuid::Uuid");
184188
typeMapping.put("URI", "String");
185-
typeMapping.put("date", "string");
186-
typeMapping.put("DateTime", "String");
189+
typeMapping.put("date", "chrono::NaiveDate");
190+
typeMapping.put("DateTime", "chrono::NaiveDateTime");
187191
typeMapping.put("password", "String");
188192
typeMapping.put("decimal", "String");
189193

@@ -772,12 +776,21 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
772776
}
773777
}
774778

779+
// Check for UUIDs
775780
for (var param : operation.allParams) {
776781
if (!hasUUIDs && param.isUuid) {
777782
hasUUIDs = true;
778783
break;
779784
}
780785
}
786+
// Check for Chrono Types
787+
for (CodegenParameter param : operation.allParams) {
788+
if (!hasChronoTypes && (param.isDate || param.isDateTime)) {
789+
LOGGER.debug("Found Chrono Type in operation Parameter: {}", param.paramName);
790+
hasChronoTypes = true;
791+
break;
792+
}
793+
}
781794

782795
// If we use a file body parameter, we need to include the imports and crates for it
783796
// But they should be added only once per file
@@ -874,32 +887,55 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
874887
CodegenModel m = map.getModel();
875888
if (m.getIsUuid() || hasUuidInProperties(m.vars)) {
876889
hasUUIDs = true;
877-
LOGGER.debug("found UUID in model: " + m.name);
890+
LOGGER.debug("Found UUID in model: {}", m.name);
891+
break;
892+
}
893+
}
894+
}
895+
896+
if (!hasChronoTypes) {
897+
for (var map : allModels) {
898+
CodegenModel m = map.getModel();
899+
if (m.getIsDate() || hasChronoTypeInProperties(m.vars)) {
900+
hasChronoTypes = true;
901+
LOGGER.debug("Found Chrono Type in model: {}", m.name);
878902
break;
879903
}
880904
}
881905
}
882906

883907
this.additionalProperties.put("hasUUIDs", hasUUIDs);
908+
this.additionalProperties.put("hasChronoType", hasChronoTypes);
884909
return objs;
885910
}
886911

887912
/**
888913
* Recursively searches for a model's properties for a UUID type field.
889914
*/
890915
private boolean hasUuidInProperties(List<CodegenProperty> properties) {
916+
return checkForPropertiesRecursively(properties, (property) -> property.isUuid);
917+
}
918+
919+
/**
920+
* Recursively searches for a model's properties for a Date or DateTime type field.
921+
*/
922+
private boolean hasChronoTypeInProperties(List<CodegenProperty> properties) {
923+
return checkForPropertiesRecursively(properties, (property) -> property.isDate || property.isDateTime);
924+
}
925+
926+
private boolean checkForPropertiesRecursively(List<CodegenProperty> properties, Function<CodegenProperty, Boolean> propertyCheck){
891927
for (CodegenProperty property : properties) {
892-
if (property.isUuid) {
928+
if (propertyCheck.apply(property)) {
893929
return true;
894930
}
895931
// Check nested properties
896-
if (property.items != null && hasUuidInProperties(Collections.singletonList(property.items))) {
932+
if (property.items != null && checkForPropertiesRecursively(Collections.singletonList(property.items), propertyCheck)) {
897933
return true;
898934
}
899-
if (property.additionalProperties != null && hasUuidInProperties(Collections.singletonList(property.additionalProperties))) {
935+
if (property.additionalProperties != null && checkForPropertiesRecursively(Collections.singletonList(property.additionalProperties), propertyCheck)) {
900936
return true;
901937
}
902-
if (property.vars != null && hasUuidInProperties(property.vars)) {
938+
if (property.vars != null && checkForPropertiesRecursively(property.vars, propertyCheck)) {
903939
return true;
904940
}
905941
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ url = "^2.5"
4545
{{#hasUUIDs}}
4646
uuid = { version = "^1.8", features = ["serde", "v4"] }
4747
{{/hasUUIDs}}
48+
{{#hasChronoTypes}}
49+
chrono = { version = "^0.4" }
50+
{{/hasChronoTypes}}
4851
{{#hyper}}
4952
{{#hyper0x}}
5053
hyper = { version = "~0.14", features = ["full"] }

0 commit comments

Comments
 (0)