Contents

Overview

The OpenAPI specification defines a standard way to express the interface exposed by a REST service.

The MicroProfile OpenAPI spec explains how MicroProfile embraces OpenAPI, adding annotations, configuration, and a service provider interface (SPI).

OpenAPI support in Helidon SE draws its inspiration from MicroProfile OpenAPI but does not implement the spec because Helidon SE does not support annotations.

The OpenAPI support in Helidon SE performs two main tasks:

  • Build an in-memory model of the REST API your service implements.

  • Expose the model in text format (typically YAML) via the /openapi endpoint.

To construct the model, Helidon gathers information about the service API from whichever of these sources are present in the application:

  • a model reader

    The SPI defines an interface you can implement in your application for programmatically providing part or all of the model;

  • a static OpenAPI document file packaged as part of your service;

  • a filter class

    The SPI defines an interface you can implement in your application which can mask parts of the model.

Maven Coordinates

To enable OpenAPI add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
    <groupId>io.helidon.openapi</groupId>
    <artifactId>helidon-openapi</artifactId>
</dependency>
Copied

Usage

You can very simply add support for OpenAPI to your Helidon SE application. This document shows what changes you need to make to your application and how to access the OpenAPI document for your application at runtime.

Changing your application

Register OpenAPISupport in your application routing

Helidon SE provides the OpenAPISupport class which your application uses to assemble the in-memory model and expose the /openapi endpoint to clients. You can create an instance either using a static create method or by instantiating its Builder. The example below illustrates one way to do this.

Furnish OpenAPI information about your endpoints

OpenAPI support in Helidon SE largely follows the MicroProfile OpenAPI spec. But because Helidon SE does not process annotations, your application supplies data for the OpenAPI model in the other ways listed earlier.

Provide a static OpenAPI file

Add a static file at META-INF/openapi.yml, META-INF/openapi.yaml, or META-INF/openapi.json. Tools such as Swagger let you describe your app’s API and they then generate an OpenAPI document file which you can include in your application so OpenAPI can use it.

Write and configure a model reader class

Write a Java class that implements the OpenAPI org.eclipse.microprofile.openapi.OASModelReader interface. Your model reader code programmatically adds elements to the internal model that OpenAPI builds.

Change your application’s MP configuration to set mp.openapi.model.reader as the fully-qualified class name of your class.

Write and configure a filter class

Write a Java class that implements the OpenAPI org.eclipse.microprofile.openapi.OASFilter interface. As OpenAPI composes its internal model, it invokes your filter with each model element before adding the element to the model. Your filter can accept the element as-is, modify it, or suppress it.

Change your application’s configuration to set mp.openapi.filter as the full-qualified class name of your class.

Add OpenAPI dependency

If you implement either a model reader or a filter, add this dependency to your pom.xml:

<dependency>
    <groupId>org.eclipse.microprofile.openapi</groupId>
    <artifactId>microprofile-openapi-api</artifactId>
    <version>{microprofile-openapi-version}</version>
</dependency>
Copied

Accessing the REST Endpoint

Once you add the SE OpenAPI dependency to your project and add code to create the OpenAPISupport object to your routing, your application automatically responds to the built-in endpoint — /openapi — and it returns the OpenAPI document describing the endpoints in your application.

By default, per the MicroProfile OpenAPI spec, the default format of the OpenAPI document is YAML. There is not yet an adopted IANA YAML media type, but a proposed one specifically for OpenAPI documents that has some support is application/vnd.oai.openapi. That is what Helidon returns, by default.

In addition, a client can specify the HTTP header Accept as either application/vnd.oai.openapi+json or application/json to request JSON. Alternatively, the client can pass the query parameter format as either JSON or YAML to receive application/json or application/vnd.oai.openapi (YAML) output, respectively.

API

The MicroProfile OpenAPI JavaDocs give full details of the classes and interfaces you can use in your code. Remember that, although the JavaDocs describe annotations, Helidon SE does not support them.

Helidon SE provides an API for creating and setting up the REST endpoint which serves OpenAPI documents to clients at the /openapi path. Use either static methods on OpenAPISupport or use its Builder to create an instance of OpenAPISupport. Then add that instance to your application’s routing. The example below shows how to do this.

Configuration

Helidon SE OpenAPI configuration supports the following settings:

Type: io.helidon.openapi.SEOpenAPISupport

Config key
openapi
Copied

Configuration options

Optional configuration options
keytypedefault valuedescription
application-path-disable

boolean

false

Sets whether the app path search should be disabled.

cors 

Assigns the CORS settings for the OpenAPI endpoint.

custom-schema-registry-class

string

 

Sets the custom schema registry class.

filter

string

 

Sets the developer-provided OpenAPI filter class name.

model.reader

string

 

Sets the developer-provided OpenAPI model reader class name.

schema.*

string

 

Sets the schema for the indicated fully-qualified class name (represented here by '*'); value is the schema in JSON format. Repeat for multiple classes.

servers

string[]

 

Sets servers.

servers.operation.*

string[]

 

Sets alternative servers to service the indicated operation (represented here by '*'). Repeat for multiple operations.

servers.path.*

string[]

 

Sets alternative servers to service all operations at the indicated path (represented here by '*'). Repeat for multiple paths.

static-file

string

META-INF/openapi.*

Sets the file system path of the static OpenAPI document file. Default types are json, yaml, and yml.

ui 

Assigns the OpenAPI U/I builder the OpenAPISupport service should use in preparing the U/I.

web-context

string

/openapi

Sets the web context path for the OpenAPI endpoint.

Examples

Helidon SE provides a complete OpenAPI example based on the SE QuickStart sample app which includes a model reader and a filter.

Most Helidon SE applications need only to create and register OpenAPISupport.

Register OpenAPISupport

Java Code to Register OpenAPISupport for Routing
Config config = Config.create();
return Routing.builder()
        .register(JsonSupport.create())
        .register(OpenAPISupport.create(config)) 
        .register(health)                   // Health at "/health"
        .register(metrics)                  // Metrics at "/metrics"
        .register("/greet", greetService)
        .build();
Copied
  • Adds the OpenAPISupport service to your server.

If you need more control over the OpenAPISupport instance, invoke OpenAPISupport.builder() to get an OpenAPISupport.Builder object and work with it.

Additional Information

Building the Jandex index

A Jandex index stores information about the classes and methods in your app and what annotations they have. It allows CDI to process annotations faster during your application’s start-up.

Add the Jandex maven plug-in to the <build><plugins> section of your pom.xml:

<plugin>
    <groupId>org.jboss.jandex</groupId>
    <artifactId>jandex-maven-plugin</artifactId>
    <version>{jandex-plugin-version}</version>
    <executions>
      <execution>
        <id>make-index</id>
        <goals>
          <goal>jandex</goal>
        </goals>
      </execution>
    </executions>
</plugin>
Copied

When you build your app maven should include the index META-INF/jandex.idx in the JAR.

Note

If you do not modify your build to create the index then the Helidon MP OpenAPI runtime automatically creates one in memory during app start-up. This slows down your app start-up and, depending on how CDI is configured, might inadvertently miss information.

We strongly recommend using the Jandex plug-in to build the index into your app.