Skip to content

Commit 9c2caf3

Browse files
dtewinkelDaniël te WinkelglennawatsonChrisPulman
authored
Ensure two interfaces with the same name do not cause compile errors (#1542)
* Fix #1261 * Revert accidental change. Fix unit tests on difference in extra spaces in output. --------- Co-authored-by: Daniël te Winkel <live@twia.nl> Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com> Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
1 parent b944483 commit 9c2caf3

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ public static void Initialize()
291291
);
292292

293293
var keyName = group.Key.Name;
294-
if (keyCount.TryGetValue(keyName, out var value))
294+
int value;
295+
while(keyCount.TryGetValue(keyName, out value))
295296
{
296297
keyName = $"{keyName}{++value}";
297298
}
@@ -583,7 +584,7 @@ static void WriteConstraitsForTypeParameter(
583584
bool isOverrideOrExplicitImplementation
584585
)
585586
{
586-
// Explicit interface implementations and ovverrides can only have class or struct constraints
587+
// Explicit interface implementations and overrides can only have class or struct constraints
587588

588589
var parameters = new List<string>();
589590
if (typeParameter.HasReferenceTypeConstraint)

Refit.Tests/NamespaceCollisionApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ public static INamespaceCollisionApi Create()
2323
namespace CollisionA
2424
{
2525
public class SomeType { }
26+
27+
public interface INamespaceCollisionApi
28+
{
29+
[Get("/")]
30+
Task<SomeType> SomeRequest();
31+
}
2632
}
2733

2834
namespace CollisionB
2935
{
3036
public class SomeType { }
37+
38+
public interface INamespaceCollisionApi
39+
{
40+
[Get("/")]
41+
Task<SomeType> SomeRequest();
42+
}
3143
}

Refit.Tests/RestService.cs

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,28 +2199,31 @@ public void NonGenericCreate()
21992199
Assert.Equal(fixture.Client.BaseAddress.AbsoluteUri, expectedBaseAddress);
22002200
}
22012201

2202-
[Fact]
2203-
public async Task TypeCollisionTest()
2204-
{
2205-
var mockHttp = new MockHttpMessageHandler();
2202+
[Fact]
2203+
public async Task TypeCollisionTest()
2204+
{
2205+
var mockHttp = new MockHttpMessageHandler();
22062206

2207-
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, };
2207+
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, };
22082208

2209-
const string Url = "https://httpbin.org/get";
2209+
const string Url = "https://httpbin.org/get";
22102210

2211-
mockHttp.Expect(HttpMethod.Get, Url).Respond("application/json", "{ }");
2211+
mockHttp.Expect(HttpMethod.Get, Url).Respond("application/json", "{ }");
22122212

2213-
var fixtureA = RestService.For<ITypeCollisionApiA>(Url);
2213+
var fixtureA = RestService.For<ITypeCollisionApiA>(Url, settings);
22142214

2215-
var respA = await fixtureA.SomeARequest();
2215+
var respA = await fixtureA.SomeARequest();
22162216

2217-
var fixtureB = RestService.For<ITypeCollisionApiB>(Url);
2217+
mockHttp.Expect(HttpMethod.Get, Url)
2218+
.Respond("application/json", "{ }");
22182219

2219-
var respB = await fixtureB.SomeBRequest();
2220+
var fixtureB = RestService.For<ITypeCollisionApiB>(Url, settings);
22202221

2221-
Assert.IsType<CollisionA.SomeType>(respA);
2222-
Assert.IsType<CollisionB.SomeType>(respB);
2223-
}
2222+
var respB = await fixtureB.SomeBRequest();
2223+
2224+
Assert.IsType<CollisionA.SomeType>(respA);
2225+
Assert.IsType<CollisionB.SomeType>(respB);
2226+
}
22242227

22252228
internal static Stream GetTestFileStream(string relativeFilePath)
22262229
{
@@ -2261,10 +2264,48 @@ internal static Stream GetTestFileStream(string relativeFilePath)
22612264
return stream;
22622265
}
22632266

2264-
public void AssertFirstLineContains(string expectedSubstring, string actualString)
2265-
{
2266-
var eolIndex = actualString.IndexOf('\n');
2267-
var firstLine = eolIndex < 0 ? actualString : actualString.Substring(0, eolIndex);
2268-
Assert.Contains(expectedSubstring, firstLine);
2269-
}
2267+
[Fact]
2268+
public async Task SameTypeNameInMultipleNamespacesTest()
2269+
{
2270+
var mockHttp = new MockHttpMessageHandler();
2271+
2272+
var settings = new RefitSettings
2273+
{
2274+
HttpMessageHandlerFactory = () => mockHttp,
2275+
};
2276+
2277+
const string Url = "https://httpbin.org/get";
2278+
2279+
mockHttp.Expect(HttpMethod.Get, Url + "/")
2280+
.Respond("application/json", "{ }");
2281+
2282+
var fixtureA = RestService.For<INamespaceCollisionApi>(Url, settings);
2283+
2284+
var respA = await fixtureA.SomeRequest();
2285+
2286+
mockHttp.Expect(HttpMethod.Get, Url + "/")
2287+
.Respond("application/json", "{ }");
2288+
2289+
var fixtureB = RestService.For<CollisionA.INamespaceCollisionApi>(Url, settings);
2290+
2291+
var respB = await fixtureB.SomeRequest();
2292+
2293+
mockHttp.Expect(HttpMethod.Get, Url + "/")
2294+
.Respond("application/json", "{ }");
2295+
2296+
var fixtureC = RestService.For<CollisionB.INamespaceCollisionApi>(Url, settings);
2297+
2298+
var respC = await fixtureC.SomeRequest();
2299+
2300+
Assert.IsType<CollisionA.SomeType>(respA);
2301+
Assert.IsType<CollisionA.SomeType>(respB);
2302+
Assert.IsType<CollisionB.SomeType>(respC);
2303+
}
2304+
2305+
public void AssertFirstLineContains(string expectedSubstring, string actualString)
2306+
{
2307+
var eolIndex = actualString.IndexOf('\n');
2308+
var firstLine = eolIndex < 0 ? actualString : actualString.Substring(0, eolIndex);
2309+
Assert.Contains(expectedSubstring, firstLine);
2310+
}
22702311
}

0 commit comments

Comments
 (0)