refit icon indicating copy to clipboard operation
refit copied to clipboard

Support inheriting from HttpMethodAttribute's children

Open JarkoDubbeldam opened this issue 7 years ago • 3 comments

Is your feature request related to a problem? Please describe.

I currently have an attribute that inherits from PostAttribute, that does some additional work to set its path property. The problem with that is that the codegeneration tool that implements the interface looks for a PostAttribute (or another of the HttpMethodAttribute-family), and thus skips over the interface altogether. Then at runtime resolving the interface will fail and throw an exception.

Describe the solution you'd like

Preferably, the code generation would use semantic analysis to check whether an interface has attributes that inherit from HttpMethodAttribute, instead of looking for the syntax tokens.

There are currently two places in the code where this would fail:

  • https://github.com/reactiveui/refit/blob/master/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs#L77: Here there is a check for the Using Refit directive. If the interface has attributes that inherit from refit-attributes, they are likely in a different namespace.
  • https://github.com/reactiveui/refit/blob/master/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs#L332: This method would need to take semantics into account when deciding whether an attribute is interesting or not.

Describe alternatives you've considered

If the adjustment of the code-generation is not feasible, it would maybe be nice to mark the HttpMethodAttribute's children as sealed, so that it is clear that inheriting from them is not supported by the library.

Describe suggestions on how to achieve the feature

I don't think this feature is possible to achieve with just syntax analysis. I am not experienced enough to know how feasible this is with semantic analysis added.

It appears from debugging that this feature shouldn't affect the Refit project itself. When resolving the interface at runtime, the code works fine up to the point that the generated type is needed, which then fails because the code wasn't generated.

Additional context

  public class LazyPostAttribute : PostAttribute {
    public LazyPostAttribute([CallerMemberName] string methodName = null) : base("/" + methodName) {
    }
  }


  public interface IPostInterface {
    [Post("/SomePostMethod")]
    Task SomePostMethod([Body] object input);
  }

  public interface ILazyPostInterface {
    [LazyPost]
    Task SomePostMethod([Body] object input);
  }

    static void Main(string[] args) {
      var postInterface = RestService.For<IPostInterface>("http://localhost:8080/"); // Works
      var lazyPostInterface = RestService.For<ILazyPostInterface>("http://localhost:8080/"); // Does not work
    }

JarkoDubbeldam avatar May 02 '19 07:05 JarkoDubbeldam

Should be implemented in #1029

clairernovotny avatar Jan 23 '21 21:01 clairernovotny

Please try v6.0-preview.84 and file bugs as you come across them.

clairernovotny avatar Jan 23 '21 23:01 clairernovotny

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Apr 14 '23 00:04 github-actions[bot]