openapi-processor-spring 2021.4 & micronaut 2021.2
Tags: openapi, java, springboot, micronaut
Published: 2021-05-31 21:44:00


# what is openapi-processor?

[openapi-processor][oap] is a small framework to process OpenAPI yaml files. Currently, openapi-processor provides java code generation for Spring Boot, Micronaut and conversion to json.

It does support [gradle][oap-gradle] and [maven][oap-maven] to run any openapi-processor as part of the build process.

See the [documentation][oap-docs] for more. There is also a [playground][oap-play] to preview the processors.

# what is new?

See the [release notes][oap-spring-release] for the full details (it is the same for spring & micronaut).

Apart from a couple of bug fixes and improvements for multipart requests and bean-validations, openapi-processor is now able to automatically add a suffix to the generated model classes.

This is enabled by configuring the `model-name-suffix` in the `mapping.yaml`:

```yaml
openapi-processor-mapping: v2

options:
  package-name: io.openapiprocessor.sample
  model-name-suffix: Resource # or Dto or ...
```

The `model-name-suffix` option sets a suffix that is automatically appended to all generated model and enum classes.

The suffix helps to

- avoid duplicate class names in generated code and normal code

- makes it easier to recognize which role or in which context a class is used. Is it a data transfer class or is it a domain class?

- keeps the suffix *"noise"* out of the OpenAPI description

Usually you will separate the classes by putting them in different packages. This helps to distinguish the classes, but when both are used in the same code, i.e. when converting one format to the other, it is a lot easier to distinguish them by their class name instead of their package name.

If a schema name from the OpenAPI description already ends with the `model-name-suffix`, the processor will not append the suffix. This allows to migrate an existing api with a suffix in the API to `model-name-suffix` step by step.
Example:

OpenAPI
```yaml
paths:
  /foo:
    get:
      responses:
        '200':
          description: the foo result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo' # (1)

components:
  schemas:

    Foo:
      type: object
      properties:
        nested:
          $ref: '#/components/schemas/BarResource' # (1)

    BarResource:
      type: object
      properties:
        prop:
          type: string
```

mapping.yaml

```yaml
openapi-processor-mapping: v2

options:
  package-name: io.openapiprocessor.sample
  model-name-suffix: Resource # (2)
```

Java

```java
// interface
public interface Api {

    @Mapping("/foo")
    FooResource getFoo(); // (3)

}

// pojos
public class FooResource { // (3)

    // ...

    @JsonProperty("nested")
    private BarResource nested;

    // ...
}

public class BarResource { // (4)

    // ...
}
```

1. a schema name without suffix
2. the suffix configuration
3. the class name of the Foo schema got the configured Resource suffix
4. the class name of the `BarResource` is identical to the original schema name. Since the existing suffix is equal to `model-name-suffix` it is ignored. Otherwise, This prevents funny class names like `BarResourceResource`.

That’s it.


[blog]: https://hauner.github.io/blog/2020/openapi-processor-mapping
[oap-maven]: https://docs.openapiprocessor.io/maven
[oap-gradle]: https://docs.openapiprocessor.io/gradle
[oap-spring]: https://docs.openapiprocessor.io/spring
[oap]: https://docs.openapiprocessor.io/oap/home/home.html
[oap-docs]: https://docs.openapiprocessor.io
[oap-play]: https://playground.openapiprocessor.io
[oap-samples]: https://docs.openapiprocessor.io/samples
[oap-spring-release]: https://github.com/openapi-processor/openapi-processor-spring/releases/tag/v2021.4
[oap-micronaut-release]: https://github.com/openapi-processor/openapi-processor-micronaut/releases/tag/v2021.2