Skip to content

Commit

Permalink
Ensure two interfaces with the same name do not cause compile errors (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
4 people authored Jun 9, 2024
1 parent b944483 commit 9c2caf3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
5 changes: 3 additions & 2 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
Expand Down Expand Up @@ -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<string>();
if (typeParameter.HasReferenceTypeConstraint)
Expand Down
12 changes: 12 additions & 0 deletions Refit.Tests/NamespaceCollisionApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ public static INamespaceCollisionApi Create()
namespace CollisionA
{
public class SomeType { }

public interface INamespaceCollisionApi
{
[Get("/")]
Task<SomeType> SomeRequest();
}
}

namespace CollisionB
{
public class SomeType { }

public interface INamespaceCollisionApi
{
[Get("/")]
Task<SomeType> SomeRequest();
}
}
81 changes: 61 additions & 20 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITypeCollisionApiA>(Url);
var fixtureA = RestService.For<ITypeCollisionApiA>(Url, settings);

var respA = await fixtureA.SomeARequest();
var respA = await fixtureA.SomeARequest();

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

var respB = await fixtureB.SomeBRequest();
var fixtureB = RestService.For<ITypeCollisionApiB>(Url, settings);

Assert.IsType<CollisionA.SomeType>(respA);
Assert.IsType<CollisionB.SomeType>(respB);
}
var respB = await fixtureB.SomeBRequest();

Assert.IsType<CollisionA.SomeType>(respA);
Assert.IsType<CollisionB.SomeType>(respB);
}

internal static Stream GetTestFileStream(string relativeFilePath)
{
Expand Down Expand Up @@ -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<INamespaceCollisionApi>(Url, settings);

var respA = await fixtureA.SomeRequest();

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

var fixtureB = RestService.For<CollisionA.INamespaceCollisionApi>(Url, settings);

var respB = await fixtureB.SomeRequest();

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

var fixtureC = RestService.For<CollisionB.INamespaceCollisionApi>(Url, settings);

var respC = await fixtureC.SomeRequest();

Assert.IsType<CollisionA.SomeType>(respA);
Assert.IsType<CollisionA.SomeType>(respB);
Assert.IsType<CollisionB.SomeType>(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);
}
}

0 comments on commit 9c2caf3

Please sign in to comment.