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

FlowConverterFacotry: java.util.NoSuchElementException: List is empty. #511

Open
ahmadmssm opened this issue Jan 8, 2024 · 9 comments
Open
Labels
bug Something isn't working

Comments

@ahmadmssm
Copy link

Ktorfit version

1.11.1

What happened and how can we reproduce this issue?

I didn't find the FlowConverterFactory in the last version as I previously reported, so I copied it from the Repo but when trying to parse any response it falls at this line
val response = getResponse()
with this error
java.util.NoSuchElementException: List is empty.

What did you expect to happen?

The parsing to success as in the previous versions.

Is there anything else we need to know about?

No response

@ahmadmssm ahmadmssm added the bug Something isn't working label Jan 8, 2024
@ahmadmssm
Copy link
Author

I rolled back to a previous build and both bugs were not reproducible.

@ahmadmssm
Copy link
Author

The issue is here

  if (requestType.typeInfo.type == HttpResponse::class)
                            emit(getResponse())

if I add more than one response converter, it will jump automatically when I call getResponse() to the other response converter, if there is only one converter, then it will work as expected.

@niyajali
Copy link

niyajali commented Nov 6, 2024

Hi, I'm getting the same error on the release build whereas on debug build it's works fine.

ktorVersion = "3.0.0"
ktorfit = "2.1.0"
ktorfitKsp = "2.1.0-1.0.26"

Caused by: java.util.NoSuchElementException: List is empty.                                                                                 
	at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:221)                                                          
	at org.mifospay.core.network.utils.FlowConverterFactory$responseConverter$1.convert(FlowConverterFactory.kt:32)                         
	at org.mifospay.core.network.utils.FlowConverterFactory$responseConverter$1.convert(FlowConverterFactory.kt:30)                         
	at de.jensklingenberg.ktorfit.internal.KtorfitConverterHelper.request(KtorfitConverterHelper.kt:33)                                     
	at org.mifospay.core.network.services._ClientServiceImpl.getAccounts(_ClientServiceImpl.kt:148)                                         
	at org.mifospay.core.data.repositoryImp.SelfServiceRepositoryImpl.getActiveAccountsWithTransactions(SelfServiceRepositoryImpl.kt:139)   
	at org.mifospay.feature.home.HomeViewModel.<init>(HomeViewModel.kt:47)                                                                  
	at org.mifospay.feature.home.di.HomeModuleKt$HomeModule$lambda$0$$inlined$viewModelOf$default$1.invoke(ViewModelOf.kt:17)               
	at org.mifospay.feature.home.di.HomeModuleKt$HomeModule$lambda$0$$inlined$viewModelOf$default$1.invoke(ViewModelOf.kt:59) (Ask Gemini)  
	at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50)                                                                 

@ahmadmssm
Copy link
Author

@niyajali
Try using the following combination together
ktorVersion = "3.0.0"
ktorfit = "2.1.0"
ktorfitKsp = "2.0.21-1.0.25"

@Foso
Copy link
Owner

Foso commented Nov 10, 2024

@niyajali Could be an issue with R8. What does your ConverterFactory look like?

@niyajali
Copy link

I'm using Ktorfit FlowConverterFactory

@Foso
Copy link
Owner

Foso commented Nov 10, 2024

@niyajali So you are using exactly the same code as in here in your code? https://github.com/Foso/Ktorfit/blob/master/ktorfit-converters/flow/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/FlowConverterFactory.kt

I'm asking because based on the package name "org.mifospay.core.network.utils.FlowConverterFactory" you are using your own class and i have other way of knowing what the code in there looks like

@niyajali
Copy link

@Foso Yes I've copied exact same code, and also tried with ktorfit-flow-converter library and getting the same error on release build

@niyajali
Copy link

package org.mifospay.core.network.utils

import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.converter.Converter
import de.jensklingenberg.ktorfit.converter.KtorfitResult
import de.jensklingenberg.ktorfit.converter.TypeData
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

/**
 * Factory that enables the use of Flow<T> as return type
 */
public class FlowConverterFactory : Converter.Factory {
    override fun responseConverter(
        typeData: TypeData,
        ktorfit: Ktorfit,
    ): Converter.ResponseConverter<HttpResponse, *>? {
        if (typeData.typeInfo.type == Flow::class) {
            return object : Converter.ResponseConverter<HttpResponse, Flow<Any?>> {
                override fun convert(getResponse: suspend () -> HttpResponse): Flow<Any?> {
// Getting error on below line, somehow typeArgs is empty after applying progurd, so when we calling first() resulting NoSuchElementException
                    val requestType = typeData.typeArgs.first() 
                    return flow {
                        val response = getResponse()
                        if (requestType.typeInfo.type == HttpResponse::class) {
                            emit(response)
                        } else {
                            val convertedBody =
                                ktorfit.nextSuspendResponseConverter(
                                    this@FlowConverterFactory,
                                    typeData.typeArgs.first(),
                                )?.convert(KtorfitResult.Success(response))
                                    ?: response.body(typeData.typeArgs.first().typeInfo)
                            emit(convertedBody)
                        }
                    }
                }
            }
        }
        return null
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants