File size: 20,896 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.openapi;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.context.FieldValueResolver;
import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;
import com.github.jknack.handlebars.helper.StringHelpers;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.FileTemplateLoader;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.apache.commons.lang3.StringUtils;
import org.ballerinalang.openapi.exception.BallerinaOpenApiException;
import org.ballerinalang.openapi.model.BallerinaOpenApi;
import org.ballerinalang.openapi.model.GenSrcFile;
import org.ballerinalang.openapi.typemodel.BallerinaOpenApiType;
import org.ballerinalang.openapi.utils.CodegenUtils;
import org.ballerinalang.openapi.utils.GeneratorConstants;
import org.ballerinalang.openapi.utils.GeneratorConstants.GenType;
import org.ballerinalang.openapi.utils.TypeExtractorUtil;
import org.ballerinalang.tool.LauncherUtils;
import org.wso2.ballerinalang.compiler.util.ProjectDirs;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.ballerinalang.openapi.model.GenSrcFile.GenFileType;
import static org.ballerinalang.openapi.utils.GeneratorConstants.GenType.GEN_CLIENT;
import static org.ballerinalang.openapi.utils.GeneratorConstants.MODULE_MD;
/**
* This class generates Ballerina Services/Clients for a provided OAS definition.
*/
public class CodeGenerator {
private String srcPackage;
private String modelPackage;
private static final PrintStream outStream = System.err;
/**
* Generates ballerina source for provided Open API Definition in {@code definitionPath}.
* Generated source will be written to a ballerina module at {@code outPath}
* <p>Method can be user for generating Ballerina mock services and clients</p>
*
* @param type Output type. Following types are supported
* <ul>
* <li>mock</li>
* <li>client</li>
* </ul>
* @param executionPath Command execution path
* @param definitionPath Input Open Api Definition file path
* @param serviceName Output Service Name
* @param outPath Destination file path to save generated source files. If not provided
* {@code definitionPath} will be used as the default destination path
* @throws IOException when file operations fail
* @throws BallerinaOpenApiException when code generator fails
*/
private void generate(GenType type, String executionPath, String definitionPath,
String reldefinitionPath , String serviceName, String outPath)
throws IOException, BallerinaOpenApiException {
if (!CodegenUtils.isBallerinaProject(Paths.get(outPath))) {
throw new BallerinaOpenApiException(OpenApiMesseges.GEN_CLIENT_PROJECT_ROOT);
}
//Check if the selected path is a ballerina root for service generation
//TODO check with team for root check
Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath));
if (type.equals(GenType.GEN_SERVICE) && projectRoot == null) {
throw LauncherUtils.createUsageExceptionWithHelp(OpenApiMesseges.GEN_SERVICE_PROJECT_ROOT);
}
Path srcPath = CodegenUtils.getSourcePath(srcPackage, outPath);
Path implPath = CodegenUtils.getImplPath(srcPackage, srcPath);
if (type.equals(GEN_CLIENT)) {
if (srcPackage.equals("")) {
srcPath = srcPath.resolve("client");
implPath = implPath.resolve("client");
}
if (Files.notExists(srcPath)) {
Files.createDirectory(srcPath);
}
if (Files.notExists(implPath)) {
Files.createDirectory(implPath);
}
}
List<GenSrcFile> genFiles = generateBalSource(type, definitionPath, reldefinitionPath, serviceName);
writeGeneratedSources(genFiles, srcPath, implPath, type);
}
/**
* Generates ballerina source for provided Open API Definition in {@code definitionPath}.
* Generated source will be written to a ballerina module at {@code outPath}
* Method can be user for generating Ballerina clients.
*
* @param executionPath Command execution path
* @param definitionPath Input Open Api Definition file path
// BUG: CWE-306 Missing Authentication for Critical Function
//
// FIXED:
* @param serviceName service name
* @param outPath Destination file path to save generated source files. If not provided
* {@code definitionPath} will be used as the default destination path
* @throws IOException when file operations fail
* @throws BallerinaOpenApiException when code generator fails
*/
public void generateClient(String executionPath, String definitionPath, String serviceName, String outPath)
throws IOException, BallerinaOpenApiException {
generate(GenType.GEN_CLIENT, executionPath, definitionPath, null, serviceName, outPath);
}
/**
* Generates ballerina source for provided Open API Definition in {@code definitionPath}.
* Generated source will be written to a ballerina module at {@code outPath}
* Method can be user for generating Ballerina clients.
*
* @param executionPath Command execution path
* @param definitionPath Input Open Api Definition file path
* @param reldefinitionPath Relative definition path to be used in the generated ballerina code
* @param serviceName service name for the generated service
* @param outPath Destination file path to save generated source files. If not provided
* {@code definitionPath} will be used as the default destination path
* @throws IOException when file operations fail
* @throws BallerinaOpenApiException when code generator fails
*/
public void generateService(String executionPath, String definitionPath,
String reldefinitionPath, String serviceName, String outPath)
throws IOException, BallerinaOpenApiException {
generate(GenType.GEN_SERVICE, executionPath, definitionPath, reldefinitionPath, serviceName, outPath);
}
/**
* Generates ballerina source for provided Open API Definition in {@code definitionPath}.
* Generated code will be returned as a list of source files
* <p>Method can be user for generating Ballerina mock services and clients</p>
*
* @param type Output type. Following types are supported
* <ul>
* <li>mock</li>
* <li>client</li>
* </ul>
* @param serviceName Out put service name
* @param definitionPath Input Open Api Definition file path
* @param reldefinitionPath Relative OpenApi File
* @return a list of generated source files wrapped as {@link GenSrcFile}
* @throws IOException when file operations fail
* @throws BallerinaOpenApiException when open api context building fail
*/
public List<GenSrcFile> generateBalSource(GenType type, String definitionPath,
String reldefinitionPath, String serviceName)
throws IOException, BallerinaOpenApiException {
OpenAPI api = new OpenAPIV3Parser().read(definitionPath);
if (api == null) {
throw new BallerinaOpenApiException("Couldn't read the definition from file: " + definitionPath);
}
if (serviceName != null) {
api.getInfo().setTitle(serviceName);
} else if (api.getInfo() == null || StringUtils.isEmpty(api.getInfo().getTitle())) {
api.getInfo().setTitle(GeneratorConstants.UNTITLED_SERVICE);
}
List<GenSrcFile> sourceFiles;
switch (type) {
case GEN_CLIENT:
// modelPackage is not in use at the moment. All models will be written into same package
// as other src files.
// Therefore value set to modelPackage is ignored here
BallerinaOpenApi definitionContext = new BallerinaOpenApi().buildContext(api).srcPackage(srcPackage)
.modelPackage(srcPackage);
definitionContext.setDefinitionPath(reldefinitionPath);
sourceFiles = generateClient(definitionContext);
break;
case GEN_SERVICE:
final BallerinaOpenApiType openApi = TypeExtractorUtil.extractOpenApiObject(api);
openApi.setBalServiceName(serviceName);
openApi.setBalModule(srcPackage);
openApi.setServers(api);
openApi.setTags(api.getTags());
if (reldefinitionPath == null) {
openApi.setDefPath(definitionPath.replaceAll(Pattern.quote("\\"),
Matcher.quoteReplacement("\\\\")));
} else {
openApi.setDefPath(reldefinitionPath.replaceAll(Pattern.quote("\\"),
Matcher.quoteReplacement("\\\\")));
}
sourceFiles = generateBallerinaService(openApi);
break;
default:
return null;
}
return sourceFiles;
}
/**
* Write ballerina definition of a <code>object</code> to a file as described by <code>template.</code>
*
* @param object Context object to be used by the template parser
* @param templateDir Directory with all the templates required for generating the source file
* @param templateName Name of the parent template to be used
* @param outPath Destination path for writing the resulting source file
* @throws IOException when file operations fail
* @deprecated This method is now deprecated.
* Use {@link #generateBalSource(GeneratorConstants.GenType, String, String, String) generate}
* and implement a file write functionality your self, if you need to customize file writing steps.
* Otherwise use {@link #generate(GeneratorConstants.GenType, String, String, String, String, String) generate}
* to directly write generated source to a ballerina module.
*/
@Deprecated
public void writeBallerina(Object object, String templateDir, String templateName, String outPath)
throws IOException {
PrintWriter writer = null;
try {
Template template = compileTemplate(templateDir, templateName);
Context context = Context.newBuilder(object).resolver(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
FieldValueResolver.INSTANCE).build();
writer = new PrintWriter(outPath, "UTF-8");
writer.println(template.apply(context));
} finally {
if (writer != null) {
writer.close();
}
}
}
private Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException {
defaultTemplateDir = defaultTemplateDir.replaceAll("\\\\", "/");
String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
handlebars.setInfiniteLoops(true); //This will allow templates to call themselves with recursion.
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelper("equals", (object, options) -> {
CharSequence result;
Object param0 = options.param(0);
if (param0 == null) {
throw new IllegalArgumentException("found 'null', expected 'string'");
}
if (object != null) {
if (object.toString().equals(param0.toString())) {
result = options.fn(options.context);
} else {
result = options.inverse();
}
} else {
result = null;
}
return result;
});
return handlebars.compile(templateName);
}
private void writeGeneratedSources(List<GenSrcFile> sources, Path srcPath, Path implPath, GenType type)
throws IOException {
// Remove old generated files - if any - before regenerate
// if srcPackage was not provided and source was written to main package nothing will be deleted.
if (srcPackage != null && !srcPackage.isEmpty() && Files.exists(srcPath)) {
final File[] listFiles = new File(String.valueOf(srcPath)).listFiles();
if (listFiles != null) {
Arrays.stream(listFiles).forEach(file -> {
boolean deleteStatus = true;
if (!file.isDirectory() && !file.getName().equals(MODULE_MD)) {
deleteStatus = file.delete();
}
//Capture return value of file.delete() since if
//unable to delete returns false from file.delete() without an exception.
if (!deleteStatus) {
outStream.println("Unable to clean module directory.");
}
});
}
}
for (GenSrcFile file : sources) {
Path filePath;
// We only overwrite files of overwritable type.
// So non overwritable files will be written to disk only once.
if (!file.getType().isOverwritable()) {
filePath = implPath.resolve(file.getFileName());
if (Files.notExists(filePath)) {
CodegenUtils.writeFile(filePath, file.getContent());
}
} else {
filePath = srcPath.resolve(file.getFileName());
CodegenUtils.writeFile(filePath, file.getContent());
}
}
//This will print the generated files to the console
if (type.equals(GenType.GEN_SERVICE)) {
outStream.println("Service generated successfully and the OpenApi contract is copied to " + srcPackage
+ "/resources. this location will be referenced throughout the ballerina project.");
} else if (type.equals(GEN_CLIENT)) {
outStream.println("Client generated successfully.");
}
outStream.println("Following files were created. \n" +
"src/ \n- " + srcPackage);
Iterator<GenSrcFile> iterator = sources.iterator();
while (iterator.hasNext()) {
outStream.println("-- " + iterator.next().getFileName());
}
}
/**
* Generate code for ballerina client.
*
* @param context model context to be used by the templates
* @return generated source files as a list of {@link GenSrcFile}
* @throws IOException when code generation with specified templates fails
*/
private List<GenSrcFile> generateClient(BallerinaOpenApi context) throws IOException {
if (srcPackage == null || srcPackage.isEmpty()) {
srcPackage = GeneratorConstants.DEFAULT_CLIENT_PKG;
}
List<GenSrcFile> sourceFiles = new ArrayList<>();
String srcFile = context.getInfo().getTitle().toLowerCase(Locale.ENGLISH)
.replaceAll(" ", "_") + ".bal";
// Generate ballerina service and resources.
String mainContent = getContent(context, GeneratorConstants.DEFAULT_CLIENT_DIR,
GeneratorConstants.CLIENT_TEMPLATE_NAME);
sourceFiles.add(new GenSrcFile(GenFileType.GEN_SRC, srcPackage, srcFile, mainContent));
// Generate ballerina records to represent schemas.
String schemaContent = getContent(context, GeneratorConstants.DEFAULT_MODEL_DIR,
GeneratorConstants.SCHEMA_TEMPLATE_NAME);
sourceFiles.add(new GenSrcFile(GenFileType.MODEL_SRC, srcPackage, GeneratorConstants.SCHEMA_FILE_NAME,
schemaContent));
return sourceFiles;
}
private List<GenSrcFile> generateBallerinaService(BallerinaOpenApiType api) throws IOException {
if (srcPackage == null || srcPackage.isEmpty()) {
srcPackage = GeneratorConstants.DEFAULT_MOCK_PKG;
}
List<GenSrcFile> sourceFiles = new ArrayList<>();
String concatTitle = api.getBalServiceName().toLowerCase(Locale.ENGLISH).replaceAll(" ", "_");
String srcFile = concatTitle + ".bal";
String mainContent = getContent(api, GeneratorConstants.DEFAULT_TEMPLATE_DIR + "/service",
"balService");
sourceFiles.add(new GenSrcFile(GenFileType.GEN_SRC, srcPackage, srcFile, mainContent));
String schemaContent = getContent(api, GeneratorConstants.DEFAULT_TEMPLATE_DIR + "/service",
"schemaList");
sourceFiles.add(new GenSrcFile(GenFileType.GEN_SRC, srcPackage, GeneratorConstants.SCHEMA_FILE_NAME,
schemaContent));
return sourceFiles;
}
/**
* Retrieve generated source content as a String value.
*
* @param object context to be used by template engine
* @param templateDir templates directory
* @param templateName name of the template to be used for this code generation
* @return String with populated template
* @throws IOException when template population fails
*/
private String getContent(BallerinaOpenApiType object, String templateDir, String templateName) throws IOException {
Template template = compileTemplate(templateDir, templateName);
Context context = Context.newBuilder(object)
.resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
.build();
return template.apply(context);
}
/**
* Retrieve generated source content as a String value.
*
* @param object context to be used by template engine
* @param templateDir templates directory
* @param templateName name of the template to be used for this code generation
* @return String with populated template
* @throws IOException when template population fails
*/
private String getContent(BallerinaOpenApi object, String templateDir, String templateName) throws IOException {
Template template = compileTemplate(templateDir, templateName);
Context context = Context.newBuilder(object)
.resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
.build();
return template.apply(context);
}
public String getSrcPackage() {
return srcPackage;
}
public void setSrcPackage(String srcPackage) {
this.srcPackage = srcPackage;
}
public String getModelPackage() {
return modelPackage;
}
public void setModelPackage(String modelPackage) {
this.modelPackage = modelPackage;
}
}
|