Skip to content

Commit

Permalink
Let the user know if a method has no attribute
Browse files Browse the repository at this point in the history
Throw a `NotImplementedException` when someone calls a method that we
won't have an implementation for.
  • Loading branch information
bennor committed Oct 31, 2014
1 parent aa6a39e commit 65e8a23
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ namespace {{Namespace}}
{{#MethodList}}
public virtual {{ReturnType}} {{Name}}({{ArgumentListWithTypes}})
{
{{#IsRefitMethod}}
var arguments = new object[] { {{ArgumentList}} };
return ({{ReturnType}}) methodImpls["{{Name}}"](Client, arguments);
{{/IsRefitMethod}}
{{^IsRefitMethod}}
throw new NotImplementedException("Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument.");
{{/IsRefitMethod}}
}

{{/MethodList}}
Expand Down
2 changes: 2 additions & 0 deletions InterfaceStubGenerator/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public ClassTemplateInfo GenerateClassInfoForInterface(InterfaceDeclarationSynta
.Select(y => y.Identifier.ValueText)),
ArgumentListWithTypes = String.Join(",", x.ParameterList.Parameters
.Select(y => String.Format("{0} {1}", y.Type.ToString(), y.Identifier.ValueText))),
IsRefitMethod = HasRefitHttpMethodAttribute(x)
})
.ToList();

Expand Down Expand Up @@ -155,6 +156,7 @@ public class MethodTemplateInfo
public string Name { get; set; }
public string ArgumentListWithTypes { get; set; }
public string ArgumentList { get; set; }
public bool IsRefitMethod { get; set; }
}

public class TemplateInformation
Expand Down
2 changes: 1 addition & 1 deletion Refit-Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void GenerateTemplateInfoForInterfaceListSmokeTest()
.ToList();

var result = fixture.GenerateTemplateInfoForInterfaceList(input);
Assert.AreEqual(3, result.ClassList.Count);
Assert.AreEqual(4, result.ClassList.Count);
}

[Test]
Expand Down
33 changes: 31 additions & 2 deletions Refit-Tests/RefitStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ public virtual Task AnotherRefitMethod()

public virtual Task NoConstantsAllowed()
{
var arguments = new object[] { };
return (Task) methodImpls["NoConstantsAllowed"](Client, arguments);
throw new NotImplementedException("Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument.");
}

}
Expand Down Expand Up @@ -211,4 +210,34 @@ public virtual Task Post()
}
}

namespace Refit.Tests
{
using RefitInternalGenerated;

[Preserve]
public partial class AutoGeneratedIAmHalfRefit : IAmHalfRefit
{
public HttpClient Client { get; protected set; }
readonly Dictionary<string, Func<HttpClient, object[], object>> methodImpls;

public AutoGeneratedIAmHalfRefit(HttpClient client, IRequestBuilder requestBuilder)
{
methodImpls = requestBuilder.InterfaceHttpMethods.ToDictionary(k => k, v => requestBuilder.BuildRestResultFuncForMethod(v));
Client = client;
}

public virtual Task Post()
{
var arguments = new object[] { };
return (Task) methodImpls["Post"](Client, arguments);
}

public virtual Task Get()
{
throw new NotImplementedException("Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument.");
}

}
}


19 changes: 19 additions & 0 deletions Refit-Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public interface INoRefitHereBuddy
Task Post();
}

public interface IAmHalfRefit
{
[Post("/anything")]
Task Post();

Task Get();
}

[TestFixture]
public class RestServiceIntegrationTests
{
Expand Down Expand Up @@ -189,5 +197,16 @@ public void NonRefitInterfacesThrowMeaningfulExceptions()
StringAssert.StartsWith("INoRefitHereBuddy", exception.Message);
}
}

[Test]
public async Task NonRefitMethodsThrowMeaningfulExceptions()
{
try {
var fixture = RestService.For<IAmHalfRefit>("http://example.com");
await fixture.Get();
} catch (NotImplementedException exception) {
StringAssert.Contains("no Refit HTTP method attribute", exception.Message);
}
}
}
}

0 comments on commit 65e8a23

Please sign in to comment.