Skip to content

Commit 56d7bcd

Browse files
feat: generate code for derived non refit methods and update tests. (#1875)
1 parent a01cb84 commit 56d7bcd

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

InterfaceStubGenerator.Shared/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool nullableEnabled
300300
.Where(m => IsRefitMethod(m, httpMethodBaseAttributeSymbol))
301301
.ToArray();
302302
var derivedNonRefitMethods = derivedMethods
303-
.Except(derivedMethods, SymbolEqualityComparer.Default)
303+
.Except(derivedRefitMethods, SymbolEqualityComparer.Default)
304304
.Cast<IMethodSymbol>()
305305
.ToArray();
306306

Refit.GeneratorTests/Incremental/InheritanceTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public interface IGitHubApi : IDisposable
7474
[Fact]
7575
public void InheritFromInterfaceDoesRegenerate()
7676
{
77-
// TODO: this currently generates invalid code see issue #1801 for more information
7877
var syntaxTree = CSharpSyntaxTree.ParseText(TwoInterface, CSharpParseOptions.Default);
7978
var compilation1 = Fixture.CreateLibrary(syntaxTree);
8079

@@ -94,6 +93,6 @@ public interface IGitHubApi : IBaseInterface
9493
"""
9594
);
9695
var driver2 = driver1.RunGenerators(compilation2);
97-
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.Cached);
96+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.Modified);
9897
}
9998
}

Refit.GeneratorTests/InterfaceTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public interface IBaseInterface
4040
[Fact]
4141
public Task RefitInterfaceDerivedFromBaseTest()
4242
{
43-
// TODO: this currently generates invalid code see issue #1801 for more information
4443
return Fixture.VerifyForType(
4544
"""
4645
public interface IGeneratedInterface : IBaseInterface

Refit.GeneratorTests/_snapshots/InterfaceTests.RefitInterfaceDerivedFromBaseTest#IGeneratedInterface.g.verified.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public RefitGeneratorTestIGeneratedInterface(global::System.Net.Http.HttpClient
4646

4747
return await ((global::System.Threading.Tasks.Task<string>)______func(this.Client, ______arguments)).ConfigureAwait(false);
4848
}
49+
50+
/// <inheritdoc />
51+
void global::RefitGeneratorTest.IBaseInterface.NonRefitMethod()
52+
{
53+
throw new global::System.NotImplementedException("Either this method has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument.");
54+
}
4955
}
5056
}
5157
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
{
6+
void NonRefitMethod();
7+
^^^^^^^^^^^^^^
8+
}
9+
*/
10+
: (19,9)-(19,23),
11+
Message: Method IBaseInterface.NonRefitMethod either has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument,
12+
Severity: Warning,
13+
WarningLevel: 1,
14+
Descriptor: {
15+
Id: RF001,
16+
Title: Refit types must have Refit HTTP method attributes,
17+
MessageFormat: Method {0}.{1} either has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument,
18+
Category: Refit,
19+
DefaultSeverity: Warning,
20+
IsEnabledByDefault: true
21+
}
22+
}
23+
]
24+
}

Refit.Tests/InheritedInterfacesApi.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public interface IImplementTheInterfaceAndUseRefit : IAmInterfaceEWithNoRefit<in
6666
[Get("/DoSomethingElse")]
6767
public new Task DoSomethingElse();
6868
}
69+
70+
public interface IImplementTheInterfaceAndDontUseRefit : IAmInterfaceD
71+
{
72+
#pragma warning disable CS0108 // Member hides inherited member; missing new keyword
73+
Task<string> Test();
74+
#pragma warning restore CS0108 // Member hides inherited member; missing new keyword
75+
}
76+
6977
public interface IMyClient
7078
{
7179
[Get("/")]

Refit.Tests/RestService.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,19 +1974,43 @@ public async Task InheritedInterfaceWithoutRefitInBaseMethodsTest()
19741974
await fixture.DoSomethingElse();
19751975
mockHttp.VerifyNoOutstandingExpectation();
19761976

1977-
mockHttp
1978-
.Expect(HttpMethod.Get, "https://httpbin.org/DoSomethingElse")
1979-
.Respond("application/json", nameof(IImplementTheInterfaceAndUseRefit.DoSomethingElse));
1980-
await ((IAmInterfaceEWithNoRefit<int>)fixture).DoSomethingElse();
1977+
// base non refit method should throw NotImplementedException
1978+
await Assert.ThrowsAsync<NotImplementedException>(
1979+
() => ((IAmInterfaceEWithNoRefit<int>)fixture).DoSomethingElse()
1980+
);
19811981
mockHttp.VerifyNoOutstandingExpectation();
1982+
}
1983+
1984+
[Fact]
1985+
public async Task InheritedInterfaceWithoutRefitMethodsOverrideBaseTest()
1986+
{
1987+
var mockHttp = new MockHttpMessageHandler();
1988+
1989+
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp };
19821990

1983-
Assert.Throws<InvalidOperationException>(
1984-
() => RestService.For<IAmInterfaceEWithNoRefit<int>>("https://httpbin.org")
1991+
var fixture = RestService.For<IImplementTheInterfaceAndDontUseRefit>(
1992+
"https://httpbin.org",
1993+
settings
19851994
);
1995+
1996+
// inherited non refit method should throw NotImplementedException
1997+
await Assert.ThrowsAsync<NotImplementedException>(
1998+
() => fixture.Test()
1999+
);
2000+
mockHttp.VerifyNoOutstandingExpectation();
2001+
2002+
// base Refit method should respond
2003+
mockHttp
2004+
.Expect(HttpMethod.Get, "https://httpbin.org/get")
2005+
.WithQueryString("result", "Test")
2006+
.Respond("application/json", nameof(IAmInterfaceD.Test));
2007+
2008+
await ((IAmInterfaceD)fixture).Test();
2009+
mockHttp.VerifyNoOutstandingExpectation();
19862010
}
19872011

19882012
[Fact]
1989-
public async Task DictionaryDynamicQueryparametersTest()
2013+
public async Task DictionaryDynamicQueryParametersTest()
19902014
{
19912015
var mockHttp = new MockHttpMessageHandler();
19922016

0 commit comments

Comments
 (0)