Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce kotlinx-datetime support, which maps to a string with format: date-time #46

Open
schott12521 opened this issue Feb 6, 2025 · 1 comment

Comments

@schott12521
Copy link
Contributor

Ktor-docs doesn't support kotlinx-datetime, instead opting to create a scheme for the LocalDateTime object:

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())
        }
    }
}

Will generate:

{
    "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" : {
        "java.time.Instant" : {
          "type" : "object"
        },
        "kotlinx.datetime.Instant" : {
          "type" : "object",
          "properties" : {
            "epochSeconds" : {
              "type" : "integer"
            },
            "nanosecondsOfSecond" : {
              "type" : "integer"
            },
            "value" : {
              "$ref" : "#/components/schemas/java.time.Instant"
            }
          },
          "required" : [ "epochSeconds", "nanosecondsOfSecond", "value" ]
        },
        "sources.Response" : {
          "type" : "object",
          "properties" : {
            "message" : {
              "$ref" : "#/components/schemas/kotlinx.datetime.Instant"
            }
          },
          "required" : [ "message" ]
        }
      }
    }
  }

This is only after importing the kotlinx-datetime library and adding it as a dependency + test dependency in TestUtils

I think instead we should just map this to type: String with format: date-time -- which is expected by the openapi spec. This will allow openApiGenerator's to create a generated-client specific datetime handler, as kotlinx-datetime should be serializable completely to a date-time.

Instead I think the generated spec should look like:

{
  "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"
        ]
      }
    }
  }
}
@tabilzad
Copy link
Owner

tabilzad commented Feb 9, 2025

We could probably handle dates the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants