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

Custom parameter annotations support #2890

Open
Blackhex opened this issue Sep 17, 2018 · 5 comments
Open

Custom parameter annotations support #2890

Blackhex opened this issue Sep 17, 2018 · 5 comments

Comments

@Blackhex
Copy link

I am writing a call adapter that would take response type and several custom annotated request parameters and transform them to a custom HTTP body. For example if user of my call adapter declares following request service:

class MyResponse(
  val id: Int,
  val name: String
)

interface MyRequestService {
  @POST("path")
  fun getFoo(@MyAnnotation1 bar1: Bar1, @MyAnnotation2 bar2: Bar2): Single<MyResponse>
}

my call adapter would prepare request that is equivalent to:

interface MyRequestService {
  @POST("path")
  fun getFoo(@Body body: MyBody): Single<MyResponse>
}

where MyBody is for example:

class MyBody(
  val what: String = "MyResponse(id(bar1), name)",
  val bar2: Bar2
)

without using my call adapter.

Looking into the code I have found out that there is currently not possible to have custom parameter annotations or any "adapter" that would transform custom annotations to Retrofit ones passing some information to the custom call adapter. Or am I wrong? Would it be possible to have such adapter?

@UglyBarnacle
Copy link

Same here.

Would like to have Custom annotations on parameters so I can merge them into a Body with a custom adapter.
Target is to provide an Interface where I can drop as many annotated Parameters so create an ugly SOAP-XML for some legacy endpoints to improve Response and Error-Handling.

Thought was maybe registering those annotations with the according mediatype to the Retrofit-Instance.

(And another point would be nice: reading annotations from the service-interface for example to have a global namespace added)

@msdx
Copy link

msdx commented Dec 21, 2018

Same needs.

@EastWoodYang
Copy link

same need.

@UpDream7
Copy link

Same needs.

@HylkeB
Copy link

HylkeB commented Sep 29, 2020

Same need, currently considering another solution, using the @tag annotation instead.

In my situation I implement my own Call.Factory where i get the OkHttp requests, for which I can access my custom method annotations by calling request.tag(Invocation::class.java). I wanted to also use custom parameter annotations, but then Retrofit throws an exception in class RequestFactory line 331 (version 2.6.2). So instead, a slight bit more ugly, I can use the @tag annotation to add whatever i want to the request. If I do @Tag customData: CustomData in my api interface method, i can extract it using request.tag(CustomData::class.java). (where customdata looks like this: class CustomData(val customData: String))

Instead I would rather use a custom annotation for this, that would be prettier: @CustomData customData: String, then I should be able to extract the required data from the Invocation (I assume, i cannot test this yet)

I think the solution would be to remove the line where the parameter error is thrown (RequestFactory.java:331). I'm not sure what other logic this would possibly break.

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

6 participants