Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@FilterNulls in Kotlin not excluding null in list #78

Open
elivity opened this issue May 5, 2022 · 0 comments
Open

@FilterNulls in Kotlin not excluding null in list #78

elivity opened this issue May 5, 2022 · 0 comments

Comments

@elivity
Copy link

elivity commented May 5, 2022

Hey,

I have the following issue and maybe I am just missing something:

I want to filter out null in an enum list. It works great in Java, but fails in Kotlin.

The following test reproduces the "wrong" behaviour:

import com.serjltt.moshi.adapters.DefaultOnDataMismatchAdapter
import com.serjltt.moshi.adapters.FilterNulls
import com.squareup.moshi.Moshi
import org.junit.Test

class AnimalTest {
    enum class Animal {
        Dog,
        Cat
    }

    data class Animals(
        @FilterNulls val animals: List<Animal> = arrayListOf()
    )

    @Test
    fun testMoshiEnumNullable() {
        val moshi = Moshi.Builder()
            .add(FilterNulls.ADAPTER_FACTORY)
            .add(DefaultOnDataMismatchAdapter.newFactory(Animal::class.java, null))
            .add(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory())
            .build()

        val response = moshi.adapter(Animals::class.java).fromJson("""{"animals" : [
            "Dog",
            "Unknown"
        ]}""")

        assert(response != null)
        // Gets [Dog, null] instead of just [Dog]
        assert(response == Animals(animals = arrayListOf(Animal.Dog)))
    }
}
@elivity elivity changed the title @FilterNulls in Kotlin @FilterNulls in Kotlin not excluding Nulls in List May 5, 2022
@elivity elivity changed the title @FilterNulls in Kotlin not excluding Nulls in List @FilterNulls in Kotlin not excluding null in List May 5, 2022
@elivity elivity changed the title @FilterNulls in Kotlin not excluding null in List @FilterNulls in Kotlin not excluding null in list May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant