ansman/kotshi

No way to set default value for a List of Lists?

Closed this issue · 7 comments

I'm trying to parse Json which has an optional field whose format is a list of lists of strings. Worked fine with moshi-kotlin; with kotshi I assume I need to find a way to bind a default value in order for this to work.

So I added a method to the project:

@JsonDefaultValue
fun <T> provideDefaultList() = listOf<T>()

and annotated the param with @JsonDefaultValue as well. However, this gives me an error at code generation time: "Generic classes must not have wildcard types if you want to use default values", referencing to the List<List<String>> type.

I don't think there is a wildcard type here. I also compiled a local version of the compiler with this error check commented out and it seems to be working fine and assigning the right default value. Is this just an overzealous error check that needs to be relaxed, or are there deeper issues here?

I'm not at all familiar with this, but FWIW it seems the check requires that the type argument be ClassName, and the argument is instead a ParameterizedTypeName. At a glance... would we need to do a recursive check here of some sort?

Which version are you using? This was fixed in 1.0.2

And Kotlin adds wildcard types automatically when possible.

I tried both 1.0.3 and the latest checkout from yesterday and both hit this issue. Interestingly, though, when I tried to just add a unit test to Kotshi itself, I didn't encounter this issue. So something must be different with the environment.

Here's a small project reproducing the issue. Unpack and run ./gradlew assemble, and you'll get this error:

error: Kotshi: Generic classes must not have wildcard types if you want to use default values
    java.util.List<? extends java.util.List<java.lang.String>> dummy) {

The only code in the project is this data class:

@JsonSerializable
data class DummyClass(
        @JsonDefaultValue
        val dummy: List<List<String>>
)

kotshi-example.zip

Ah, my bad. It had to do with default values. The code is very clear as to it not being supported but I'm unsure as to why. I've fixed it in #87

Thanks! Patched this in locally and it seems to work as expected.

Fixed, either you can apply @JvmSuppressWildcards or use the latest snapshot

FWIW I did try using @JvmSuppressWildcards on 1.0.3 but it didn't work for me. I declared the field value as List<@JvmSuppressWildcards List<Int>>, but still got the warning:

error: Kotshi: Generic classes must not have wildcard types if you want to use default values
        java.util.List<java.util.List<java.lang.Integer>> backgroundShapeBoxes) {
                                                          ^

Released in 1.0.4