Skip to content

[BUG] Generic mapping of java.util.Optional does not work #353

@slaaap

Description

@slaaap

Describe the bug
Generic mapping of java.util.Optional field does not work

To Reproduce

data class O1(val value: Optional<String>)
data class O2 (val value: String?)
object OMapper: ObjectMappie<O1, O2>() {
  // works fine
  fun mapOptional(value: Optional<String>) = value.orElse(null)
  // Fails with 'cannot assign source type Optional<String> to target type String?'
  //fun <T> mapOptional(value: Optional<T>): T? = value.orElse(null)
}

fun main () {
  OMapper.map(O1(Optional.empty())).let(::println)
  OMapper.map(O1(Optional.of("test"))).let(::println)
}

Expected behavior
java.util.Optional can be generically mapped to nullable T and vice-versa, currently it only possible to map by explicitly specifying generic type

Kotlin version
2.3.0

Mappie version
2.3.0-2.3.1

Mappie and Kotlin compatibility
Yes

Invocation method
Gradle

Additional context
Can it be considered to add Optional<-> Nullable as build-in mapper?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions