chimney icon indicating copy to clipboard operation
chimney copied to clipboard

withFieldComputed is ignored when enableBeanSetters

Open melgenek opened this issue 3 years ago • 1 comments

The example works with version 0.4.2 but it doesn't compile in 0.5.*

import io.scalaland.chimney.dsl._

import scala.beans.BeanProperty

case class Entity(field1: String, field2: Option[String])

class AnotherEntity {
  @BeanProperty
  var field1: String = _
  @BeanProperty
  var field2: String = _
}

object Main extends App {
  val entity = Entity("1", Some("2"))

  val anotherEntity = entity.into[AnotherEntity]
    .withFieldComputed(_.field2, _.field2.orNull)
    .enableBeanSetters
    .transform
}

With 0.4.2 chimney the example compiles and properly copies the values. With 0.5.3 I have an error

Chimney can't derive transformation from hello.Entity to hello.AnotherEntity

java.lang.String
  derivation from entity.field2: scala.Option to java.lang.String is not supported in Chimney!


hello.AnotherEntity
  field2: java.lang.String - can't derive transformation from field2: scala.Option in source type hello.Entity

The issue can be related to https://github.com/scalalandio/chimney/issues/110 but the field is not missing.

Here is a complete example that reproduces the issue https://github.com/melgenek/chimney-playground

melgenek avatar Aug 03 '20 08:08 melgenek

From this example it looks like the 0.4.2 behaviour was accidental and based on the fact that the vars in this example were public. Only that way you could even access _.field2 of AnotherEntity in withFieldComputed.

Usually Java beans don't expose their fields and you can only access public getters/setters. Currently the library doesn't support withFieldComputed for Java beans. The easiest way to fix it would be to implement #110.

krzemin avatar Aug 05 '20 17:08 krzemin

Resolved on branch scala-3 (https://github.com/scalalandio/chimney/pull/325).

MateuszKubuszok avatar Jul 11 '23 23:07 MateuszKubuszok