Parameter type must not include a type variable or wildcard
Asked Answered
M

2

10

in my android app I use Retrofit 2:

public enum OperationType {
    @SerializedName("payment")
    PAYMENT,
    @SerializedName("payout")
    PAYOUT,
    @SerializedName("transfer")
    TRANSFER
}




fun getOperationsList(typeList: List<OperationType>, callback: Callback<List<Operation>>) {
        val call = myRestClient.getOperationsList(typeList)
        executeRequest(call, callback)
}


@GET("/operations")
fun getOperationsList(@Query("type") typeList: List<OperationType>): Call<List<Operation>>

but I get runtime error in this line:

val call = myRestClient.getOperationsList(typeList)

error:

Shutting down VM
 FATAL EXCEPTION: main
 Process: md.qsystems.android.tango.debug, PID: 22714
 java.lang.IllegalArgumentException: Parameter type must not include a type variable or wildcard: java.util.List<? extends OperationType> (parameter #1)
     for method TangoRestClient.getOperationsList
    at retrofit2.Utils.methodError(Utils.java:52)
    at retrofit2.Utils.methodError(Utils.java:42)
    at retrofit2.Utils.parameterError(Utils.java:61)
    at retrofit2.RequestFactory$Builder.validateResolvableType(RequestFactory.java:764)
    at retrofit2.RequestFactory$Builder.parseParameterAnnotation(RequestFactory.java:401)
    at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:306)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:193)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:149)
    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
    at $Proxy1.getOperationsList(Unknown Source)
    at mTransportService.getOperationsList(TransportService.kt:160)
Masaccio answered 6/8, 2019 at 11:48 Comment(0)
P
13

You can add @JvmSuppressWildcards either inside the angle brackets before the String type, or before the List. Both should work and remove the wildcard.

Workaround taken from this issue.

Phytoplankton answered 6/8, 2019 at 11:56 Comment(1)
IDE was showing Unresolved reference error until I added s at the end, so use @JvmSuppressWildcardsNonpartisan
L
0

If you have several Lists in a request probably you should add several @JvmSuppressWildcardss.

@GET("/get-teams")
suspend fun teams(
    @Query("statuses") statuses: List<@JvmSuppressWildcards TeamStatus>? = null,
    @Query("types") types: List<@JvmSuppressWildcards TeamType>? = null,
    @Query("name") name: String? = null,
): List<Team>

Or replace complex types with simple. For instance, TeamStatus is a enum:

enum class TeamStatus {
    ENABLED,
    DISABLED,
}

Then replace the method:

@GET("/get-teams")
suspend fun teams(
    @Query("statuses") statuses: List<String>? = null,
    @Query("types") types: List<String>? = null,
    @Query("name") name: String? = null,
): List<Team>

and convert data:

teams(
    statuses = statuses.map { it.name },
    types = types.map { it.name },
)
Laudianism answered 4/4, 2024 at 9:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.