Tamas
02/17/2020, 7:57 PMval updates = call.receive<List<PositionPatch>>()
however I receive List<LinkedHashMap
. when I send / receive just one object, then it works like a charm.
is there a way to influence the binding?Evan R.
02/19/2020, 1:41 PMTamas
02/19/2020, 1:48 PMEvan R.
02/19/2020, 3:53 PMval updatesString = call.receive<String>()
val updates = jacksonObjectMapper.read<List<PostitionPatch>>()
Or something else?Evan R.
02/19/2020, 3:55 PMEvan R.
02/19/2020, 3:55 PMTamas
02/20/2020, 11:38 AMinstall(ContentNegotiation) {
jackson {
findAndRegisterModules()
enable(SerializationFeature.INDENT_OUTPUT)
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
}
}
Tamas
02/20/2020, 11:39 AMval updates = call.receive<List<PositionPatch>>()
then I receive a LinkedHashMap inside of the listTamas
02/20/2020, 11:39 AMTamas
02/20/2020, 11:39 AMval mapper = jacksonObjectMapper()
val updateString = call.receive<String>()
val type = TypeFactory
.defaultInstance()
.constructParametricType(List::class.java, PositionPatch::class.java)
val updates = mapper.readValue(updateString, type) as List<PositionPatch>
Evan R.
02/20/2020, 1:48 PMTamas
02/20/2020, 1:50 PMTamas
02/20/2020, 1:50 PMinstall(ContentNegotiation) {
jackson {
findAndRegisterModules()
Tamas
02/20/2020, 1:51 PMimplementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_version"
Tamas
02/20/2020, 1:51 PMTamas
02/20/2020, 1:51 PM@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(name = "update-category", value = UpdateCategory::class),
JsonSubTypes.Type(name = "update-comment", value = UpdateComment::class)
)
sealed class PositionPatch(
val type: String
)
@JsonTypeName("update-category")
data class UpdateCategory(val category: Category?) : PositionPatch("update-category")
@JsonTypeName("update-comment")
data class UpdateComment(val comment: String?) : PositionPatch("update-comment")
Evan R.
02/20/2020, 1:53 PMTamas
02/20/2020, 1:53 PMEvan R.
02/20/2020, 1:54 PMTamas
02/20/2020, 1:54 PMext.kotlin_version = '1.3.61'
ext.jackson_version = '2.10.1'
ext.koin_version = '2.0.1'
Tamas
02/20/2020, 1:57 PMTamas
02/20/2020, 8:14 PMTamas
02/20/2020, 8:14 PMbackend-tests.http
with which you can testEvan R.
02/23/2020, 3:06 PMEvan R.
02/23/2020, 3:07 PMoverride suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? {
val request = context.subject
val type = request.type
val value = request.value as? ByteReadChannel ?: return null
val reader = value.toInputStream().reader(context.call.request.contentCharset() ?: Charsets.UTF_8)
return objectmapper.readValue(reader, type.javaObjectType)
}
Evan R.
02/23/2020, 3:10 PMtype.javaObjectType
in this case refers to java.util.list
with the generics erased. However, the subject has a typeInfo
property with the full reified generic type info that just isn't being used. I'd open an issue on the Ktor github or YouTrack.Evan R.
02/23/2020, 3:12 PMObjectMapper.readValue<List<PositionPatch>>()
via the extension method from the Jackson Kotlin Module and it will still work.Evan R.
02/23/2020, 3:13 PMimport com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
// ...
post("/sample-receive/list") {
val receivedList = call.receive<String>()
val om = ObjectMapper().registerKotlinModule()
println(om.readValue<List<PositionPatch>>(receivedList))
call.respond(HttpStatusCode.OK)
}
Tamas
03/04/2020, 7:56 PMTamas
03/04/2020, 7:57 PM