|
41 | 41 | import java.time.temporal.ChronoField; |
42 | 42 | import java.util.*; |
43 | 43 | import java.util.concurrent.TimeUnit; |
| 44 | +import java.util.stream.Collectors; |
44 | 45 |
|
45 | 46 | import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; |
46 | 47 | import static org.openapitools.codegen.utils.StringUtils.camelize; |
@@ -81,6 +82,9 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig |
81 | 82 | public static final String MAP_FILE_BINARY_TO_DATA = "mapFileBinaryToData"; |
82 | 83 | public static final String USE_CUSTOM_DATE_WITHOUT_TIME = "useCustomDateWithoutTime"; |
83 | 84 | public static final String VALIDATABLE = "validatable"; |
| 85 | + public static final String ADDITIONAL_MODEL_OBJECT_ATTRIBUTES = "additionalModelObjectAttributes"; |
| 86 | + public static final String ADDITIONAL_MODEL_ENUM_ATTRIBUTES = "additionalModelEnumAttributes"; |
| 87 | + public static final String ADDITIONAL_MODEL_IMPORTS = "additionalModelImports"; |
84 | 88 | protected static final String LIBRARY_ALAMOFIRE = "alamofire"; |
85 | 89 | protected static final String LIBRARY_URLSESSION = "urlsession"; |
86 | 90 | protected static final String LIBRARY_VAPOR = "vapor"; |
@@ -125,6 +129,12 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig |
125 | 129 | protected boolean useCustomDateWithoutTime = false; |
126 | 130 | @Setter |
127 | 131 | protected boolean validatable = true; |
| 132 | + @Getter @Setter |
| 133 | + protected List<String> additionalModelObjectAttributes = new LinkedList<>(); |
| 134 | + @Getter @Setter |
| 135 | + protected List<String> additionalModelEnumAttributes = new LinkedList<>(); |
| 136 | + @Getter @Setter |
| 137 | + protected List<String> additionalModelImports = new LinkedList<>(); |
128 | 138 | @Setter |
129 | 139 | protected String[] responseAs = new String[0]; |
130 | 140 | protected String sourceFolder = swiftPackagePath; |
@@ -349,6 +359,18 @@ public Swift5ClientCodegen() { |
349 | 359 | "Make validation rules and validator for model properties (default: true)") |
350 | 360 | .defaultValue(Boolean.TRUE.toString())); |
351 | 361 |
|
| 362 | + cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_OBJECT_ATTRIBUTES, |
| 363 | + "Additional Swift attributes prepended to generated model struct/class declarations " |
| 364 | + + "(e.g. @MainActor, custom @attached macros). " |
| 365 | + + "List separated by semicolon (;) or new line (Linux or Windows).")); |
| 366 | + cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_ENUM_ATTRIBUTES, |
| 367 | + "Additional Swift attributes prepended to generated model enum declarations " |
| 368 | + + "(e.g. @CasePathable, @dynamicMemberLookup, custom @attached macros). " |
| 369 | + + "List separated by semicolon (;) or new line (Linux or Windows).")); |
| 370 | + cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_IMPORTS, |
| 371 | + "Additional Swift modules to import in every generated model file. " |
| 372 | + + "List separated by semicolon (;) or new line (Linux or Windows).")); |
| 373 | + |
352 | 374 | supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession"); |
353 | 375 | supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire"); |
354 | 376 | supportedLibraries.put(LIBRARY_VAPOR, "HTTP client: Vapor"); |
@@ -674,6 +696,48 @@ public void processOpts() { |
674 | 696 | break; |
675 | 697 | } |
676 | 698 |
|
| 699 | + if (additionalProperties.containsKey(ADDITIONAL_MODEL_OBJECT_ATTRIBUTES)) { |
| 700 | + String value = additionalProperties.get(ADDITIONAL_MODEL_OBJECT_ATTRIBUTES).toString(); |
| 701 | + setAdditionalModelObjectAttributes(splitAdditionalModelOption(value)); |
| 702 | + } |
| 703 | + if (additionalProperties.containsKey(ADDITIONAL_MODEL_ENUM_ATTRIBUTES)) { |
| 704 | + String value = additionalProperties.get(ADDITIONAL_MODEL_ENUM_ATTRIBUTES).toString(); |
| 705 | + setAdditionalModelEnumAttributes(splitAdditionalModelOption(value)); |
| 706 | + } |
| 707 | + if (additionalProperties.containsKey(ADDITIONAL_MODEL_IMPORTS)) { |
| 708 | + String value = additionalProperties.get(ADDITIONAL_MODEL_IMPORTS).toString(); |
| 709 | + setAdditionalModelImports(splitAdditionalModelOption(value)); |
| 710 | + } |
| 711 | + } |
| 712 | + |
| 713 | + private static List<String> splitAdditionalModelOption(String value) { |
| 714 | + return Arrays.stream(SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX.split(value.trim())) |
| 715 | + .map(String::trim) |
| 716 | + .filter(s -> !s.isEmpty()) |
| 717 | + .collect(Collectors.toList()); |
| 718 | + } |
| 719 | + |
| 720 | + @Override |
| 721 | + public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) { |
| 722 | + objs = super.postProcessAllModels(objs); |
| 723 | + if (additionalModelObjectAttributes.isEmpty() |
| 724 | + && additionalModelEnumAttributes.isEmpty() |
| 725 | + && additionalModelImports.isEmpty()) { |
| 726 | + return objs; |
| 727 | + } |
| 728 | + for (String modelName : objs.keySet()) { |
| 729 | + Map<String, Object> models = (Map<String, Object>) objs.get(modelName); |
| 730 | + if (!additionalModelObjectAttributes.isEmpty()) { |
| 731 | + models.put(ADDITIONAL_MODEL_OBJECT_ATTRIBUTES, additionalModelObjectAttributes); |
| 732 | + } |
| 733 | + if (!additionalModelEnumAttributes.isEmpty()) { |
| 734 | + models.put(ADDITIONAL_MODEL_ENUM_ATTRIBUTES, additionalModelEnumAttributes); |
| 735 | + } |
| 736 | + if (!additionalModelImports.isEmpty()) { |
| 737 | + models.put(ADDITIONAL_MODEL_IMPORTS, additionalModelImports); |
| 738 | + } |
| 739 | + } |
| 740 | + return objs; |
677 | 741 | } |
678 | 742 |
|
679 | 743 | @Override |
|
0 commit comments