refit icon indicating copy to clipboard operation
refit copied to clipboard

feature: UrlAttribute for parameter

Open Paul-N opened this issue 3 years ago • 6 comments

Case Let's say we have a REST service and it's response contain a field with url of resource. For example an object that contain a collection and next page url (see https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md#981-server-driven-paging).

Solution It would be nice to have an attribute in REST interface method parameter for setting an url

interface IMyService
{
    [Get]
    Task<Response> GetCollectionByUrl([Url] string url);
}

You can see same attribute already implemented in Retrofit https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Url.html

What have I done I forked this repo and implemented this attribute support and I wrote one unit test for it. You can check it: https://github.com/Paul-N/refit/tree/feature/url_attribute

Paul-N avatar May 30 '22 07:05 Paul-N

Nice. Yeah I could see how this makes sense for that particular use case. Though from having added a new attribute to Refit before and it resulting in bugs I highly recommend adding very comprehensive unit test suite to make sure the other attributes still behave properly when they appear in conjunction with the new one. Unfortunately an early design decision in Refit to automagically infer a [Body] attribute if none is present on a POST request makes this part of the codebase prone to introducing bugs if you're not really careful because of how it tracks how many parameters the method signature has and works out whether or not one of them should be treated like a [Body].

james-s-tayler avatar Jul 14 '22 12:07 james-s-tayler

This is a working example:

    [Put("/{path}")]
    [QueryUriFormat(UriFormat.Unescaped)]
    public Task<string> SendStatusNotificationAsync(string path, StatusNotificationRequest request);

Then we need to have the path without the leading / as it will fail otherwise

hartmark avatar Nov 10 '22 11:11 hartmark

Any chance this feature will be in ongoing 7.0 version?

Paul-N avatar May 24 '23 15:05 Paul-N