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

Feature Request - Ignoring methods in the interface with an annotation #4130

Open
ghahramani opened this issue Apr 15, 2024 · 2 comments
Open

Comments

@ghahramani
Copy link

I have multiple interfaces for different external services which some of interfaces are implemented via Retrofit and some have a custom implementation. Therefore, I needed to put a base interface as the contract for all clients. Here is the example

@Scope(SCOPE_PROTOTYPE)
interface BaseApiClient {

    fun type(): Type

    fun supports(type: Type): Boolean = type == type()

    fun create(
        headers: Map<String, String> = hashMapOf(),
        body: CreateClientRequestRestModel
    ): Mono<CreateClientResponseRestModel>

}

and the retrofit interface is

interface RetrofitClient : BaseApiClient {

    override fun type() = EXTERNAL_ONE

    @POST("api/blah/blah")
    override fun create(
        @HeaderMap headers: Map<String, String>,
        @Body body: @JvmSuppressWildcards CreatClientRequestRestModel
    ): Mono<CreateClientResponseRestModel>

}

I am encountering this error

Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).
    for method RetrofitClient.type

is there a way to just add a @Ignore annotation and tell to retrofit ignores the methods that are not annotation with http verb annotations? something like the below

interface RetrofitClient : BaseApiClient {
    
    @Ignore // -> This is a new annotation
    override fun type() = EXTERNAL_ONE

    @POST("api/blah/blah")
    override fun create(
        @HeaderMap headers: Map<String, String>,
        @Body body: @JvmSuppressWildcards CreatClientRequestRestModel
    ): Mono<CreateClientResponseRestModel>

}
@JakeWharton
Copy link
Collaborator

Are you using -Xjvm-default=all in your Kotlin compiler flags? Retrofit should ignore any method that has a default implementation, but it needs to be a default as seen by Java's reflection. Since Kotlin used to target versions of the JVM older than 8, it has a compatibility mode for doing its default functions as rendered in bytecode.

You would also annotate each one with @JvmDefault, but that's a bit archaic nowadays with 8 being everyone's minimum (if not even higher).

@ghahramani
Copy link
Author

I used @JvmDefault but did not work and also it is deprecated, I did not use Xjvm-default=all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants