Skip to content

A plugin to generate Java source code from OpenApi definitions.

Notifications You must be signed in to change notification settings

jeka-dev/openapi-plugin

Repository files navigation

_dev.sample.Build Status Maven Central

Openapi generator for JeKa

A plugin to generate code from OpenAPI definitions.

This plugin is a lightweight wrapper for openapi-generator-cli and provides:

  • Automatic installation: Installs the OpenAPI generator for the specified version.
  • Easy access to common commands.
  • Automatic setup of OpenAPI generation as a project source generator.

Resources:

  • Command-line documentation: jeka openapi: --doc or jeka @dev.jeka:openapi-plugin:0.11.8-1 openapi: --doc.
  • Source code: Visit here.
  • OpenAPI: Visit here.
  • OpenAPI Generator: Visit here.
  • OpenAPI Generator documentation: Visit here.

Initialisation

The plugin scans properties named openapi.gen.xxx, creates a source generator for each, and registers it to the ProjectKBean.

Configuration

You can append openapi source generator to your working project, just declaring properties in your jeka.properties file, following the below example:

# Import this plugin into JeKa classpath
jeka.inject.classpath=dev.jeka:openapi-plugin:0.11.0-1

# Specify the version of openapi-generator-cli to set
@openapi=
@openapi.cliVersion=7.0.1

# Append a source generator, called 'myServer', to the project based on the following command line.
# The sources will be generated automatically prior compilation
# Any property formatted as openapi.gen.xxx will be taken in account
openapi.gen.myServer=generate -g spring \
  --model-name-prefix Rest \
  -i https://petstore.swagger.io/v2/swagger.json \
  --additional-properties=useBeanValidation=true,useSwaggerUI=false,interfaceOnly=true

# Append a second source generator
openapi.gen.myClient=generate -g client \
  --model-name-prefix Rest \
  -i https://my.spec.server/an-api.json

See project example here.

Programmatic Usage

Programmatic configuration may feel more natural to setup non-trivial structures.

String SPEC_URL = "https://petstore.swagger.io/v2/swagger.json";

// Create a source generator
JkOpenapiSourceGenerator sourceGenerator = JkOpenapiSourceGenerator.of("spring", SPEC_URL);
sourceGenerator.openapiCmd
        .addApiAndModelPackage("com.mycompany")
        .add(JkOpenapiCmdBuilder.MODEL_NAME_PREFIX, "Rest")
        .addAdditionalProperties("useSpringBoot3", "true")
        .add("--language-specific-primitives=Pet")
        .addImportMapping("Pet", "com.yourpackage.models.Pet")
        .addImportMapping("DateTime", "java.time.LocalDateTime")
        .addTypeMapping("DateTime", "java.time.LocalDateTime");

// Bind this generator to the project
JkProject project = myProject();
project.compilation.addSourceGenerator(project);

See project example here.


Contributors

The plugin code lies in jeka-src and so is built using the base KBean instead of project.

How to release ?

Just use the github release mechanism. Creating a release consists in creating a tag, that will trigger a build and a publication on Maven Central.

About

A plugin to generate Java source code from OpenApi definitions.

Resources

Stars

Watchers

Forks

Packages

No packages published