Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion e2e/binance/src/test/kotlin/BinanceTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import binance.client.HttpResult
import binance.client.api.v3.V3Client
import binance.models.paths.api.v3.avgPrice.get.response.GetApiV3AvgPriceResponse400
import binance.models.paths.api.v3.klines.get.parameters.i1.`1`
import binance.models.paths.api.v3.klines.get.parameters.i1.Interval
import io.ktor.client.engine.cio.*
import io.ktor.client.engine.mock.*
Expand Down
1 change: 0 additions & 1 deletion e2e/polymorphism/src/test/kotlin/ClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.denisbrandi.netmock.engine.NetMockEngine
import kotlinx.coroutines.test.runTest
import sample.client.Client
import sample.models.components.parameters.UserType
import sample.models.components.parameters.UserTypeParam
import sample.models.components.schemas.AdminUser.AdminUser
import sample.models.paths.users.get.response.GetUsersResponse200
import kotlin.test.Test
Expand Down
13 changes: 13 additions & 0 deletions e2e/polymorphism/src/test/resources/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ components:
enum:
- value1
- value2
Companion:
properties:
intId:
type: integer
description:
type: string

CompanionList:
properties:
companions:
type: array
items:
"$ref": '#/components/schemas/Companion'
User:
nullable: true
description: Normal user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dshatz.openapi2ktor.generators

import com.dshatz.openapi2ktor.generators.Type.Companion.simpleType
import com.dshatz.openapi2ktor.utils.*
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
Expand Down Expand Up @@ -98,12 +99,14 @@ class TypeStore {
}
}

fun <T: Type> T.makeTypeName(): TypeName {
return when (this) {
is Type.WithTypeName -> typeName as ClassName
is Type.Reference -> resolveReference(jsonReference).makeTypeName()
is Type.List -> List::class.asTypeName().parameterizedBy(itemsType.makeTypeName())
is Type.SimpleType -> this.kotlinType
fun <T: Type> T.makeTypeName(): TypeName = with (ReservedKeywords) {
escapeKeywords().run {
return when (this) {
is Type.WithTypeName -> typeName as ClassName
is Type.Reference -> resolveReference(jsonReference).makeTypeName()
is Type.List -> List::class.asTypeName().parameterizedBy(itemsType.makeTypeName())
is Type.SimpleType -> this.kotlinType
}
}
}

Expand Down Expand Up @@ -220,4 +223,13 @@ class TypeStore {
return sb.toString()
}

}
}

object ReservedKeywords {
private val reservedKeywords = setOf("Companion")
fun Type.escapeKeywords(): Type {
return if (this is Type.WithTypeName && this.simpleName() in reservedKeywords) {
this.withTypeName(this.typeName.updateSimpleName { it + "_" })
} else this
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dshatz.openapi2ktor.generators.models

import com.dshatz.openapi2ktor.GeneratorConfig
import com.dshatz.openapi2ktor.generators.ReservedKeywords
import com.dshatz.openapi2ktor.generators.Type
import com.dshatz.openapi2ktor.generators.TypeStore
import com.dshatz.openapi2ktor.utils.*
Expand Down Expand Up @@ -131,7 +132,7 @@ class KotlinxCodeGenerator(override val typeStore: TypeStore, private val packag
val typeToSuperInterfaces = interfaceMappingForOneOf(typeStore)
.mapKeys { it.key.makeTypeName() }
return typeStore.getTypes().values.filterIsInstance<Type.WithTypeName.Object>().map { type ->
val className = type.typeName as ClassName
val className = with (ReservedKeywords) { type.escapeKeywords().makeTypeName() as ClassName }
val fileSpec = FileSpec.builder(className)

val interfacesForOneOf = typeToSuperInterfaces[type.typeName]
Expand Down Expand Up @@ -382,9 +383,11 @@ class KotlinxCodeGenerator(override val typeStore: TypeStore, private val packag

val prop = PropertySpec.builder(name, finalType)
.apply {
// Add kdoc
if (propInfo.doc != null) {
addKdoc(propInfo.doc.toCodeBlock(::findConcreteType))
}
// Add serializable annotation. From a non-null type.
val simpleTypeOrNull = (actualType as? Type.SimpleType)?.kotlinType?.copy(nullable = false)
if (simpleTypeOrNull != null && isDate(simpleTypeOrNull)) {
addAnnotation(AnnotationSpec.builder(Serializable::class.asClassName()).addMember(CodeBlock.of("%T::class", dateSerializers[simpleTypeOrNull]!!)).build())
Expand Down