[kotlin][kotlin-spring] Add companionObject option to generate companion objects in data classes
Closed
TH
8mo ago+103+103+012345678901234567890123456789-3-3-0123456789
Summary
- Adds a new boolean CLI option
companionObject(default:false) to both the kotlin client and kotlin-spring server generators - Generates an empty
companion object { }on all data class models, enabling users to add companion extensions from outside the generated code - Companion object is only emitted when no other feature (
serializableModel,generateRoomModels,generateOneOfAnyOfWrappers) already provides one, avoiding duplicates
Motivation
Kotlin allows extensions on companion objects, but only if the class declares one. Since generated model classes don't have companion objects by default, users can't add static-style methods that feel natural in Kotlin.
With companionObject=true, users can define companion extensions on the generated API models in their own code:
// Map between API DTOs and domain models
fun Pet.Companion.fromDomain(pet: DomainPet): Pet =
Pet(
id = pet.id,
name = pet.displayName,
status = Pet.Status.valueOf(pet.availability.name.lowercase()),
photoUrls = pet.images.map { it.url },
category = Category.fromDomain(pet.category),
tags = pet.labels.map { Tag(id = it.id, name = it.value) }
)
// Build common request payloads
fun Order.Companion.placeFor(petId: Long, quantity: Int = 1): Order =
Order(
petId = petId,
quantity = quantity,
shipDate = OffsetDateTime.now(),
status = Order.Status.placed,
complete = false
)
// Test fixtures for API integration tests
fun User.Companion.fixture(
username: String = "test-user-${UUID.randomUUID()}",
email: String = "test@example.com"
): User = User(
id = 0,
username = username,
email = email,
password = "test-password",
phone = "+1555000000",
userStatus = 1
)
// Validate request payloads before sending
fun Pet.Companion.validatePayload(pet: Pet): List<String> = buildList {
if (pet.name.isBlank()) add("name is required")
if (pet.photoUrls.isEmpty()) add("at least one photoUrl is required")
pet.name.length.takeIf { it > 255 }?.let { add("name exceeds max length of 255") }
}
// Inspect API response models
fun ModelApiResponse.Companion.isSuccess(response: ModelApiResponse): Boolean =
response.code in 200..299
Without a companion object on the generated class, none of these patterns compile ā you'd have to use top-level functions or wrapper classes, losing the ClassName.method() call-site ergonomics.
Changes
kotlin (client)
- KotlinClientCodegen.java: Added
COMPANION_OBJECTconstant, field, getter, CLI option,processOpts()handling, andpostProcessModels()x-has-data-class-bodycondition - data_class.mustache: Added conditional empty companion object block, guarded to avoid duplicate companion objects
- KotlinClientCodegenModelTest.java: Added tests for option default and generated output verification
kotlin-spring (server)
- KotlinSpringServerCodegen.java: Added
COMPANION_OBJECTconstant, field, CLI option, andprocessOpts()handling - dataClass.mustache: Added conditional empty companion object block, guarded against
serializableModelduplicates - KotlinSpringServerCodegenTest.java: Added tests for option default and generated output verification
PR checklist
- Read the contribution guidelines
- Pull request title follows conventional commit style
- Ran tests (46 tests, 0 failures across both generators)
- Regenerated samples for both
kotlin-*andkotlin-spring-*ā no diff (default isfalse) - No breaking changes to existing generated code
/cc @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l @dennisameling @wing328
š¤ Generated with Claude Code