-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kotlin-server][javalin6] Add Javalin 6 support (#18928)
* [kotlin-server][javalin6] Add Javalin 6 support Javalin 5 support was added in 13edc5d. Javalin 6 has been released, with some breaking changes. Let's add a new supportedLibrary to not break existing users of Javalin 5. https://javalin.io/migration-guide-javalin-5-to-6 * Fix Gradle config and don't include JVM 8 CI anymore (JVM 11 is the minimum for Javalin) * Update docs * Fix optional query parameter handling and turn into expected type
- Loading branch information
1 parent
c806ea5
commit 793aba7
Showing
42 changed files
with
1,355 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Samples Kotlin server (jdk21) | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'samples/server/petstore/kotlin-server/javalin-6/**' | ||
pull_request: | ||
paths: | ||
- 'samples/server/petstore/kotlin-server/javalin-6/**' | ||
|
||
env: | ||
GRADLE_VERSION: 8.8 | ||
|
||
jobs: | ||
build: | ||
name: Build Kotlin server | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
sample: | ||
- samples/server/petstore/kotlin-server/javalin-6 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: 21 | ||
- name: Cache maven dependencies | ||
uses: actions/cache@v4 | ||
env: | ||
cache-name: maven-repository | ||
with: | ||
path: | | ||
~/.gradle | ||
key: ${{ runner.os }}-${{ github.job }}-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }} | ||
- name: Install Gradle wrapper | ||
uses: eskatos/gradle-command-action@v3 | ||
with: | ||
gradle-version: ${{ env.GRADLE_VERSION }} | ||
build-root-directory: ${{ matrix.sample }} | ||
arguments: wrapper | ||
- name: Build | ||
working-directory: ${{ matrix.sample }} | ||
run: ./gradlew build -x test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
generatorName: kotlin-server | ||
outputDir: samples/server/petstore/kotlin-server/javalin-6 | ||
library: javalin6 | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
additionalProperties: | ||
hideGenerationTimestamp: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...es/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/Main.kt.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package {{packageName}} | ||
|
||
import io.javalin.Javalin | ||
import io.javalin.apibuilder.ApiBuilder.* | ||
|
||
{{#apiInfo}} | ||
{{#apis}} | ||
{{#operations}}import {{apiPackage}}.{{classname}} | ||
import {{apiPackage}}.{{classname}}ServiceImpl | ||
{{/operations}} | ||
{{/apis}} | ||
|
||
fun main() { | ||
{{#apis}} | ||
{{#operations}} | ||
val {{#camelcase}}{{classname}}{{/camelcase}} = {{classname}}({{classname}}ServiceImpl()) | ||
{{/operations}} | ||
{{/apis}} | ||
|
||
val app = Javalin | ||
.create { config -> | ||
config.router.apiBuilder { | ||
{{#apis}} | ||
{{#operations}} | ||
{{#operation}} | ||
path("{{path}}") { {{#lowercase}}{{httpMethod}}{{/lowercase}}({{#camelcase}}{{classname}}{{/camelcase}}::{{operationId}}) } | ||
{{/operation}} | ||
{{/operations}} | ||
|
||
{{/apis}} | ||
} | ||
} | ||
|
||
app.start({{serverPort}}) | ||
} | ||
{{/apiInfo}} |
94 changes: 94 additions & 0 deletions
94
...les/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/README.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# {{packageName}} - Kotlin Server library for {{appName}} | ||
|
||
{{#unescapedAppDescription}} | ||
{{.}} | ||
{{/unescapedAppDescription}} | ||
|
||
Generated by OpenAPI Generator {{generatorVersion}}{{^hideGenerationTimestamp}} ({{generatedDate}}){{/hideGenerationTimestamp}}. | ||
|
||
## Build | ||
|
||
First, create the gradle wrapper script: | ||
|
||
``` | ||
gradle wrapper | ||
``` | ||
|
||
Then, run: | ||
|
||
``` | ||
./gradlew check assemble | ||
``` | ||
|
||
This runs all tests and packages the library. | ||
|
||
## Running | ||
|
||
The server builds as a fat jar with a main entrypoint. To start the service, run `java -jar ./build/libs/{{artifactId}}.jar`. | ||
|
||
You may also run in docker: | ||
|
||
``` | ||
docker build -t {{artifactId}} . | ||
docker run -p 8080:8080 {{artifactId}} | ||
``` | ||
|
||
## Features/Implementation Notes | ||
|
||
* Supports JSON inputs/outputs, File inputs, and Form inputs (see ktor documentation for more info). | ||
* ~Supports collection formats for query parameters: csv, tsv, ssv, pipes.~ | ||
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions. | ||
|
||
{{#generateApiDocs}} | ||
<a id="documentation-for-api-endpoints"></a> | ||
## Documentation for API Endpoints | ||
|
||
All URIs are relative to *{{{basePath}}}* | ||
|
||
Class | Method | HTTP request | Description | ||
------------ | ------------- | ------------- | ------------- | ||
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{{summary}}} | ||
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} | ||
{{/generateApiDocs}} | ||
|
||
{{#generateModelDocs}} | ||
<a id="documentation-for-models"></a> | ||
## Documentation for Models | ||
|
||
{{#modelPackage}} | ||
{{#models}}{{#model}} - [{{{modelPackage}}}.{{{classname}}}]({{modelDocPath}}{{{classname}}}.md) | ||
{{/model}}{{/models}} | ||
{{/modelPackage}} | ||
{{^modelPackage}} | ||
No model defined in this package | ||
{{/modelPackage}} | ||
{{/generateModelDocs}} | ||
|
||
<a id="documentation-for-authorization"></a> | ||
## Documentation for Authorization | ||
|
||
{{^authMethods}}Endpoints do not require authorization.{{/authMethods}} | ||
{{#hasAuthMethods}}Authentication schemes defined for the API:{{/hasAuthMethods}} | ||
{{#authMethods}} | ||
<a id="{{name}}"></a> | ||
### {{name}} | ||
|
||
{{#isApiKey}}- **Type**: API key | ||
- **API key parameter name**: {{keyParamName}} | ||
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}} | ||
{{/isApiKey}} | ||
{{#isBasicBasic}}- **Type**: HTTP basic authentication | ||
{{/isBasicBasic}} | ||
{{#isBasicBearer}}- **Type**: HTTP Bearer Token authentication{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} | ||
{{/isBasicBearer}} | ||
{{#isHttpSignature}}- **Type**: HTTP signature authentication | ||
{{/isHttpSignature}} | ||
{{#isOAuth}}- **Type**: OAuth | ||
- **Flow**: {{flow}} | ||
- **Authorization URL**: {{authorizationUrl}} | ||
- **Scopes**: {{^scopes}}N/A{{/scopes}} | ||
{{#scopes}} - {{scope}}: {{description}} | ||
{{/scopes}} | ||
{{/isOAuth}} | ||
|
||
{{/authMethods}} |
26 changes: 26 additions & 0 deletions
26
modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/api.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package {{apiPackage}} | ||
|
||
import io.javalin.http.Context | ||
import io.javalin.http.bodyAsClass | ||
import io.javalin.http.pathParamAsClass | ||
import io.javalin.http.queryParamAsClass | ||
|
||
{{#imports}}import {{import}} | ||
{{/imports}} | ||
|
||
{{#operations}} | ||
class {{classname}}(private val service: {{classname}}Service) { | ||
{{#operation}} | ||
/**{{#summary}} | ||
* {{.}}{{/summary}} | ||
* {{unescapedNotes}} | ||
{{#allParams}}* @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} | ||
{{/allParams}}*/ | ||
fun {{operationId}}(ctx: Context) { | ||
val result = service.{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}ctx) | ||
ctx.status({{#responses}}{{#-first}}{{code}}{{/-first}}{{/responses}}).json(result) | ||
} | ||
|
||
{{/operation}} | ||
} | ||
{{/operations}} |
1 change: 1 addition & 0 deletions
1
...openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/bodyParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isBodyParam}}{{#isArray}}ctx.bodyAsClass<List<{{{baseType}}}>>(){{/isArray}}{{^isArray}}ctx.bodyAsClass<{{{baseType}}}>(){{/isArray}}{{/isBodyParam}} |
33 changes: 33 additions & 0 deletions
33
...i-generator/src/main/resources/kotlin-server/libraries/javalin6/build.gradle.kts.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
plugins { | ||
kotlin("jvm") version "2.0.0" | ||
} | ||
|
||
group = "{{groupId}}" | ||
version = "{{artifactVersion}}" | ||
|
||
kotlin { | ||
jvmToolchain(21) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(21)) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("io.javalin:javalin:6.1.6") | ||
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1") | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.1") | ||
implementation("org.slf4j:slf4j-simple:2.0.13") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
1 change: 1 addition & 0 deletions
1
...openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/formParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isFormParam}}{{^isFile}}ctx.formParam("{{baseName}}"){{/isFile}}{{#isFile}}ctx.uploadedFile("{{baseName}}"){{/isFile}}{{/isFormParam}} |
1 change: 1 addition & 0 deletions
1
...s/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.gradle.caching=true |
1 change: 1 addition & 0 deletions
1
...enapi-generator/src/main/resources/kotlin-server/libraries/javalin6/headerParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isHeaderParam}}ctx.header("{{baseName}}"){{/isHeaderParam}} |
1 change: 1 addition & 0 deletions
1
...i-generator/src/main/resources/kotlin-server/libraries/javalin6/optionalDataType.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#required}}{{{dataType}}}{{/required}}{{^required}}{{#defaultValue}}{{{dataType}}}{{/defaultValue}}{{^defaultValue}}{{{dataType}}}?{{/defaultValue}}{{/required}} |
1 change: 1 addition & 0 deletions
1
...openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/pathParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isPathParam}}ctx.pathParamAsClass<{{{dataType}}}>("{{baseName}}").get(){{/isPathParam}} |
1 change: 1 addition & 0 deletions
1
...penapi-generator/src/main/resources/kotlin-server/libraries/javalin6/queryParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isQueryParam}}{{#isArray}}ctx.queryParams("{{baseName}}"){{/isArray}}{{^isArray}}ctx.queryParamAsClass<{{{dataType}}}>("{{baseName}}"){{^required}}.allowNullable(){{/required}}.get(){{/isArray}}{{/isQueryParam}} |
1 change: 1 addition & 0 deletions
1
...penapi-generator/src/main/resources/kotlin-server/libraries/javalin6/returnTypes.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isMap}}Map<String, {{{returnType}}}>{{/isMap}}{{#isArray}}{{#reactive}}Flow{{/reactive}}{{^reactive}}{{{returnContainer}}}{{/reactive}}<{{{returnType}}}>{{/isArray}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}} |
Oops, something went wrong.