Last active
May 20, 2020 04:09
-
-
Save jmini/55fe3e1f337a2e1e45856e5dbbeb9328 to your computer and use it in GitHub Desktop.
Generate the Java code to create an OpenAPI document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS org.openapitools.empoa:empoa-swagger-core:1.1.0 | |
//DEPS org.openapitools.empoa:empoa-javapoet:1.1.0 | |
//DEPS io.swagger.parser.v3:swagger-parser:2.0.20 | |
//DEPS org.slf4j:slf4j-simple:1.7.30 | |
import org.eclipse.microprofile.openapi.models.OpenAPI; | |
import org.openapitools.empoa.javapoet.JavaFileConverter; | |
import org.openapitools.empoa.swagger.core.internal.SwAdapter; | |
import com.squareup.javapoet.JavaFile; | |
import io.swagger.parser.OpenAPIParser; | |
import io.swagger.v3.parser.core.models.ParseOptions; | |
import io.swagger.v3.parser.core.models.SwaggerParseResult; | |
public class generateCode { | |
public static void main(String... args) { | |
if(args.length == 0) { | |
System.err.println("Expecting arguments:"); | |
System.err.println(" - Path to the OpenAPI specification Json or Yaml file (mandatory)"); | |
System.err.println(" - Class name (if omitted \"SpecMain\" is used)"); | |
System.err.println(" - Package name (if omitted \"tmp\" is used)"); | |
System.exit(1); | |
} else { | |
String packageName = (args.length > 1) ? args[2] : "tmp"; | |
String className = (args.length > 0) ? args[1] : "Spec"; | |
String specLocation = args[0]; | |
System.out.println("// Generated based on spec: " + specLocation); | |
final OpenAPIParser openApiParser = new OpenAPIParser(); | |
final ParseOptions options = new ParseOptions(); | |
final SwaggerParseResult parserResult = openApiParser.readLocation(specLocation, null, options); | |
OpenAPI openAPI = SwAdapter.toOpenAPI(parserResult.getOpenAPI()); | |
JavaFile javaFile = JavaFileConverter.createOpenAPI(openAPI, packageName, className); | |
System.out.println(javaFile.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See blog post: Generate the Java code to create an OpenAPI document