Skip to content

Custom converter for constructor creation no longer applied #585

@tomas-c

Description

@tomas-c

I was previously using version 1.2.3 and just upgraded to 1.2.6 and noticed an issue when using a custom converter to map Postgres json columns to Kotlin data classes.

Here's my entity object:
@Table("test")
data class TestEntity(
    val data: TestData,    // JSON column in the database
    @Id
    val id: UUID = UUID.randomUUID()
)
Here's my custom converter:
@ReadingConverter
class R2dbcJsonToObjectConverter(
    private val objectMapper: ObjectMapper
) : GenericConverter, ConditionalConverter {

    override fun getConvertibleTypes(): Set<GenericConverter.ConvertiblePair>? {
        return setOf(GenericConverter.ConvertiblePair(Json::class.java, Object::class.java))
    }

    override fun convert(source: Any?, sourceType: TypeDescriptor, targetType: TypeDescriptor): Any? {
        if (Json::class.java.isAssignableFrom(sourceType.type)) {
            return objectMapper.readValue((source as Json).asString(), targetType.type)
        } else {
            return null;
        }
    }

    override fun matches(sourceType: TypeDescriptor, targetType: TypeDescriptor): Boolean {
        return Json::class.java.isAssignableFrom(sourceType.type)
    }
}

It looks like version 1.2.6 calls MappingR2dbcConverter.readFrom here which considers TestEntity.data field as an entity here and tries to create an instance of TestData instead of using the conversion service.

In version 1.2.3 the conversion was performed instead by calling MappingR2dbcConverter.resultSet.get here and using the conversion service, without a call to MappingR2dbcConverter.readFrom.

So far I can think of two solutions:

  • do not use complex types in entity objects - make TestEntity.data field of type Json and manually convert to the TestData object
  • add TestData to a list of simple types such that MappingR2dbcConverter does not consider it an entity

Is there a better way?

Thanks for this library and all the work so far!

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions