Let’s say that you’re given the task of writing a microservice AND providing a documentation in OpenAPI format. You already know that there are two main approaches:
-
design-first : write the OpenAPI document (a.k.a. the
openapi.yaml
file) and then generate the code -
code-first : write the code, using OpenAPI annotations, and then generate the OpenAPI document
This repository contains different implementations of a simple service to demonstrate differences between the design-first and code-first approach.
Accompanying presentation at eclipsecon Europe 2019 : MicroProfile OpenAPI - code first or design first?, Talk on Youtube
The examples use Quarkus as server just because it not only supports MicroProfile OpenAPI, but comes also with an integrated UI. But the examples should run with very few code changes in any MicroProfile compatible server.
The example projects all work without the parent umbrella project, so you can easily copy just the example you’re interested in and start modifying it.
All example projects have integration tests (in OpenapiDocumentIT.java
) that check the result of
the /openapi
endpoint. Not only protects this from silent changes due to a changed implementation
when the project dependencies are updated (like it happened when I updated from Quarkus 0.21.1 to
0.23.2), but it allows also to easily compare the differences between the example projects:
-
every project contains a
src/test/resources/openapi-sorted.yaml
file with a little bit normalized OpenAPI document (sorting YAML files ist not trivial…) -
the design-first projects contain also the generated POJO files as
Xxx.java.txt
files (the txt suffix protects them from being recognized as Java files by the IDE and being reformatted), allowing to compare the result of openapi-generator and swagger-codegen.
In addition, all example projects have a rest-api.http
file which allows to run some example HTTP
request against a running service directly from within IntelliJ. And there is an _idea
directory
containing the IntelliJ project setup. But you can import the projects easily in any IDE with Maven
support.
Comparison between the examples using code-first and design-first approach
Solution | Pros | Cons |
---|---|---|
code-first |
|
|
design-first |
|
|