Skip to content

Commit 2831dc0

Browse files
committed
Add test demonstrating the issue
1 parent 3b674da commit 2831dc0

File tree

3 files changed

+698
-2
lines changed

3 files changed

+698
-2
lines changed

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/OperationTests.Controllers.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class TestController : ControllerBase
124124
{
125125
/// <param name="userId">The id of the user.</param>
126126
[HttpGet("{userId}")]
127-
public string Get()
127+
public string Get(int userId)
128128
{
129129
return "Hello, World!";
130130
}
@@ -141,4 +141,49 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
141141
Assert.Equal("The id of the user.", path.Parameters[0].Description);
142142
});
143143
}
144+
145+
[Fact]
146+
public async Task SupportsRouteParametersWithCustomNamesFromControllers()
147+
{
148+
var source = """
149+
using Microsoft.AspNetCore.Builder;
150+
using Microsoft.AspNetCore.Mvc;
151+
using Microsoft.Extensions.DependencyInjection;
152+
153+
var builder = WebApplication.CreateBuilder();
154+
155+
builder.Services
156+
.AddControllers()
157+
.AddApplicationPart(typeof(TestController).Assembly);
158+
builder.Services.AddOpenApi();
159+
160+
var app = builder.Build();
161+
162+
app.MapControllers();
163+
164+
app.Run();
165+
166+
[ApiController]
167+
[Route("[controller]")]
168+
public class TestController : ControllerBase
169+
{
170+
/// <param name="userId">The id of the user.</param>
171+
[HttpGet("{user_id}")]
172+
public string Get([FromRoute(Name = "user_id")] int userId)
173+
{
174+
return "Hello, World!";
175+
}
176+
}
177+
178+
public partial class Program {}
179+
180+
""";
181+
var generator = new XmlCommentGenerator();
182+
await SnapshotTestHelper.Verify(source, generator, out var compilation);
183+
await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
184+
{
185+
var path = document.Paths["/Test/{user_id}"].Operations[HttpMethod.Get];
186+
Assert.Equal("The id of the user.", path.Parameters[0].Description);
187+
});
188+
}
144189
}

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/OperationTests.SupportsRouteParametersFromControllers#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static Dictionary<string, XmlComment> GenerateCacheEntries()
7171
{
7272
var cache = new Dictionary<string, XmlComment>();
7373

74-
cache.Add(@"M:TestController.Get", new XmlComment(null, null, null, null, null, false, null, [new XmlParameterComment(@"userId", @"The id of the user.", null, false)], null));
74+
cache.Add(@"M:TestController.Get(System.Int32)", new XmlComment(null, null, null, null, null, false, null, [new XmlParameterComment(@"userId", @"The id of the user.", null, false)], null));
7575

7676
return cache;
7777
}

0 commit comments

Comments
 (0)