|
45 | 45 | import java.math.BigDecimal; |
46 | 46 | import java.math.BigInteger; |
47 | 47 | import java.util.*; |
| 48 | +import java.util.function.Function; |
48 | 49 | import java.util.stream.Collectors; |
49 | 50 |
|
50 | 51 | public class RustClientCodegen extends AbstractRustCodegen implements CodegenConfig { |
@@ -91,6 +92,9 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon |
91 | 92 | // The API has at least one UUID type. |
92 | 93 | // If the API does not contain any UUIDs we do not need depend on the `uuid` crate |
93 | 94 | 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; |
94 | 98 |
|
95 | 99 | @Override |
96 | 100 | public CodegenType getTag() { |
@@ -182,8 +186,8 @@ public RustClientCodegen() { |
182 | 186 | typeMapping.put("map", "std::collections::HashMap"); |
183 | 187 | typeMapping.put("UUID", "uuid::Uuid"); |
184 | 188 | 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"); |
187 | 191 | typeMapping.put("password", "String"); |
188 | 192 | typeMapping.put("decimal", "String"); |
189 | 193 |
|
@@ -772,12 +776,21 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo |
772 | 776 | } |
773 | 777 | } |
774 | 778 |
|
| 779 | + // Check for UUIDs |
775 | 780 | for (var param : operation.allParams) { |
776 | 781 | if (!hasUUIDs && param.isUuid) { |
777 | 782 | hasUUIDs = true; |
778 | 783 | break; |
779 | 784 | } |
780 | 785 | } |
| 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 | + } |
781 | 794 |
|
782 | 795 | // If we use a file body parameter, we need to include the imports and crates for it |
783 | 796 | // But they should be added only once per file |
@@ -874,32 +887,55 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo |
874 | 887 | CodegenModel m = map.getModel(); |
875 | 888 | if (m.getIsUuid() || hasUuidInProperties(m.vars)) { |
876 | 889 | 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); |
878 | 902 | break; |
879 | 903 | } |
880 | 904 | } |
881 | 905 | } |
882 | 906 |
|
883 | 907 | this.additionalProperties.put("hasUUIDs", hasUUIDs); |
| 908 | + this.additionalProperties.put("hasChronoType", hasChronoTypes); |
884 | 909 | return objs; |
885 | 910 | } |
886 | 911 |
|
887 | 912 | /** |
888 | 913 | * Recursively searches for a model's properties for a UUID type field. |
889 | 914 | */ |
890 | 915 | 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){ |
891 | 927 | for (CodegenProperty property : properties) { |
892 | | - if (property.isUuid) { |
| 928 | + if (propertyCheck.apply(property)) { |
893 | 929 | return true; |
894 | 930 | } |
895 | 931 | // Check nested properties |
896 | | - if (property.items != null && hasUuidInProperties(Collections.singletonList(property.items))) { |
| 932 | + if (property.items != null && checkForPropertiesRecursively(Collections.singletonList(property.items), propertyCheck)) { |
897 | 933 | return true; |
898 | 934 | } |
899 | | - if (property.additionalProperties != null && hasUuidInProperties(Collections.singletonList(property.additionalProperties))) { |
| 935 | + if (property.additionalProperties != null && checkForPropertiesRecursively(Collections.singletonList(property.additionalProperties), propertyCheck)) { |
900 | 936 | return true; |
901 | 937 | } |
902 | | - if (property.vars != null && hasUuidInProperties(property.vars)) { |
| 938 | + if (property.vars != null && checkForPropertiesRecursively(property.vars, propertyCheck)) { |
903 | 939 | return true; |
904 | 940 | } |
905 | 941 | } |
|
0 commit comments