OpenFeign / querydsl

Unified Queries for Java
https://querydsl.com
Apache License 2.0
260 stars 39 forks source link

fix: map Java Object to Kotlin Any #511

Closed allantodd closed 3 months ago

allantodd commented 3 months ago

The code generator uses Object instead of Any resulting in compiler warnings as described at https://github.com/querydsl/querydsl/issues/3740

allantodd commented 3 months ago

Thanks, sorry I didn't get time to reply sooner.

Here's a more complete example of how we're using Any to store arbitrary nested metadata as JSON in Postgres. The problem is that basically Kotlin calls Object Any and it was missed in the mappings.

    @Convert(converter = MetadataConverter::class)
    @Column(columnDefinition = "jsonb")
    var metadata: Map<String, Any> = mutableMapOf()

where the converter is

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import jakarta.persistence.AttributeConverter

class MetadataConverter : AttributeConverter<Map<String, Any>, String> {

    private val objectMapper = jacksonObjectMapper()

    override fun convertToDatabaseColumn(attribute: Map<String, Any>?): String {
        return objectMapper.writeValueAsString(attribute ?: emptyMap<String, Any>())
    }

    override fun convertToEntityAttribute(dbData: String?): Map<String, Any> {
        return objectMapper.readValue(dbData ?: "{}")
    }
}
velo commented 3 months ago

Well, if you keen, please update the example https://github.com/OpenFeign/querydsl/tree/master/querydsl-examples/querydsl-example-kotlin-codegen

All good if you can't, but amazing if you could

allantodd commented 3 months ago

Ah, hadn't seen that module. Okay will take a look. Thanks