|
| 1 | +package com.wordnik.swagger.codegen.languages; |
| 2 | + |
| 3 | +import com.wordnik.swagger.codegen.*; |
| 4 | +import com.wordnik.swagger.models.Operation; |
| 5 | +import com.wordnik.swagger.models.properties.*; |
| 6 | + |
| 7 | +import java.util.*; |
| 8 | +import java.io.File; |
| 9 | + |
| 10 | +public class RetrofitClientCodegen extends DefaultCodegen implements CodegenConfig { |
| 11 | + protected String invokerPackage = "io.swagger.client"; |
| 12 | + protected String groupId = "io.swagger"; |
| 13 | + protected String artifactId = "swagger-java-client"; |
| 14 | + protected String artifactVersion = "1.0.0"; |
| 15 | + protected String sourceFolder = "src/main/java"; |
| 16 | + |
| 17 | + public CodegenType getTag() { |
| 18 | + return CodegenType.CLIENT; |
| 19 | + } |
| 20 | + |
| 21 | + public String getName() { |
| 22 | + return "retrofit"; |
| 23 | + } |
| 24 | + |
| 25 | + public String getHelp() { |
| 26 | + return "Generates a Retrofit client library."; |
| 27 | + } |
| 28 | + |
| 29 | + public RetrofitClientCodegen() { |
| 30 | + super(); |
| 31 | + outputFolder = "generated-code/java"; |
| 32 | + modelTemplateFiles.put("model.mustache", ".java"); |
| 33 | + apiTemplateFiles.put("api.mustache", ".java"); |
| 34 | + templateDir = "retrofit"; |
| 35 | + apiPackage = "io.swagger.client.api"; |
| 36 | + modelPackage = "io.swagger.client.model"; |
| 37 | + |
| 38 | + reservedWords = new HashSet<String> ( |
| 39 | + Arrays.asList( |
| 40 | + "abstract", "continue", "for", "new", "switch", "assert", |
| 41 | + "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", |
| 42 | + "this", "break", "double", "implements", "protected", "throw", "byte", "else", |
| 43 | + "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", |
| 44 | + "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", |
| 45 | + "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", |
| 46 | + "native", "super", "while") |
| 47 | + ); |
| 48 | + |
| 49 | + additionalProperties.put("invokerPackage", invokerPackage); |
| 50 | + additionalProperties.put("groupId", groupId); |
| 51 | + additionalProperties.put("artifactId", artifactId); |
| 52 | + additionalProperties.put("artifactVersion", artifactVersion); |
| 53 | + |
| 54 | + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); |
| 55 | + supportingFiles.add(new SupportingFile("service.mustache", |
| 56 | + (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ServiceGenerator.java")); |
| 57 | + |
| 58 | + languageSpecificPrimitives = new HashSet<String>( |
| 59 | + Arrays.asList( |
| 60 | + "String", |
| 61 | + "boolean", |
| 62 | + "Boolean", |
| 63 | + "Double", |
| 64 | + "Integer", |
| 65 | + "Long", |
| 66 | + "Float", |
| 67 | + "Object") |
| 68 | + ); |
| 69 | + instantiationTypes.put("array", "ArrayList"); |
| 70 | + instantiationTypes.put("map", "HashMap"); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String escapeReservedWord(String name) { |
| 75 | + return "_" + name; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String apiFileFolder() { |
| 80 | + return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar); |
| 81 | + } |
| 82 | + |
| 83 | + public String modelFileFolder() { |
| 84 | + return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toVarName(String name) { |
| 89 | + // replace - with _ e.g. created-at => created_at |
| 90 | + name = name.replaceAll("-", "_"); |
| 91 | + |
| 92 | + // if it's all uppper case, do nothing |
| 93 | + if (name.matches("^[A-Z_]*$")) |
| 94 | + return name; |
| 95 | + |
| 96 | + // camelize (lower first character) the variable name |
| 97 | + // pet_id => petId |
| 98 | + name = camelize(name, true); |
| 99 | + |
| 100 | + // for reserved word or word starting with number, append _ |
| 101 | + if(reservedWords.contains(name) || name.matches("^\\d.*")) |
| 102 | + name = escapeReservedWord(name); |
| 103 | + |
| 104 | + return name; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String toParamName(String name) { |
| 109 | + // should be the same as variable name |
| 110 | + return toVarName(name); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toModelName(String name) { |
| 115 | + // model name cannot use reserved keyword, e.g. return |
| 116 | + if(reservedWords.contains(name)) |
| 117 | + throw new RuntimeException(name + " (reserved word) cannot be used as a model name"); |
| 118 | + |
| 119 | + // camelize the model name |
| 120 | + // phone_number => PhoneNumber |
| 121 | + return camelize(name); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String toModelFilename(String name) { |
| 126 | + // should be the same as the model name |
| 127 | + return toModelName(name); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getTypeDeclaration(Property p) { |
| 133 | + if(p instanceof ArrayProperty) { |
| 134 | + ArrayProperty ap = (ArrayProperty) p; |
| 135 | + Property inner = ap.getItems(); |
| 136 | + return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">"; |
| 137 | + } |
| 138 | + else if (p instanceof MapProperty) { |
| 139 | + MapProperty mp = (MapProperty) p; |
| 140 | + Property inner = mp.getAdditionalProperties(); |
| 141 | + |
| 142 | + return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">"; |
| 143 | + } |
| 144 | + return super.getTypeDeclaration(p); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getSwaggerType(Property p) { |
| 149 | + String swaggerType = super.getSwaggerType(p); |
| 150 | + String type = null; |
| 151 | + if(typeMapping.containsKey(swaggerType)) { |
| 152 | + type = typeMapping.get(swaggerType); |
| 153 | + if(languageSpecificPrimitives.contains(type)) |
| 154 | + return toModelName(type); |
| 155 | + } |
| 156 | + else |
| 157 | + type = swaggerType; |
| 158 | + return toModelName(type); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String toOperationId(String operationId) { |
| 163 | + // method name cannot use reserved keyword, e.g. return |
| 164 | + if(reservedWords.contains(operationId)) |
| 165 | + throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); |
| 166 | + |
| 167 | + return camelize(operationId, true); |
| 168 | + } |
| 169 | + |
| 170 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 171 | + Map<String, Object> operations = (Map<String, Object>)objs.get("operations"); |
| 172 | + if(operations != null) { |
| 173 | + List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); |
| 174 | + for(CodegenOperation operation : ops) { |
| 175 | + if (operation.hasConsumes == Boolean.TRUE) { |
| 176 | + Map<String, String> firstType = operation.consumes.get(0); |
| 177 | + if (firstType != null) { |
| 178 | + if ("multipart/form-data".equals(firstType.get("mediaType"))) { |
| 179 | + operation.isMultipart = Boolean.TRUE; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + if(operation.returnType == null) { |
| 184 | + operation.returnType = "Void"; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + return objs; |
| 189 | + } |
| 190 | +} |
0 commit comments