Skip to content

Support for Kotlin value types #1098

@FWest98

Description

@FWest98

Currently, this library does not support Kotlin (inline) value types/classes for a config mapping setup.

For an example, let's consider the following definition:

@JvmInline
value class Size (private val value: Long)
class SizeConverter : Converter<Size> { ... }

@ConfigMapping
interface Settings {
    fun size(): Size
}

In this setup, explicitly requesting an instance of Size using getValue works fine, as expected since the target type is explicitly passed. However, for the configmap setup, Smallrye will see the size field for its backing type, Long. Then, conversion fails since the converter is defined for Size and not Long. Note that annotating the field with WithConverter does not work - Smallrye will throw an error that the types do not match.

I am afraid this is not a very easy fix: it seems like special, Kotlin-specific, reflection APIs are needed to find the "actual" value class type. For example, Jackson does not yet support this in mainline and has a separate experimental library to implement it: https://github.com/ProjectMapK/jackson-module-kogera

There do seem to be a few workarounds at the moment:

  • One could force Kotlin to make the field "boxed" by making the value class implement an interface and then setting the configmap field type to be the interface. However, that triggers another issue: Converter for interface type is ignored #931
  • One could write a custom converter for the backing field type (Long), but then this converter cannot be auto-discovered to prevent conflicts with normal uses of that backing type. Consequently, each configmap field needs to be annotated by hand.
  • Not using a value class - which is not always desirable given the overhead of boxing/unboxing a primitive type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions