|
| 1 | +# Multiple API Specifications |
| 2 | + |
| 3 | +The OpenAPI plugin supports generating multiple independent specifications from a single application. |
| 4 | +This enables API versioning, separation of internal and external APIs, or organizing endpoints by product or team. |
| 5 | + |
| 6 | +Each specification has a unique identifier, its own configuration, and contains only the routes assigned to it. |
| 7 | +Specifications are generated and served independently. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Configuring Specifications |
| 13 | + |
| 14 | +Each specification can have its own configuration that extends or overrides the base plugin configuration. |
| 15 | + |
| 16 | +All configuration options available in the base plugin config are available for individual specifications: |
| 17 | + |
| 18 | +```kotlin |
| 19 | +install(OpenApi) { |
| 20 | + info { // (1)! |
| 21 | + title = "My API" |
| 22 | + } |
| 23 | + spec("v1") { // (2)! |
| 24 | + info { |
| 25 | + version = "1.0.0" |
| 26 | + } |
| 27 | + } |
| 28 | + spec("v2") { // (3)! |
| 29 | + info { |
| 30 | + version = "2.0.0" |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +1. Base configuration for all specifications |
| 37 | +2. Define and configure specification with identifier "v1" |
| 38 | +3. Define and configure specification with identifier "v2" |
| 39 | + |
| 40 | +Each specification is identified by a unique string (e.g., "v1", "v2", "internal"). |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Assigning Routes to Specifications |
| 46 | + |
| 47 | +Routes must be assigned to specifications to appear in them. Assignment can be done explicitly in route documentation or automatically via an assigner function. |
| 48 | + |
| 49 | + |
| 50 | +### Assignment at Route Documentation |
| 51 | + |
| 52 | +The `specName` property assigns a route to a specific specification: |
| 53 | + |
| 54 | +```kotlin |
| 55 | +get("users", { |
| 56 | + specName = "v1" |
| 57 | + description = "Get users (v1)" |
| 58 | +}) { } |
| 59 | +``` |
| 60 | + |
| 61 | +Routes can be easily assigned in groups by adding the `specName` at a parent route: |
| 62 | + |
| 63 | +```kotlin |
| 64 | +route("v1", { |
| 65 | + specName = "v1" // (1)! |
| 66 | +}) { |
| 67 | + get("users") { } |
| 68 | + get("products") { } |
| 69 | + get("orders") { } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +1. All child routes are assigned to "v1". |
| 74 | + |
| 75 | +All routes within this block are automatically assigned to the "v1" specification through inheritance. |
| 76 | + |
| 77 | + |
| 78 | +### Assignment via Assigner Function |
| 79 | + |
| 80 | +Routes can be automatically assigned based on their properties using an assigner function: |
| 81 | + |
| 82 | +```kotlin |
| 83 | +install(OpenApi) { |
| 84 | + specAssigner = { url, tags -> |
| 85 | + when (url.firstOrNull()) { |
| 86 | + "v1" -> "v1" |
| 87 | + "v2" -> "v2" |
| 88 | + "internal" -> "internal" |
| 89 | + else -> OpenApiPluginConfig.DEFAULT_SPEC_ID |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +The function receives: |
| 96 | + |
| 97 | +- `url`: URL path as a list of segments (e.g., `["api", "v1", "users"]` for `/api/v1/users`) |
| 98 | +- `tags`: Tags assigned to the route |
| 99 | + |
| 100 | +The function returns the specification identifier to assign the route to. |
| 101 | + |
| 102 | + |
| 103 | +### Unassigned Routes |
| 104 | + |
| 105 | +Routes without an assignment are assigned to the default specification with the identifier `OpenApiPluginConfig.DEFAULT_SPEC_ID`. |
| 106 | +The default specification only exists if any routes are assigned to it. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +## Serving Multiple Specifications |
| 112 | + |
| 113 | +Each specification is served independently via HTTP routes. |
| 114 | + |
| 115 | +```kotlin |
| 116 | +routing { |
| 117 | + route("v1/api.json") { // (1)! |
| 118 | + openApi("v1") |
| 119 | + } |
| 120 | + route("v2/api.json") { // (2)! |
| 121 | + openApi("v2") |
| 122 | + } |
| 123 | + route("internal/api.json") { // (3)! |
| 124 | + openApi("internal") |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +1. Expose v1 specification. |
| 130 | +2. Expose v2 specification. |
| 131 | +3. Expose internal specification. |
| 132 | + |
| 133 | +??? info "Multiple Specifications with Swagger UI and Redoc" |
| 134 | + |
| 135 | + More information on how to handle multiple specifications with documentation UIs: |
| 136 | + |
| 137 | + [:octicons-arrow-right-24: Swagger UI](./../../swaggerui/getting_started.md) |
| 138 | + |
| 139 | + [:octicons-arrow-right-24: ReDoc](./../../redoc/getting_started.md) |
0 commit comments