retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

support return a List that its' element may be null

Open ethi1989 opened this issue 1 year ago • 2 comments

it seems no support the return a List that its' element may be null ,like below example. The function prototype

interface ApiService {
  suspend fun funA(
        @Body ids: List<Long>
    ): Result<BaseResponse<List<ADTO?>>>
}

The Api Provide like below

    fun provideApiService(configure: AppConfigure, okHttpClient: OkHttpClient): ApiService {
        val appJson = Json {
            ignoreUnknownKeys = true
            explicitNulls = true
        }
        return Retrofit.Builder()           
            .client(okHttpClient)
            .baseUrl(configure.baseApiUrl)
            .addConverterFactory(appJson.asConverterFactory("application/json;charset=UTF-8".toMediaType()))
            .addCallAdapterFactory(ResultCallAdapterFactory.create())
            .build().create(ApiService::class.java)
    }

The packet is {"code":0,"data":[{"createTime":"2022-11-23 15:28:29","faceId":123,"faceUrl":"","feature":"ABC"},null,null]}

it is failed, because the null element , how can i to solve this problem? tks

ethi1989 avatar Aug 27 '24 09:08 ethi1989

Retrofit is a Java library and uses Java reflection to pass type information to the body converters. As such, we don't get nullability information (at least until future versions of Java add it).

Because the source of the reflective type is Java-based, I would hope that the conversion library would assume nulls are allowed (since it's effectively a platform type).

JakeWharton avatar Aug 27 '24 21:08 JakeWharton

I'll bring this up with the kotlinx.serialization devs

JakeWharton avatar Aug 27 '24 21:08 JakeWharton

I'll bring this up with the kotlinx.serialization devs

is it will be support in the future? I use GsonConvertyFactory temporary. tks again

ethi1989 avatar Aug 28 '24 14:08 ethi1989

Right so this is simply a limitation of using Java-based reflection and our inability to read the Kotlin module metadata to parse out the nullability of the inner type. There's nothing we can do right now. #3075 tracks parsing Kotlin metadata.

JakeWharton avatar Sep 18 '24 21:09 JakeWharton