Skip to content

feat: Add Kotlin type generator#1050

Open
thagikura wants to merge 1 commit intosupabase:masterfrom
thagikura:kotlin-typegen
Open

feat: Add Kotlin type generator#1050
thagikura wants to merge 1 commit intosupabase:masterfrom
thagikura:kotlin-typegen

Conversation

@thagikura
Copy link
Copy Markdown

What kind of change does this PR introduce?

Feature

What is the current behavior?

Type generators exist for TypeScript, Swift, Go, and Python, but not for Kotlin. Kotlin developers
using the supabase-kt SDK must manually write
@Serializable data classes to match their database schema.

What is the new behavior?

Adds a Kotlin type generator at GET /generators/kotlin that produces @Serializable data classes
compatible with kotlinx.serialization. Follows the
same pattern as the Swift generator (#779).

For each table, three data classes are generated (Select, Insert, Update). Views get Select
only. PostgreSQL enums become @Serializable enum class with @SerialName values.

Example output:

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
enum class UserStatus {
    @SerialName("ACTIVE") ACTIVE,
    @SerialName("INACTIVE") INACTIVE
}

@Serializable
data class UsersSelect(
    val id: Long,
    val name: String? = null,
    val status: UserStatus? = null
)

@Serializable
data class UsersInsert(
    val id: Long? = null,
    val name: String? = null,
    val status: UserStatus? = null
)

@Serializable
data class UsersUpdate(
    val id: Long? = null,
    val name: String? = null,
    val status: UserStatus? = null
)

Additional context

- @SerialName annotations are only added when the camelCase property name differs from the column name
- Kotlin reserved words (in, when, val, etc.) are escaped with backticks
- Nullability follows the same rules as Swift: Select respects column nullability, Insert makes
identity/generated/default columns nullable, Update makes everything nullable
- Inline snapshot test added covering tables, views, materialized views, enums, composite types, and
enum/table type references

Add a Kotlin type generator that produces @serializable data classes
from PostgreSQL schema, following the same pattern as the Swift generator.

- Three data classes per table: Select, Insert, Update
- Select-only for views and materialized views
- @serializable enum classes for PostgreSQL enums
- @SerialName annotations where camelCase differs from column name
- Kotlin keyword escaping with backticks

Ref: supabase-community/supabase-kt#647
Ref: https://github.com/orgs/supabase/discussions/43955

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

1 participant