-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for kotlinx serialization SerialName
- Loading branch information
Showing
10 changed files
with
156 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
create-plugin/src/main/kotlin/io/github/tabilzad/ktor/k2/JsonNameResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.tabilzad.ktor.k2 | ||
|
||
import org.jetbrains.kotlin.fir.FirSession | ||
import org.jetbrains.kotlin.fir.declarations.FirProperty | ||
import org.jetbrains.kotlin.fir.declarations.getStringArgument | ||
import org.jetbrains.kotlin.fir.declarations.primaryConstructorIfAny | ||
import org.jetbrains.kotlin.fir.declarations.utils.isData | ||
import org.jetbrains.kotlin.fir.expressions.FirAnnotation | ||
import org.jetbrains.kotlin.fir.resolve.fqName | ||
import org.jetbrains.kotlin.fir.resolve.getContainingClass | ||
|
||
object JsonNameResolver { | ||
|
||
fun getCustomNameFromAnnotation( | ||
property: FirProperty, | ||
session: FirSession, | ||
): String? = getCustomNameFromPropertyAnnotation(property, session) | ||
?: getMoshiNameFromDataClassConstructorParamAnnotation(property, session) | ||
|
||
private fun getCustomNameFromPropertyAnnotation( | ||
property: FirProperty, | ||
session: FirSession, | ||
): String? = property.annotations.getCustomNameFromAnnotation(session) | ||
|
||
private fun List<FirAnnotation>?.getCustomNameFromAnnotation( | ||
session: FirSession | ||
): String? = this?.let { annotations -> | ||
SerializationFramework.entries.mapNotNull { | ||
annotations | ||
.find { annotation -> annotation.fqName(session) == it.fqName } | ||
?.getStringArgument(it.identifier, session) | ||
} | ||
}?.firstOrNull() | ||
|
||
private fun getMoshiNameFromDataClassConstructorParamAnnotation( | ||
property: FirProperty, | ||
session: FirSession, | ||
): String? = property.getContainingClass(session) | ||
?.takeIf { it.isData } | ||
?.primaryConstructorIfAny(session) | ||
?.valueParameterSymbols | ||
?.find { it.name == property.name } | ||
?.annotations | ||
?.getCustomNameFromAnnotation(session) | ||
} |
49 changes: 0 additions & 49 deletions
49
create-plugin/src/main/kotlin/io/github/tabilzad/ktor/k2/MoshiJsonNameResolver.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
create-plugin/src/test/resources/expected/SerializationAnnotated-expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"openapi" : "3.1.0", | ||
"info" : { | ||
"title" : "Open API Specification", | ||
"description" : "test", | ||
"version" : "1.0.0" | ||
}, | ||
"paths" : { | ||
"/v1/action" : { | ||
"post" : { | ||
"requestBody" : { | ||
"required" : true, | ||
"content" : { | ||
"application/json" : { | ||
"schema" : { | ||
"$ref" : "#/components/schemas/sources.SerialNameAnnotated" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components" : { | ||
"schemas" : { | ||
"sources.SerialNameAnnotated" : { | ||
"type" : "object", | ||
"properties" : { | ||
"notAnnotated" : { | ||
"type" : "string" | ||
}, | ||
"serial_annotated_constructor_derived_property" : { | ||
"type" : "string" | ||
}, | ||
"serial_annotated_constructor_parameter" : { | ||
"type" : "string" | ||
}, | ||
"serial_annotated_lateinit_var" : { | ||
"type" : "string" | ||
}, | ||
"serial_annotated_mutable_property" : { | ||
"type" : "string" | ||
} | ||
}, | ||
"required" : [ "serial_annotated_constructor_parameter", "notAnnotated", "serial_annotated_constructor_derived_property", "serial_annotated_lateinit_var" ] | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
create-plugin/src/test/resources/sources/SerializationAnnotated.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sources | ||
|
||
import io.github.tabilzad.ktor.annotations.GenerateOpenApi | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SerialNameAnnotated( | ||
@SerialName("serial_annotated_constructor_parameter") | ||
val serialNameAnnotatedConstructorParameter: String, | ||
val notAnnotated: String, | ||
) { | ||
@SerialName("serial_annotated_constructor_derived_property") | ||
val serialNameAnnotatedConstructorDerivedProperty = notAnnotated + "" | ||
|
||
@SerialName("serial_annotated_mutable_property") | ||
var serialNameAnnotatedProperty: String? = null | ||
|
||
@SerialName("serial_annotated_lateinit_var") | ||
lateinit var serialNameAnnotatedLateinitVar: String | ||
} | ||
|
||
@GenerateOpenApi | ||
fun Application.moduleSerialization() { | ||
routing { | ||
route("/v1") { | ||
post("/action") { | ||
call.receive<SerialNameAnnotated>() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters