Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested maps implementation & Annotation for unknown types #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ annotation class KtorFieldDescription(
val description: String = "",
val required: Boolean = false
)

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.FIELD)
annotation class OpenApiProperty(
val type : String = "",
val format: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.tabilzad.ktor.k2.visitors
import io.github.tabilzad.ktor.PluginConfiguration
import io.github.tabilzad.ktor.annotations.KtorDescription
import io.github.tabilzad.ktor.annotations.KtorFieldDescription
import io.github.tabilzad.ktor.annotations.OpenApiProperty
import io.github.tabilzad.ktor.getKDocComments
import io.github.tabilzad.ktor.k1.visitors.KtorDescriptionBag
import io.github.tabilzad.ktor.k1.visitors.toSwaggerType
Expand Down Expand Up @@ -44,7 +45,7 @@ internal class ClassDescriptorVisitorK2(
) : FirDefaultVisitor<ObjectType, ObjectType>() {


@OptIn(SealedClassInheritorsProviderInternals::class, SymbolInternals::class)
@OptIn(SealedClassInheritorsProviderInternals::class, SymbolInternals::class, PrivateForInline::class)
override fun visitProperty(property: FirProperty, data: ObjectType): ObjectType {
val coneTypeOrNull = property.returnTypeRef.coneTypeOrNull!!
val type = if (coneTypeOrNull is ConeTypeParameterType && genericParameters.isNotEmpty()) {
Expand Down Expand Up @@ -74,6 +75,21 @@ internal class ClassDescriptorVisitorK2(
data
}

property.findAnnotation(OpenApiProperty::class.simpleName) != null -> {
val formatAnnotation = property.findAnnotation(OpenApiProperty::class.simpleName)
val resolved = formatAnnotation?.let { FirExpressionEvaluator.evaluateAnnotationArguments(it, session) }
val dataType = resolved?.entries?.find { it.key.asString() == "type" }?.value?.result
val format = resolved?.entries?.find { it.key.asString() == "format" }?.value?.result
data.addProperty(
property,
objectType = ObjectType(
type = dataType?.accept(StringResolutionVisitor(), ""),
format = format?.accept(StringResolutionVisitor(), "")
), session
)
data
}

else -> {
val fqClassName = type.fqNameStr()

Expand Down Expand Up @@ -105,6 +121,12 @@ internal class ClassDescriptorVisitorK2(
)
}

valueClassType.isMap -> {
acc.type = "object"
acc.additionalProperties = valueClassType.resolveItems()
}


valueClassType.isAny -> {
acc.type = "object"
}
Expand Down Expand Up @@ -298,6 +320,11 @@ internal class ClassDescriptorVisitorK2(
ObjectType("string").apply {
enum = typeSymbol?.resolveEnumEntries()
}
} else if (type.isMap() && this.typeArguments.firstOrNull()?.type?.isString == true) {
ObjectType(
"object",
additionalProperties = this.typeArguments.lastOrNull()?.type?.resolveItems()
)
} else {
if (!classNames.names.contains(jetTypeFqName)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data class OpenApiSpec(
var type: String?,
var properties: MutableMap<String, ObjectType>? = null,
var items: ObjectType? = null,
var format: String? = null,
var enum: List<String>? = null,
@JsonIgnore
override var fqName: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@
}
}
}
},
"/v2/postInstantRequest" : {
"post" : {
"requestBody" : {
"required" : true,
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/sources.requests.InstantRequest"
}
}
}
}
}
}
},
"components" : {
Expand Down Expand Up @@ -128,6 +142,16 @@
}
}
},
"complexMapValueMap" : {
"type" : "object",
"properties" : { },
"additionalProperties" : {
"type" : "object",
"additionalProperties" : {
"$ref" : "#/components/schemas/sources.requests.ComplexMapValue"
}
}
},
"complexNestedList" : {
"type" : "array",
"items" : {
Expand Down Expand Up @@ -213,7 +237,21 @@
}
}
},
"required" : [ "list", "nestedList", "nestedMutableList", "complexList", "complexNestedList", "complexListStringMap", "complexListMap", "complexNestedListMap", "stringMap", "intValueMap", "complexValueMap", "enumValueMap", "complexEnumValueMap" ]
"required" : [ "list", "nestedList", "nestedMutableList", "complexList", "complexNestedList", "complexListStringMap", "complexListMap", "complexNestedListMap", "stringMap", "intValueMap", "complexValueMap", "enumValueMap", "complexEnumValueMap", "complexMapValueMap" ]
},
"sources.requests.InstantRequest" : {
"type" : "object",
"properties" : {
"date" : {
"type" : "string",
"format" : "iso 8601"
},
"formattedInstant" : {
"type" : "string",
"format" : "date-time"
}
},
"required" : [ "date", "formattedInstant" ]
},
"sources.requests.NestedRequest" : {
"type" : "object",
Expand Down
5 changes: 5 additions & 0 deletions create-plugin/src/test/resources/sources/RequestBody.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.routing.*
import sources.requests.ComplexRequest
import sources.requests.InstantRequest
import sources.requests.NestedRequest
import sources.requests.SimpleRequest

Expand Down Expand Up @@ -33,6 +34,10 @@ fun Application.requestBodyTest() {
post("/postBodyComplexRequest") {
call.receive<ComplexRequest>()
}

post("/postInstantRequest") {
call.receive<InstantRequest>()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package sources.requests

import io.github.tabilzad.ktor.annotations.OpenApiProperty
import java.time.Instant

data class SimpleRequest(
val string: String,
val integer: Int,
Expand Down Expand Up @@ -32,6 +35,7 @@ data class ComplexRequest(
val complexValueMap: Map<String, ComplexMapValue>,
val enumValueMap: Map<String, MyEnum>,
val complexEnumValueMap: Map<String, List<MyEnum>>,
val complexMapValueMap: Map<String, Map<String, ComplexMapValue>>
)

data class ComplexMapValue(
Expand All @@ -42,6 +46,8 @@ data class ComplexMapKey(
val something: Int
)

data class InstantRequest(@OpenApiProperty(type = "string", format = "iso 8601") val date: Instant, @OpenApiProperty(type = "string", format = "date-time") val formattedInstant: Instant)

enum class MyEnum {
ONE,
TWO,
Expand Down
Loading