| 1 | # Generating the OpenApi Spec (*.yaml) |
| 2 | |
| 3 | It is possible to generate an OpenApi spec in the form of a `*.yaml` file by invoking two simple Gradle tasks. |
| 4 | |
| 5 | ## Generate `*.yaml` files |
| 6 | |
| 7 | Every module (=subproject) that contains REST endpoints is scanned for Jakarta Annotations which are then used to |
| 8 | generate a `*.yaml` specification for that particular module. This means that there is one `*.yaml`file _per module_, |
| 9 | resulting in several `*.yaml` files. |
| 10 | |
| 11 | Those files are named `MODULENAME.yaml`, e.g. `observability.yaml` or `control.yaml`. |
| 12 | |
| 13 | To re-generate those files, simply invoke |
| 14 | |
| 15 | ```shell |
| 16 | ./gradlew clean resolve |
| 17 | ``` |
| 18 | |
| 19 | This will generate all `*.yaml` files in the `resources/openapi/yaml` directory. |
| 20 | |
| 21 | ## Gradle Plugins |
| 22 | |
| 23 | We use the official Swagger Gradle plugins: |
| 24 | |
| 25 | - `"io.swagger.core.v3.swagger-gradle-plugin"`: used to generate a `*.yaml` file per module |
| 26 | |
| 27 | So in order for a module to be picked up by the Swagger Gradle plugin, simply add it to the `build.gradle.kts`: |
| 28 | |
| 29 | ```kotlin |
| 30 | // in yourModule/build.gradle.kts |
| 31 | |
| 32 | val rsApi: String by project |
| 33 | |
| 34 | plugins { |
| 35 | `java-library` |
| 36 | id(libs.plugins.swagger.get().pluginId) //<-- add this |
| 37 | } |
| 38 | |
| 39 | dependencies { |
| 40 | implementation("jakarta.ws.rs:jakarta.ws.rs-api:${rsApi}") //<-- you'll probably already have this |
| 41 | // other dependencies |
| 42 | } |
| 43 | |
| 44 | ``` |
| 45 | |
| 46 | If you developed a REST endpoint, you very likely already have the `jakarta.ws.rs:....` part in your build file. If you |
| 47 | don't, it'll get added automatically for you. |
| 48 | |
| 49 | ### Categorizing your API |
| 50 | |
| 51 | All APIs in EDC should be "categorized", i.e. they should belong to a certain group of APIs. |
| 52 | Please see [this decision record](./decision-records/2022-11-09-api-refactoring/renaming.md) for reference. In order to |
| 53 | add your module to one of the categories, simply add this block to your module's `build.gradle.kts`: |
| 54 | |
| 55 | ```kotlin |
| 56 | plugins { |
| 57 | `java-library` |
| 58 | id(libs.plugins.swagger.get().pluginId) |
| 59 | } |
| 60 | |
| 61 | dependencies { |
| 62 | // ... |
| 63 | } |
| 64 | |
| 65 | // add this block: |
| 66 | edcBuild { |
| 67 | swagger { |
| 68 | apiGroup.set("management-api") |
| 69 | } |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | This tells the build plugin how to categorize your API and SwaggerHub will list it accordingly. |
| 74 | _Note: currently we have categories for `control-api` and `management-api`_ |
| 75 | |
| 76 | ## How to generate code |
| 77 | |
| 78 | This feature does **neither** expose the generated files through a REST endpoint providing any sort of live try-out |
| 79 | feature, **nor** does it generate any sort of client code. A visual documentation page for our APIs is served |
| 80 | through [SwaggerHub](https://app.swaggerhub.com/home?type=API). |
| 81 | |
| 82 | However, there is Gradle plugin capable of generating client code. |
| 83 | Please refer to the [official documentation](https://github.com/int128/gradle-swagger-generator-plugin). |