diff --git a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs index a792f35fc..185ec6cfd 100644 --- a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs +++ b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs @@ -291,7 +291,8 @@ public static void Initialize() ); var keyName = group.Key.Name; - if (keyCount.TryGetValue(keyName, out var value)) + int value; + while(keyCount.TryGetValue(keyName, out value)) { keyName = $"{keyName}{++value}"; } @@ -583,7 +584,7 @@ static void WriteConstraitsForTypeParameter( bool isOverrideOrExplicitImplementation ) { - // Explicit interface implementations and ovverrides can only have class or struct constraints + // Explicit interface implementations and overrides can only have class or struct constraints var parameters = new List(); if (typeParameter.HasReferenceTypeConstraint) diff --git a/Refit.Tests/NamespaceCollisionApi.cs b/Refit.Tests/NamespaceCollisionApi.cs index 4649bf547..9930a2ad7 100644 --- a/Refit.Tests/NamespaceCollisionApi.cs +++ b/Refit.Tests/NamespaceCollisionApi.cs @@ -23,9 +23,21 @@ public static INamespaceCollisionApi Create() namespace CollisionA { public class SomeType { } + + public interface INamespaceCollisionApi + { + [Get("/")] + Task SomeRequest(); + } } namespace CollisionB { public class SomeType { } + + public interface INamespaceCollisionApi + { + [Get("/")] + Task SomeRequest(); + } } diff --git a/Refit.Tests/RestService.cs b/Refit.Tests/RestService.cs index 9696f251d..d334059a4 100644 --- a/Refit.Tests/RestService.cs +++ b/Refit.Tests/RestService.cs @@ -2199,28 +2199,31 @@ public void NonGenericCreate() Assert.Equal(fixture.Client.BaseAddress.AbsoluteUri, expectedBaseAddress); } - [Fact] - public async Task TypeCollisionTest() - { - var mockHttp = new MockHttpMessageHandler(); + [Fact] + public async Task TypeCollisionTest() + { + var mockHttp = new MockHttpMessageHandler(); - var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, }; - const string Url = "https://httpbin.org/get"; + const string Url = "https://httpbin.org/get"; - mockHttp.Expect(HttpMethod.Get, Url).Respond("application/json", "{ }"); + mockHttp.Expect(HttpMethod.Get, Url).Respond("application/json", "{ }"); - var fixtureA = RestService.For(Url); + var fixtureA = RestService.For(Url, settings); - var respA = await fixtureA.SomeARequest(); + var respA = await fixtureA.SomeARequest(); - var fixtureB = RestService.For(Url); + mockHttp.Expect(HttpMethod.Get, Url) + .Respond("application/json", "{ }"); - var respB = await fixtureB.SomeBRequest(); + var fixtureB = RestService.For(Url, settings); - Assert.IsType(respA); - Assert.IsType(respB); - } + var respB = await fixtureB.SomeBRequest(); + + Assert.IsType(respA); + Assert.IsType(respB); + } internal static Stream GetTestFileStream(string relativeFilePath) { @@ -2261,10 +2264,48 @@ internal static Stream GetTestFileStream(string relativeFilePath) return stream; } - public void AssertFirstLineContains(string expectedSubstring, string actualString) - { - var eolIndex = actualString.IndexOf('\n'); - var firstLine = eolIndex < 0 ? actualString : actualString.Substring(0, eolIndex); - Assert.Contains(expectedSubstring, firstLine); - } + [Fact] + public async Task SameTypeNameInMultipleNamespacesTest() + { + var mockHttp = new MockHttpMessageHandler(); + + var settings = new RefitSettings + { + HttpMessageHandlerFactory = () => mockHttp, + }; + + const string Url = "https://httpbin.org/get"; + + mockHttp.Expect(HttpMethod.Get, Url + "/") + .Respond("application/json", "{ }"); + + var fixtureA = RestService.For(Url, settings); + + var respA = await fixtureA.SomeRequest(); + + mockHttp.Expect(HttpMethod.Get, Url + "/") + .Respond("application/json", "{ }"); + + var fixtureB = RestService.For(Url, settings); + + var respB = await fixtureB.SomeRequest(); + + mockHttp.Expect(HttpMethod.Get, Url + "/") + .Respond("application/json", "{ }"); + + var fixtureC = RestService.For(Url, settings); + + var respC = await fixtureC.SomeRequest(); + + Assert.IsType(respA); + Assert.IsType(respB); + Assert.IsType(respC); + } + + public void AssertFirstLineContains(string expectedSubstring, string actualString) + { + var eolIndex = actualString.IndexOf('\n'); + var firstLine = eolIndex < 0 ? actualString : actualString.Substring(0, eolIndex); + Assert.Contains(expectedSubstring, firstLine); + } }