feat(kotlin): Support for date and datetime from JSON schema by nikhilunni · Pull Request #2845 · glideapps/quicktype
Description
Add support for JSON Schema format: date-time, format: date, and format: time in the Kotlin code generator. These formats now generate proper java.time.* types instead of String.
This implementation follows the same pattern as Java's date-time support, using:
OffsetDateTimeforformat: "date-time"LocalDateforformat: "date"OffsetTimeforformat: "time"
The feature works across all Kotlin frameworks: just-types, jackson, klaxon, and kotlinx.
Related Issue
Fixes #2460
Motivation and Context
We prefer Kotlin generation to Java generation for nullability propagation, but did not support datetime formatting, as Java does. Plus, saw there was a longstanding issue for this.
Previous Behaviour / Output
For the given JSON schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"eventDate": {
"type": "string",
"format": "date"
},
"eventTime": {
"type": "string",
"format": "time"
},
"plannedDateTime": {
"type": "string",
"format": "date-time"
}
}
}It would output:
package quicktype data class MySchema ( val eventDate: String, val eventTime: String, val plannedDateTime: String )
New Behaviour / Output
package quicktype import java.time.OffsetDateTime import java.time.LocalDate import java.time.OffsetTime data class MySchema ( val eventDate: LocalDate, val eventTime: OffsetTime, val plannedDateTime: OffsetDateTime )
How Has This Been Tested?
Manual Testing
- Tested with the existing test/inputs/schema/date-time.schema file
- Verified output for all Kotlin frameworks:
--framework just-types--framework jackson--framework klaxon--framework kotlinx
- Tested with the exact schema from issue Kotlin date and datetime types are generated as strings from json schema #2460
- Verified proper handling of:
- Simple date-time properties
- Date-time types in unions
- Date-time types in arrays
- Complex nested structures with date-time types
Automated Testing
- Added "date-time" feature to KotlinLanguage test configuration
- Added "date-time" feature to KotlinJacksonLanguage test
configuration - Follows the same pattern as the Golang date-time feature
implementation (commit 1f89a97)