|
| 1 | +package io.swagger.codegen.languages.java; |
| 2 | + |
| 3 | +import io.swagger.codegen.CliOption; |
| 4 | +import io.swagger.codegen.CodegenConstants; |
| 5 | +import io.swagger.codegen.CodegenOperation; |
| 6 | +import io.swagger.codegen.CodegenParameter; |
| 7 | +import io.swagger.codegen.CodegenResponse; |
| 8 | +import io.swagger.codegen.CodegenType; |
| 9 | +import io.swagger.codegen.languages.features.BeanValidationFeatures; |
| 10 | +import io.swagger.codegen.utils.ModelUtils; |
| 11 | +import io.swagger.codegen.utils.URLPathUtil; |
| 12 | +import io.swagger.v3.oas.models.OpenAPI; |
| 13 | +import io.swagger.v3.oas.models.Operation; |
| 14 | +import io.swagger.v3.oas.models.PathItem; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import java.net.URL; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue; |
| 24 | + |
| 25 | +public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen implements BeanValidationFeatures { |
| 26 | + /** |
| 27 | + * Name of the sub-directory in "src/main/resource" where to find the |
| 28 | + * Mustache template for the JAX-RS Codegen. |
| 29 | + */ |
| 30 | + protected static final String JAXRS_TEMPLATE_DIRECTORY_NAME = "JavaJaxRS"; |
| 31 | + protected String implFolder = "src/main/java"; |
| 32 | + protected String testResourcesFolder = "src/test/resources"; |
| 33 | + protected String title = "Swagger Server"; |
| 34 | + |
| 35 | + protected boolean useBeanValidation = true; |
| 36 | + |
| 37 | + static Logger LOGGER = LoggerFactory.getLogger(AbstractJavaJAXRSServerCodegen.class); |
| 38 | + |
| 39 | + public AbstractJavaJAXRSServerCodegen() { |
| 40 | + super(); |
| 41 | + |
| 42 | + sourceFolder = "src/gen/java"; |
| 43 | + invokerPackage = "io.swagger.api"; |
| 44 | + artifactId = "swagger-jaxrs-server"; |
| 45 | + dateLibrary = "legacy"; //TODO: add joda support to all jax-rs |
| 46 | + |
| 47 | + apiPackage = "io.swagger.api"; |
| 48 | + modelPackage = "io.swagger.model"; |
| 49 | + |
| 50 | + additionalProperties.put("title", title); |
| 51 | + // java inflector uses the jackson lib |
| 52 | + additionalProperties.put("jackson", "true"); |
| 53 | + |
| 54 | + cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC)); |
| 55 | + cliOptions.add(new CliOption("title", "a title describing the application")); |
| 56 | + |
| 57 | + cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); |
| 58 | + cliOptions.add(new CliOption("serverPort", "The port on which the server should be started")); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + // =============== |
| 63 | + // COMMONS METHODS |
| 64 | + // =============== |
| 65 | + |
| 66 | + @Override |
| 67 | + public CodegenType getTag() { |
| 68 | + return CodegenType.SERVER; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void processOpts() { |
| 73 | + super.processOpts(); |
| 74 | + |
| 75 | + if (additionalProperties.containsKey(CodegenConstants.IMPL_FOLDER)) { |
| 76 | + implFolder = (String) additionalProperties.get(CodegenConstants.IMPL_FOLDER); |
| 77 | + } |
| 78 | + |
| 79 | + if (additionalProperties.containsKey(USE_BEANVALIDATION)) { |
| 80 | + this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION)); |
| 81 | + } |
| 82 | + |
| 83 | + if (useBeanValidation) { |
| 84 | + writePropertyBack(USE_BEANVALIDATION, useBeanValidation); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void preprocessOpenAPI(OpenAPI openAPI) { |
| 91 | + if (!this.additionalProperties.containsKey("serverPort")) { |
| 92 | + final URL urlInfo = URLPathUtil.getServerURL(openAPI); |
| 93 | + String port = "8080"; // Default value for a JEE Server |
| 94 | + if ( urlInfo != null && urlInfo.getPort() != 0) { |
| 95 | + port = String.valueOf(urlInfo.getPort()); |
| 96 | + } |
| 97 | + this.additionalProperties.put("serverPort", port); |
| 98 | + } |
| 99 | + |
| 100 | + if (openAPI.getPaths() != null) { |
| 101 | + for (String pathname : openAPI.getPaths().keySet()) { |
| 102 | + PathItem pathItem = openAPI.getPaths().get(pathname); |
| 103 | + final Operation[] operations = ModelUtils.createOperationArray(pathItem); |
| 104 | + for (Operation operation : operations) { |
| 105 | + if (operation != null && operation.getTags() != null) { |
| 106 | + List<Map<String, String>> tags = new ArrayList<Map<String, String>>(); |
| 107 | + for (String tag : operation.getTags()) { |
| 108 | + Map<String, String> value = new HashMap<String, String>(); |
| 109 | + value.put("tag", tag); |
| 110 | + value.put("hasMore", "true"); |
| 111 | + tags.add(value); |
| 112 | + } |
| 113 | + if (tags.size() > 0) { |
| 114 | + tags.get(tags.size() - 1).remove("hasMore"); |
| 115 | + } |
| 116 | + if (operation.getTags().size() > 0) { |
| 117 | + String tag = operation.getTags().get(0); |
| 118 | + operation.setTags(Arrays.asList(tag)); |
| 119 | + } |
| 120 | + operation.addExtension("x-tags", tags); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 129 | + @SuppressWarnings("unchecked") |
| 130 | + Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); |
| 131 | + if ( operations != null ) { |
| 132 | + @SuppressWarnings("unchecked") |
| 133 | + List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); |
| 134 | + for ( CodegenOperation operation : ops ) { |
| 135 | + boolean hasConsumes = getBooleanValue(operation, CodegenConstants.HAS_CONSUMES_EXT_NAME); |
| 136 | + if (hasConsumes) { |
| 137 | + Map<String, String> firstType = operation.consumes.get(0); |
| 138 | + if (firstType != null) { |
| 139 | + if ("multipart/form-data".equals(firstType.get("mediaType"))) { |
| 140 | + operation.getVendorExtensions().put(CodegenConstants.IS_MULTIPART_EXT_NAME, Boolean.TRUE); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + boolean isMultipartPost = false; |
| 146 | + List<Map<String, String>> consumes = operation.consumes; |
| 147 | + if(consumes != null) { |
| 148 | + for(Map<String, String> consume : consumes) { |
| 149 | + String mt = consume.get("mediaType"); |
| 150 | + if(mt != null) { |
| 151 | + if(mt.startsWith("multipart/form-data")) { |
| 152 | + isMultipartPost = true; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + for(CodegenParameter parameter : operation.allParams) { |
| 159 | + if(isMultipartPost) { |
| 160 | + parameter.vendorExtensions.put("x-multipart", "true"); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + List<CodegenResponse> responses = operation.responses; |
| 165 | + if ( responses != null ) { |
| 166 | + for ( CodegenResponse resp : responses ) { |
| 167 | + if ( "0".equals(resp.code) ) { |
| 168 | + resp.code = "200"; |
| 169 | + } |
| 170 | + |
| 171 | + if (resp.baseType == null) { |
| 172 | + resp.dataType = "void"; |
| 173 | + resp.baseType = "Void"; |
| 174 | + // set vendorExtensions.x-java-is-response-void to true as baseType is set to "Void" |
| 175 | + resp.vendorExtensions.put("x-java-is-response-void", true); |
| 176 | + } |
| 177 | + |
| 178 | + if ("array".equals(resp.containerType)) { |
| 179 | + resp.containerType = "List"; |
| 180 | + } else if ("map".equals(resp.containerType)) { |
| 181 | + resp.containerType = "Map"; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if ( operation.returnBaseType == null ) { |
| 187 | + operation.returnType = "void"; |
| 188 | + operation.returnBaseType = "Void"; |
| 189 | + // set vendorExtensions.x-java-is-response-void to true as returnBaseType is set to "Void" |
| 190 | + operation.vendorExtensions.put("x-java-is-response-void", true); |
| 191 | + } |
| 192 | + |
| 193 | + if ("array".equals(operation.returnContainer)) { |
| 194 | + operation.returnContainer = "List"; |
| 195 | + } else if ("map".equals(operation.returnContainer)) { |
| 196 | + operation.returnContainer = "Map"; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + return objs; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public String toApiName(final String name) { |
| 205 | + String computed = name; |
| 206 | + if ( computed.length() == 0 ) { |
| 207 | + return "DefaultApi"; |
| 208 | + } |
| 209 | + computed = sanitizeName(computed); |
| 210 | + return camelize(computed) + "Api"; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public String apiFilename(String templateName, String tag) { |
| 215 | + String result = super.apiFilename(templateName, tag); |
| 216 | + |
| 217 | + if ( templateName.endsWith("Impl.mustache") ) { |
| 218 | + int ix = result.lastIndexOf('/'); |
| 219 | + result = result.substring(0, ix) + "/impl" + result.substring(ix, result.length() - 5) + "ServiceImpl.java"; |
| 220 | + result = result.replace(apiFileFolder(), implFileFolder(implFolder)); |
| 221 | + } else if ( templateName.endsWith("Factory.mustache") ) { |
| 222 | + int ix = result.lastIndexOf('/'); |
| 223 | + result = result.substring(0, ix) + "/factories" + result.substring(ix, result.length() - 5) + "ServiceFactory.java"; |
| 224 | + result = result.replace(apiFileFolder(), implFileFolder(implFolder)); |
| 225 | + } else if ( templateName.endsWith("Service.mustache") ) { |
| 226 | + int ix = result.lastIndexOf('.'); |
| 227 | + result = result.substring(0, ix) + "Service.java"; |
| 228 | + } |
| 229 | + return result; |
| 230 | + } |
| 231 | + |
| 232 | + private String implFileFolder(String output) { |
| 233 | + return outputFolder + "/" + output + "/" + apiPackage().replace('.', '/'); |
| 234 | + } |
| 235 | + |
| 236 | + public void setUseBeanValidation(boolean useBeanValidation) { |
| 237 | + this.useBeanValidation = useBeanValidation; |
| 238 | + } |
| 239 | + |
| 240 | + |
| 241 | +} |
0 commit comments