Skip to content

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

@slanos

Description

@slanos

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"
        ]
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions