|
| 1 | +package io.github.venkat1701; |
| 2 | + |
| 3 | +import com.squareup.javapoet.JavaFile; |
| 4 | +import com.squareup.javapoet.MethodSpec; |
| 5 | +import com.squareup.javapoet.TypeSpec; |
| 6 | +import io.swagger.v3.oas.models.OpenAPI; |
| 7 | +import io.swagger.v3.oas.models.Operation; |
| 8 | +import io.swagger.v3.oas.models.PathItem; |
| 9 | +import io.swagger.v3.parser.OpenAPIV3Parser; |
| 10 | + |
| 11 | +import javax.lang.model.element.Modifier; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +public class ApiWrapperGenerator { |
| 17 | + public static void generateApiWrapper(OpenAPI openAPI, String outputDir) throws IOException { |
| 18 | + TypeSpec.Builder apiWrapperClassBuilder = TypeSpec.classBuilder("ApiWrapperGenerator") |
| 19 | + .addModifiers(Modifier.PUBLIC); |
| 20 | + |
| 21 | + openAPI.getPaths().forEach((path, pathItem) -> { |
| 22 | + Map<PathItem.HttpMethod, Operation> operations = pathItem.readOperationsMap(); |
| 23 | + operations.forEach((httpMethod, operation) -> { |
| 24 | + String methodName = operation.getOperationId(); |
| 25 | + if (methodName == null || methodName.isEmpty()) { |
| 26 | + methodName = httpMethod.name().toLowerCase() + "_" + path; |
| 27 | + } |
| 28 | + MethodSpec methodSpec = MethodSpec.methodBuilder(sanitizeMethodName(sanitizePath(methodName))) |
| 29 | + .addModifiers(Modifier.PUBLIC) |
| 30 | + .returns(void.class) |
| 31 | + .addParameter(String.class, "request") |
| 32 | + .addStatement("$T.out.println($S)", System.class, |
| 33 | + "Executing " + httpMethod.name() + " on " + path) |
| 34 | + .build(); |
| 35 | + |
| 36 | + apiWrapperClassBuilder.addMethod(methodSpec); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + TypeSpec apiWrapperClass = apiWrapperClassBuilder.build(); |
| 41 | + JavaFile javaFile = JavaFile.builder("io.demotesting.api", apiWrapperClass) |
| 42 | + .build(); |
| 43 | + javaFile.writeTo(Paths.get(outputDir)); |
| 44 | + } |
| 45 | + |
| 46 | + private static String sanitizePath(String path) { |
| 47 | + String[] sanitized = path.replaceAll("^/+", "").replaceAll("[^a-zA-Z0-9]", "_").split("_+"); |
| 48 | + String methodName = sanitized[0]; |
| 49 | + for(int i=1; i<sanitized.length; i++) { |
| 50 | + methodName += Character.toUpperCase(sanitized[i].charAt(0))+sanitized[i].substring(1); |
| 51 | + } |
| 52 | + return methodName; |
| 53 | + } |
| 54 | + |
| 55 | + private static String sanitizeMethodName(String methodName) { |
| 56 | + String[] methodNameArray = methodName.split("_"); |
| 57 | + String sanitizedMethodName = methodNameArray[0]; |
| 58 | + for(int i=1; i<methodNameArray.length; i++) { |
| 59 | + sanitizedMethodName += Character.toUpperCase(methodNameArray[i].charAt(0))+methodNameArray[i].substring(1); |
| 60 | + } |
| 61 | + return sanitizedMethodName; |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) { |
| 65 | + try { |
| 66 | + String specPath = "src/main/resources/openapi.yaml"; |
| 67 | + OpenAPI openAPI = new OpenAPIV3Parser().read(specPath); |
| 68 | + if (openAPI == null) { |
| 69 | + System.err.println("Failed to parse the OpenAPI specification."); |
| 70 | + return; |
| 71 | + } |
| 72 | + generateApiWrapper(openAPI, "./generated-sources"); |
| 73 | + System.out.println("API wrapper generated successfully."); |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments