Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions create-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation(libs.serialization)
implementation(libs.serialization.json)
implementation(libs.bundles.ktor)
implementation(libs.kotlinx.datetime)

testImplementation(projects.common)
testImplementation(libs.classGraph)
Expand All @@ -29,6 +30,7 @@ dependencies {
testImplementation(libs.assertJ)
testImplementation(platform(libs.junit))
testImplementation(libs.junitJupiter)
testImplementation(libs.kotlinx.datetime)
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ internal class ExpressionsVisitorK2(
OpenApiSpec.SchemaType(
type = kotlinType.toString().toSwaggerType()
)
} else if (kotlinType?.lookupTagIfAny?.name?.asString() == "Instant" ||
Copy link
Owner

@tabilzad tabilzad Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comparing simple names, we should compare fully qualified package path with the name. So let's define the corresponding fq names in ClassIds.kt similar to others. Then we could just compare kotlinType.classId

Copy link
Owner

@tabilzad tabilzad Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also we're adding this explicit check here for when the whole response body in just an instant or date but the same case for request body is not handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I can add a test that validates request body works the same as well, good call.

kotlinType?.lookupTagIfAny?.name?.asString() == "LocalDateTime") {
OpenApiSpec.SchemaType(
type = "string",
format = "date-time"
)
} else {
val typeRef = response.type?.generateTypeAndVisitMemberDescriptors()
OpenApiSpec.SchemaType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ private fun ConeKotlinType.isBuiltinType(classId: ClassId, isNullable: Boolean?)
return lookupTag.classId == classId && (isNullable == null || isNullableAny == isNullable)
}

@Suppress("CyclomaticComplexMethod")
fun isDatetime(fqClassName: String): Boolean {
return fqClassName == "kotlinx.datetime.Instant" ||
fqClassName == "kotlinx.datetime.LocalDateTime" ||
fqClassName == "java.time.Instant"
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all of the above, re: use classId's

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you could probably re-use this method in the resolveToOpenSpecFormat for consistency.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also java.time.LocalDateTime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should actually be seperate mappings, but im not sure what the specs map to.. anyway LocalDate is a date object, and Instant is a datetime object, which are both strings with different string formats per the spec: https://swagger.io/docs/specification/v3_0/data-models/data-types/#strings

fun FirRegularClassSymbol.resolveEnumEntries(): List<String> {
return declarationSymbols.filterIsInstance<FirEnumEntrySymbol>().map { it.name.asString() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ internal class ClassDescriptorVisitorK2(
ObjectType(type = "string", enum = enumValues)
}

fqClassName?.let { isDatetime(it) } == true -> {
ObjectType(type = "string", format = "date-time")
}

typeSymbol?.isSealed == true -> {

if (!classNames.names.contains(fqClassName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package io.github.tabilzad.ktor.output

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.sun.security.ntlm.Server
import io.github.tabilzad.ktor.ContentType
import io.github.tabilzad.ktor.OpenApiSpecParam
import io.github.tabilzad.ktor.model.Info
import io.github.tabilzad.ktor.model.SecurityScheme
import java.nio.file.Path

internal typealias ContentSchema = Map<String, OpenApiSpec.SchemaType>

Expand Down Expand Up @@ -78,6 +76,7 @@ data class OpenApiSpec(

data class SchemaType(
val type: String? = null,
val format: String? = null,
val items: SchemaRef? = null,
@Suppress("ConstructorParameterNaming")
val `$ref`: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,42 @@ class K2StabilityTest {
result.assertWith(expected)
}

@Test
fun `should correctly resolve kotlinx-datetime using instant class into date-time response annotations`() {
val (source, expected) = loadSourceAndExpected("ResponseBodyKotlinxDatetimeInstant")
generateCompilerTest(
testFile,
source,
PluginConfiguration.createDefault(hideTransients = false, hidePrivateFields = false)
)
val result = testFile.readText()
result.assertWith(expected)
}

@Test
fun `should correctly resolve kotlinx-datetime using instant class into date-time response annotations, simple response body`() {
val (source, expected) = loadSourceAndExpected("ResponseBodyKotlinxDatetimeInstant2")
generateCompilerTest(
testFile,
source,
PluginConfiguration.createDefault(hideTransients = false, hidePrivateFields = false)
)
val result = testFile.readText()
result.assertWith(expected)
}

@Test
fun `should correctly resolve kotlinx-datetime using localdatetime class into date-time response annotations`() {
val (source, expected) = loadSourceAndExpected("ResponseBodyKotlinxDatetimeLocalDateTime")
generateCompilerTest(
testFile,
source,
PluginConfiguration.createDefault(hideTransients = false, hidePrivateFields = false)
)
val result = testFile.readText()
result.assertWith(expected)
}

@Test
fun `should handle abstract or sealed schema definitions`() {
val (source, expected) = loadSourceAndExpected("Abstractions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,6 @@ private val deps = arrayOf(
"kotlinx-coroutines-core:1.10.1",
"moshi:1.14.0",
"kotlinx-serialization-core:2.1.0",
"kotlinx-datetime:0.4.0",
"annotations:0.6.7-alpha"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"openapi" : "3.1.0",
"info" : {
"title" : "Open API Specification",
"description" : "test",
"version" : "1.0.0"
},
"paths" : {
"/temp" : {
"get" : {
"responses" : {
"200" : {
"description" : "Success",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/sources.Response"
}
}
}
}
}
}
}
},
"components" : {
"schemas" : {
"sources.Response" : {
"type" : "object",
"properties" : {
"message" : {
"type" : "string",
"format" : "date-time"
}
},
"required" : [ "message" ]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"openapi" : "3.1.0",
"info" : {
"title" : "Open API Specification",
"description" : "test",
"version" : "1.0.0"
},
"paths" : {
"/temp" : {
"get" : {
"responses" : {
"200" : {
"description" : "Success",
"content" : {
"application/json" : {
"schema" : {
"type" : "string",
"format" : "date-time"
}
}
}
}
}
}
}
},
"components" : {
"schemas" : { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"openapi" : "3.1.0",
"info" : {
"title" : "Open API Specification",
"description" : "test",
"version" : "1.0.0"
},
"paths" : {
"/temp" : {
"get" : {
"responses" : {
"200" : {
"description" : "Success",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/sources.Response"
}
}
}
}
}
}
}
},
"components" : {
"schemas" : {
"sources.Response" : {
"type" : "object",
"properties" : {
"message" : {
"type" : "string",
"format" : "date-time"
}
},
"required" : [ "message" ]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sources

import io.github.tabilzad.ktor.annotations.GenerateOpenApi
import io.github.tabilzad.ktor.annotations.KtorResponds
import io.github.tabilzad.ktor.annotations.ResponseEntry
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable


@Serializable
data class Response(
val message: Instant = Clock.System.now()
)

@GenerateOpenApi
fun Application.responseBodySample() {
routing {
@KtorResponds(
mapping = [
ResponseEntry("200", Response::class, description = "Success"),
]
)
get("temp") {
call.respond(Response())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sources

import io.github.tabilzad.ktor.annotations.GenerateOpenApi
import io.github.tabilzad.ktor.annotations.KtorResponds
import io.github.tabilzad.ktor.annotations.ResponseEntry
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable


@GenerateOpenApi
fun Application.responseBodySample() {
routing {
@KtorResponds(
mapping = [
ResponseEntry("200", kotlinx.datetime.Instant::class, description = "Success"),
]
)
get("temp") {
call.respond(Clock.System.now())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sources

import io.github.tabilzad.ktor.annotations.GenerateOpenApi
import io.github.tabilzad.ktor.annotations.KtorResponds
import io.github.tabilzad.ktor.annotations.ResponseEntry
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toKotlinLocalDateTime
import kotlinx.serialization.Serializable


@Serializable
data class Response(
val message: LocalDateTime = java.time.LocalDateTime.now().toKotlinLocalDateTime()
)

@GenerateOpenApi
fun Application.responseBodySample() {
routing {
@KtorResponds(
mapping = [
ResponseEntry("200", Response::class, description = "Success"),
]
)
get("temp") {
call.respond(Response())
}
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kotlinReflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref =
moshi = { module = "com.squareup.moshi:moshi", version = "1.14.0" }
serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerialization" }
serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerialization" }

kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.4.0" }
1 change: 1 addition & 0 deletions ktor-docs-plugin-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
shadow(projects.common)
implementation(libs.serialization.json)
implementation(libs.serialization)
implementation(libs.kotlinx.datetime)
}

gradlePlugin {
Expand Down