retrofit
retrofit copied to clipboard
support return a List that its' element may be null
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
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).
I'll bring this up with the kotlinx.serialization devs
I'll bring this up with the kotlinx.serialization devs
is it will be support in the future? I use GsonConvertyFactory temporary. tks again
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.