|
| 1 | +package io.swagger.codegen.languages; |
| 2 | + |
| 3 | +import com.samskivert.mustache.Mustache; |
| 4 | +import com.samskivert.mustache.Template; |
| 5 | +import io.swagger.codegen.*; |
| 6 | +import io.swagger.models.properties.ArrayProperty; |
| 7 | +import io.swagger.models.properties.MapProperty; |
| 8 | +import io.swagger.models.properties.Property; |
| 9 | +import org.apache.commons.lang3.StringUtils; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.StringWriter; |
| 14 | +import java.io.Writer; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +public class DartJaguarClientCodegen extends DartClientCodegen { |
| 21 | + private static Set<String> modelToIgnore = new HashSet<>(); |
| 22 | + static { |
| 23 | + modelToIgnore.add("datetime"); |
| 24 | + modelToIgnore.add("list"); |
| 25 | + modelToIgnore.add("map"); |
| 26 | + modelToIgnore.add("file"); |
| 27 | + } |
| 28 | + public DartJaguarClientCodegen() { |
| 29 | + super(); |
| 30 | + browserClient = false; |
| 31 | + outputFolder = "generated-code/dart-jaguar"; |
| 32 | + embeddedTemplateDir = templateDir = "dart-jaguar"; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getName() { |
| 37 | + return "dart-jaguar"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getHelp() { |
| 42 | + return "Generates a Dart Jaguar client library."; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String toDefaultValue(Property p) { |
| 47 | + if (p instanceof MapProperty) { |
| 48 | + return "const {}"; |
| 49 | + } else if (p instanceof ArrayProperty) { |
| 50 | + return "const []"; |
| 51 | + } |
| 52 | + return super.toDefaultValue(p); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void processOpts() { |
| 57 | + if (additionalProperties.containsKey(PUB_NAME)) { |
| 58 | + this.setPubName((String) additionalProperties.get(PUB_NAME)); |
| 59 | + } else { |
| 60 | + //not set, use to be passed to template |
| 61 | + additionalProperties.put(PUB_NAME, pubName); |
| 62 | + } |
| 63 | + |
| 64 | + if (additionalProperties.containsKey(PUB_VERSION)) { |
| 65 | + this.setPubVersion((String) additionalProperties.get(PUB_VERSION)); |
| 66 | + } else { |
| 67 | + //not set, use to be passed to template |
| 68 | + additionalProperties.put(PUB_VERSION, pubVersion); |
| 69 | + } |
| 70 | + |
| 71 | + if (additionalProperties.containsKey(PUB_DESCRIPTION)) { |
| 72 | + this.setPubDescription((String) additionalProperties.get(PUB_DESCRIPTION)); |
| 73 | + } else { |
| 74 | + //not set, use to be passed to template |
| 75 | + additionalProperties.put(PUB_DESCRIPTION, pubDescription); |
| 76 | + } |
| 77 | + |
| 78 | + if (additionalProperties.containsKey(USE_ENUM_EXTENSION)) { |
| 79 | + this.setUseEnumExtension(convertPropertyToBooleanAndWriteBack(USE_ENUM_EXTENSION)); |
| 80 | + } else { |
| 81 | + // Not set, use to be passed to template. |
| 82 | + additionalProperties.put(USE_ENUM_EXTENSION, useEnumExtension); |
| 83 | + } |
| 84 | + |
| 85 | + if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) { |
| 86 | + this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER)); |
| 87 | + } |
| 88 | + |
| 89 | + // make api and model doc path available in mustache template |
| 90 | + additionalProperties.put("apiDocPath", apiDocPath); |
| 91 | + additionalProperties.put("modelDocPath", modelDocPath); |
| 92 | + |
| 93 | + final String libFolder = sourceFolder + File.separator + "lib"; |
| 94 | + |
| 95 | + supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml")); |
| 96 | + supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml")); |
| 97 | + supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart")); |
| 98 | + |
| 99 | + supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); |
| 100 | + supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); |
| 101 | + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); |
| 102 | + |
| 103 | + final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth"; |
| 104 | + supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart")); |
| 105 | + supportingFiles.add(new SupportingFile("auth/basic_auth.mustache", authFolder, "basic_auth.dart")); |
| 106 | + supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart")); |
| 107 | + supportingFiles.add(new SupportingFile("auth/auth.mustache", authFolder, "auth.dart")); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @Override |
| 112 | + public Map<String, Object> postProcessModels(Map<String, Object> objs) { |
| 113 | + objs = super.postProcessModels(objs); |
| 114 | + List<Object> models = (List<Object>) objs.get("models"); |
| 115 | + for (Object _mo : models) { |
| 116 | + Map<String, Object> mo = (Map<String, Object>) _mo; |
| 117 | + Set<String> modelImports = new HashSet<>(); |
| 118 | + CodegenModel cm = (CodegenModel) mo.get("model"); |
| 119 | + for (String modelImport : cm.imports) { |
| 120 | + if(!modelToIgnore.contains(modelImport.toLowerCase())) { |
| 121 | + modelImports.add(underscore(modelImport)); |
| 122 | + } |
| 123 | + } |
| 124 | + cm.imports = modelImports; |
| 125 | + cm.vendorExtensions.put("hasVars", cm.vars.size() > 0); |
| 126 | + } |
| 127 | + //objs.put("modelImports", modelImports); |
| 128 | + return objs; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 133 | + objs = super.postProcessOperations(objs); |
| 134 | + Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); |
| 135 | + List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation"); |
| 136 | + |
| 137 | + Set<String> modelImports = new HashSet<>(); |
| 138 | + |
| 139 | + for (CodegenOperation op : operationList) { |
| 140 | + op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase()); |
| 141 | + boolean isJson = true; //default to JSON |
| 142 | + boolean isForm = false; |
| 143 | + boolean isMultipart = false; |
| 144 | + if(op.consumes != null) { |
| 145 | + for (Map<String, String> consume : op.consumes) { |
| 146 | + if (consume.containsKey("mediaType")) { |
| 147 | + String type = consume.get("mediaType"); |
| 148 | + isJson = type.equalsIgnoreCase("application/json"); |
| 149 | + isForm = type.equalsIgnoreCase("application/x-www-form-urlencoded"); |
| 150 | + isMultipart = type.equalsIgnoreCase("multipart/form-data"); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + op.vendorExtensions.put("isJson", isJson); |
| 157 | + op.vendorExtensions.put("isForm", isForm); |
| 158 | + op.vendorExtensions.put("isMultipart", isMultipart); |
| 159 | + |
| 160 | + Set<String> imports = new HashSet<>(); |
| 161 | + for (String item : op.imports) { |
| 162 | + if(!modelToIgnore.contains(item.toLowerCase())) { |
| 163 | + imports.add(underscore(item)); |
| 164 | + } |
| 165 | + } |
| 166 | + modelImports.addAll(imports); |
| 167 | + op.imports = imports; |
| 168 | + |
| 169 | + String[] items = op.path.split("/", -1); |
| 170 | + String jaguarPath = ""; |
| 171 | + |
| 172 | + for (int i = 0; i < items.length; ++i) { |
| 173 | + if (items[i].matches("^\\{(.*)\\}$")) { // wrap in {} |
| 174 | + jaguarPath = jaguarPath + ":" + items[i].replace("{", "").replace("}", ""); |
| 175 | + } else { |
| 176 | + jaguarPath = jaguarPath + items[i]; |
| 177 | + } |
| 178 | + |
| 179 | + if (i != items.length -1) { |
| 180 | + jaguarPath = jaguarPath + "/"; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + op.path = jaguarPath; |
| 185 | + } |
| 186 | + |
| 187 | + objs.put("modelImports", modelImports); |
| 188 | + |
| 189 | + return objs; |
| 190 | + } |
| 191 | +} |
0 commit comments