Generate the Java code to create an OpenAPI document
The Eclipse MicroProfile OpenAPI project provides the interfaces to manipulate OpenAPI documents. With the OASFactory
utility it is possible to create them with a fluent API.
Example:
package fr.jmini.tmp;
import static org.eclipse.microprofile.openapi.OASFactory.*;
import org.eclipse.microprofile.openapi.models.OpenAPI;
public final class CreateSpec {
public static OpenAPI create() {
return createOpenAPI()
.openapi("3.0.1")
.info(
createInfo()
.title("Ping Specification")
.version("1.0")
)
.addServer(
createServer()
.url("http://localhost:8000/")
)
.paths(
createPaths()
.addPathItem(
"/ping", createPathItem()
.GET(
createOperation()
.operationId("pingGet")
.responses(
createAPIResponses()
.addAPIResponse(
"200", createAPIResponse()
.description("OK")
)
)
)
)
);
}
}
Corresponds to this OpenAPI document (as YAML file):
openapi: 3.0.1
info:
title: Ping Specification
version: "1.0"
servers:
- url: http://localhost:8000/
paths:
/ping:
get:
operationId: pingGet
responses:
200:
description: OK
It can be a lot of work to create the Java code manually. The empoa project contains all the tools needed to automate this task. The code generator is using the JavaPoet library. Combined with Swagger-Parser, it is possible to to the conversation:
OpenAPI document
(as Yaml or Json file) => Java
code.
The script (gist) to generate the java code is quite simple (presented as a simple jbang script to have the required dependencies):
Usage:
jbang generateCode.java path-to/yaml/ping.yaml CreateSpec fr.jmini.tmp
You might need to format the produced code, to be sure the indentation looks good.
NOTE: If you are not a jbang user, it is easy to compile the presented snippet inside a Gradle or Maven project.