-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kotlin-server: Fix required + nullable combination
- Loading branch information
1 parent
8675874
commit 4f2a297
Showing
17 changed files
with
292 additions
and
1 deletion.
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
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-required-and-nullable-properties | ||
library: javalin6 | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/required-and-nullable-properties.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
additionalProperties: | ||
hideGenerationTimestamp: "true" |
3 changes: 2 additions & 1 deletion
3
modules/openapi-generator/src/main/resources/kotlin-server/data_class_req_var.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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{{#description}} | ||
/* {{{.}}} */ | ||
{{/description}} | ||
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}} | ||
{{! Note that required properties may be nullable according to the OpenAPI specification. }} | ||
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}} |
51 changes: 51 additions & 0 deletions
51
modules/openapi-generator/src/test/resources/3_0/required-and-nullable-properties.yaml
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,51 @@ | ||
# OpenAPI schemas support 4 possible combinations for properties: | ||
# 1. nullable + required | ||
# 2. nullable + not required | ||
# 3. not nullable + required | ||
# 4. not nullable + not required | ||
# This sample contains all of them to demonstrate how they are rendered in the generated code. | ||
# Related discussion: https://github.com/OpenAPITools/openapi-generator/issues/14765 | ||
openapi: 3.0.3 | ||
info: | ||
title: Nullable Required | ||
version: 1.0.0 | ||
servers: | ||
- url: /api/v3 | ||
paths: | ||
/pet: | ||
post: | ||
operationId: addPet | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Pet' | ||
responses: | ||
"200": | ||
description: Successful operation | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Pet' | ||
components: | ||
schemas: | ||
Pet: | ||
required: | ||
- notNullable_required | ||
- nullable_required | ||
type: object | ||
properties: | ||
nullable_notRequired: | ||
type: string | ||
example: doggie | ||
nullable: true | ||
notNullable_notRequired: | ||
type: string | ||
example: doggie | ||
notNullable_required: | ||
type: string | ||
example: doggie | ||
nullable_required: | ||
type: string | ||
example: doggie | ||
nullable: true |
23 changes: 23 additions & 0 deletions
23
.../server/petstore/kotlin-server-required-and-nullable-properties/.openapi-generator-ignore
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,23 @@ | ||
# OpenAPI Generator Ignore | ||
# Generated by openapi-generator https://github.com/openapitools/openapi-generator | ||
|
||
# Use this file to prevent files from being overwritten by the generator. | ||
# The patterns follow closely to .gitignore or .dockerignore. | ||
|
||
# As an example, the C# client generator defines ApiClient.cs. | ||
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: | ||
#ApiClient.cs | ||
|
||
# You can match any string of characters against a directory, file or extension with a single asterisk (*): | ||
#foo/*/qux | ||
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux | ||
|
||
# You can recursively match patterns against a directory, file or extension with a double asterisk (**): | ||
#foo/**/qux | ||
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux | ||
|
||
# You can also negate patterns with an exclamation (!). | ||
# For example, you can ignore all files in a docs folder with the file extension .md: | ||
#docs/*.md | ||
# Then explicitly reverse the ignore rule for a single file: | ||
#!docs/README.md |
10 changes: 10 additions & 0 deletions
10
...s/server/petstore/kotlin-server-required-and-nullable-properties/.openapi-generator/FILES
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,10 @@ | ||
.openapi-generator-ignore | ||
README.md | ||
build.gradle.kts | ||
gradle.properties | ||
settings.gradle | ||
src/main/kotlin/org/openapitools/server/Main.kt | ||
src/main/kotlin/org/openapitools/server/apis/DefaultApi.kt | ||
src/main/kotlin/org/openapitools/server/apis/DefaultApiService.kt | ||
src/main/kotlin/org/openapitools/server/apis/DefaultApiServiceImpl.kt | ||
src/main/kotlin/org/openapitools/server/models/Pet.kt |
1 change: 1 addition & 0 deletions
1
...server/petstore/kotlin-server-required-and-nullable-properties/.openapi-generator/VERSION
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.10.0-SNAPSHOT |
60 changes: 60 additions & 0 deletions
60
samples/server/petstore/kotlin-server-required-and-nullable-properties/README.md
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,60 @@ | ||
# org.openapitools.server - Kotlin Server library for Nullable Required | ||
|
||
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
|
||
Generated by OpenAPI Generator 7.10.0-SNAPSHOT. | ||
|
||
## 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/kotlin-server.jar`. | ||
|
||
You may also run in docker: | ||
|
||
``` | ||
docker build -t kotlin-server . | ||
docker run -p 8080:8080 kotlin-server | ||
``` | ||
|
||
## 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. | ||
|
||
<a id="documentation-for-api-endpoints"></a> | ||
## Documentation for API Endpoints | ||
|
||
All URIs are relative to */api/v3* | ||
|
||
Class | Method | HTTP request | Description | ||
------------ | ------------- | ------------- | ------------- | ||
*DefaultApi* | [**addPet**](docs/DefaultApi.md#addpet) | **POST** /pet | | ||
|
||
|
||
<a id="documentation-for-models"></a> | ||
## Documentation for Models | ||
|
||
- [org.openapitools.server.models.Pet](docs/Pet.md) | ||
|
||
|
||
<a id="documentation-for-authorization"></a> | ||
## Documentation for Authorization | ||
|
||
Endpoints do not require authorization. | ||
|
33 changes: 33 additions & 0 deletions
33
samples/server/petstore/kotlin-server-required-and-nullable-properties/build.gradle.kts
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 = "org.openapitools" | ||
version = "1.0.0" | ||
|
||
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
samples/server/petstore/kotlin-server-required-and-nullable-properties/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
samples/server/petstore/kotlin-server-required-and-nullable-properties/settings.gradle
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 @@ | ||
rootProject.name = 'kotlin-server' |
21 changes: 21 additions & 0 deletions
21
...n-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/Main.kt
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,21 @@ | ||
package org.openapitools.server | ||
|
||
import io.javalin.Javalin | ||
import io.javalin.apibuilder.ApiBuilder.* | ||
|
||
import org.openapitools.server.apis.DefaultApi | ||
import org.openapitools.server.apis.DefaultApiServiceImpl | ||
|
||
fun main() { | ||
val defaultApi = DefaultApi(DefaultApiServiceImpl()) | ||
|
||
val app = Javalin | ||
.create { config -> | ||
config.router.apiBuilder { | ||
path("/pet") { post(defaultApi::addPet) } | ||
|
||
} | ||
} | ||
|
||
app.start() | ||
} |
20 changes: 20 additions & 0 deletions
20
...quired-and-nullable-properties/src/main/kotlin/org/openapitools/server/apis/DefaultApi.kt
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,20 @@ | ||
package org.openapitools.server.apis | ||
|
||
import io.javalin.http.Context | ||
import io.javalin.http.bodyAsClass | ||
import io.javalin.http.pathParamAsClass | ||
import io.javalin.http.queryParamAsClass | ||
|
||
import org.openapitools.server.models.Pet | ||
|
||
class DefaultApi(private val service: DefaultApiService) { | ||
/** | ||
* | ||
* @param pet (optional) | ||
*/ | ||
fun addPet(ctx: Context) { | ||
val result = service.addPet(ctx.bodyAsClass<Pet>(), ctx) | ||
ctx.status(200).json(result) | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...and-nullable-properties/src/main/kotlin/org/openapitools/server/apis/DefaultApiService.kt
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,17 @@ | ||
package org.openapitools.server.apis | ||
|
||
import org.openapitools.server.models.Pet | ||
import io.javalin.http.Context | ||
|
||
interface DefaultApiService { | ||
|
||
/** | ||
* POST /pet | ||
* | ||
* @param pet (optional) | ||
* @param ctx The Javalin context. Especially handy if you need to access things like authentication headers in your service. (required) | ||
* @return Successful operation (status code 200) | ||
* @see DefaultApi#addPet | ||
*/ | ||
fun addPet(pet: Pet?, ctx: Context): Pet | ||
} |
11 changes: 11 additions & 0 deletions
11
...nullable-properties/src/main/kotlin/org/openapitools/server/apis/DefaultApiServiceImpl.kt
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,11 @@ | ||
package org.openapitools.server.apis | ||
|
||
import org.openapitools.server.models.Pet | ||
import io.javalin.http.Context | ||
|
||
class DefaultApiServiceImpl : DefaultApiService { | ||
|
||
override fun addPet(pet: Pet?, ctx: Context): Pet { | ||
TODO("Implement me") | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...er-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/models/Pet.kt
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,28 @@ | ||
/** | ||
* Nullable Required | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
package org.openapitools.server.models | ||
|
||
|
||
/** | ||
* | ||
* @param notNullableRequired | ||
* @param nullableRequired | ||
* @param nullableNotRequired | ||
* @param notNullableNotRequired | ||
*/ | ||
data class Pet( | ||
val notNullableRequired: kotlin.String, | ||
val nullableRequired: kotlin.String?, | ||
val nullableNotRequired: kotlin.String? = null, | ||
val notNullableNotRequired: kotlin.String? = null | ||
) | ||
|