Skip to content

kotlin: Mapping from null property to object with default value #28

Description

@brice-laurencin

Given this code:

    data class From (
        val nonNullProperty:String = "I'm not nullable",
        val nullProperty:String? = null
    )

    @NoArgsConstructor
    data class To(
        val nonNullProperty: String,
        val otherNonNullProperty: String = "I'm not nullable",
    )
    @Test
    fun rien() {

        val shapeshift = ShapeShiftBuilder().withMapping<From, To> {
            autoMap(AutoMappingStrategy.BY_NAME)
            From::nullProperty mappedTo To::otherNonNullProperty
        }.build()



        checkNotNull(shapeshift.map<From, To>(From()).otherNonNullProperty)
    }

I would assume that the resulting object had otherNonNullProperty with its default value, but it seems using the no-arg constructor from noArg kotlin plugin does not honor default values.

Any way to work around this? I'd rather not use an ObjectSupplier if that would be possible, this seems tedious and error-prone.

FYI, if all the members of To have a default value, it works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions