Skip to content

Commit 13b11d7

Browse files
kotlin-server: Fix required + nullable combination for properties (#20054)
* kotlin-server: Fix required + nullable combination * Update FILES * Updated generated code
1 parent 223a169 commit 13b11d7

File tree

17 files changed

+291
-1
lines changed

17 files changed

+291
-1
lines changed

.github/workflows/samples-kotlin-server-jdk17.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
- 'samples/server/petstore/kotlin-server/**'
88
- 'samples/server/petstore/kotlin-server-modelMutable/**'
99
- 'samples/server/petstore/kotlin-springboot-*/**'
10+
- 'samples/server/petstore/kotlin-server-required-and-nullable-properties/**'
1011
# comment out due to gradle build failure
1112
# - samples/server/petstore/kotlin-spring-default/**
1213
pull_request:
@@ -15,6 +16,7 @@ on:
1516
- 'samples/server/petstore/kotlin-server/**'
1617
- 'samples/server/petstore/kotlin-server-modelMutable/**'
1718
- 'samples/server/petstore/kotlin-springboot-*/**'
19+
- 'samples/server/petstore/kotlin-server-required-and-nullable-properties/**'
1820
# comment out due to gradle build failure
1921
# - samples/server/petstore/kotlin-spring-default/**
2022

@@ -30,6 +32,7 @@ jobs:
3032
matrix:
3133
sample:
3234
# server
35+
- samples/server/petstore/kotlin-server-required-and-nullable-properties
3336
- samples/server/petstore/kotlin-springboot-3
3437
- samples/server/petstore/kotlin-springboot-delegate-nodefaults
3538
- samples/server/petstore/kotlin-springboot-request-cookie

.github/workflows/samples-kotlin-server-jdk21.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ on:
44
push:
55
paths:
66
- 'samples/server/petstore/kotlin-server/**'
7+
- 'samples/server/petstore/kotlin-server-required-and-nullable-properties/**'
78
pull_request:
89
paths:
910
- 'samples/server/petstore/kotlin-server/**'
11+
- 'samples/server/petstore/kotlin-server-required-and-nullable-properties/**'
1012

1113
env:
1214
GRADLE_VERSION: '8.10'
@@ -21,6 +23,7 @@ jobs:
2123
sample:
2224
- samples/server/petstore/kotlin-server/javalin-6
2325
- samples/server/petstore/kotlin-server/ktor
26+
- samples/server/petstore/kotlin-server-required-and-nullable-properties
2427
steps:
2528
- uses: actions/checkout@v4
2629
- uses: actions/setup-java@v4
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
generatorName: kotlin-server
2+
outputDir: samples/server/petstore/kotlin-server-required-and-nullable-properties
3+
library: javalin6
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/required-and-nullable-properties.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-server
6+
additionalProperties:
7+
hideGenerationTimestamp: "true"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{#description}}
22
/* {{{.}}} */
33
{{/description}}
4-
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
4+
{{! Note that required properties may be nullable according to the OpenAPI specification. }}
5+
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{{classname}}}.{{{nameInPascalCase}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{^isNumber}}{{{defaultValue}}}{{/isNumber}}{{#isNumber}}{{{dataType}}}("{{{defaultValue}}}"){{/isNumber}}{{/defaultValue}}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# OpenAPI schemas support 4 possible combinations for properties:
2+
# 1. nullable + required
3+
# 2. nullable + not required
4+
# 3. not nullable + required
5+
# 4. not nullable + not required
6+
# This sample contains all of them to demonstrate how they are rendered in the generated code.
7+
# Related discussion: https://github.com/OpenAPITools/openapi-generator/issues/14765
8+
openapi: 3.0.3
9+
info:
10+
title: Nullable Required
11+
version: 1.0.0
12+
servers:
13+
- url: /api/v3
14+
paths:
15+
/pet:
16+
post:
17+
operationId: addPet
18+
requestBody:
19+
content:
20+
application/json:
21+
schema:
22+
$ref: '#/components/schemas/Pet'
23+
responses:
24+
"200":
25+
description: Successful operation
26+
content:
27+
application/json:
28+
schema:
29+
$ref: '#/components/schemas/Pet'
30+
components:
31+
schemas:
32+
Pet:
33+
required:
34+
- notNullable_required
35+
- nullable_required
36+
type: object
37+
properties:
38+
nullable_notRequired:
39+
type: string
40+
example: doggie
41+
nullable: true
42+
notNullable_notRequired:
43+
type: string
44+
example: doggie
45+
notNullable_required:
46+
type: string
47+
example: doggie
48+
nullable_required:
49+
type: string
50+
example: doggie
51+
nullable: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
README.md
2+
build.gradle.kts
3+
gradle.properties
4+
settings.gradle
5+
src/main/kotlin/org/openapitools/server/Main.kt
6+
src/main/kotlin/org/openapitools/server/apis/DefaultApi.kt
7+
src/main/kotlin/org/openapitools/server/apis/DefaultApiService.kt
8+
src/main/kotlin/org/openapitools/server/apis/DefaultApiServiceImpl.kt
9+
src/main/kotlin/org/openapitools/server/models/Pet.kt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.10.0-SNAPSHOT
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# org.openapitools.server - Kotlin Server library for Nullable Required
2+
3+
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
4+
5+
Generated by OpenAPI Generator 7.10.0-SNAPSHOT.
6+
7+
## Build
8+
9+
First, create the gradle wrapper script:
10+
11+
```
12+
gradle wrapper
13+
```
14+
15+
Then, run:
16+
17+
```
18+
./gradlew check assemble
19+
```
20+
21+
This runs all tests and packages the library.
22+
23+
## Running
24+
25+
The server builds as a fat jar with a main entrypoint. To start the service, run `java -jar ./build/libs/kotlin-server.jar`.
26+
27+
You may also run in docker:
28+
29+
```
30+
docker build -t kotlin-server .
31+
docker run -p 8080:8080 kotlin-server
32+
```
33+
34+
## Features/Implementation Notes
35+
36+
* Supports JSON inputs/outputs, File inputs, and Form inputs (see ktor documentation for more info).
37+
* ~Supports collection formats for query parameters: csv, tsv, ssv, pipes.~
38+
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.
39+
40+
<a id="documentation-for-api-endpoints"></a>
41+
## Documentation for API Endpoints
42+
43+
All URIs are relative to */api/v3*
44+
45+
Class | Method | HTTP request | Description
46+
------------ | ------------- | ------------- | -------------
47+
*DefaultApi* | [**addPet**](docs/DefaultApi.md#addpet) | **POST** /pet |
48+
49+
50+
<a id="documentation-for-models"></a>
51+
## Documentation for Models
52+
53+
- [org.openapitools.server.models.Pet](docs/Pet.md)
54+
55+
56+
<a id="documentation-for-authorization"></a>
57+
## Documentation for Authorization
58+
59+
Endpoints do not require authorization.
60+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.21"
3+
}
4+
5+
group = "org.openapitools"
6+
version = "1.0.0"
7+
8+
kotlin {
9+
jvmToolchain(21)
10+
}
11+
12+
java {
13+
toolchain {
14+
languageVersion.set(JavaLanguageVersion.of(21))
15+
}
16+
}
17+
18+
repositories {
19+
mavenCentral()
20+
}
21+
22+
dependencies {
23+
implementation("io.javalin:javalin:6.1.6")
24+
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
25+
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.1")
26+
implementation("org.slf4j:slf4j-simple:2.0.13")
27+
28+
testImplementation("org.jetbrains.kotlin:kotlin-test")
29+
}
30+
31+
tasks.test {
32+
useJUnitPlatform()
33+
}

0 commit comments

Comments
 (0)