Skip to content

Commit d526727

Browse files
committed
Do not throw when a status code with a codefix appears multiple times in the method body
Fixes #4480
1 parent b1f4a7b commit d526727

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/Mvc/src/Microsoft.AspNetCore.Mvc.Api.Analyzers/AddResponseTypeAttributeCodeFixAction.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ private static Dictionary<int, string> GetStatusCodeConstants(INamedTypeSymbol s
169169
}
170170

171171
var statusCode = metadata.IsDefaultResponse ? 200 : metadata.StatusCode;
172-
statusCodes.Add(statusCode, (statusCode, metadata.ReturnType));
172+
if (!statusCodes.ContainsKey(statusCode))
173+
{
174+
// If a status code appears multiple times in the actual metadata, pick the first one to
175+
// appear in the codefix
176+
statusCodes.Add(statusCode, (statusCode, metadata.ReturnType));
177+
}
173178
}
174179

175180
return statusCodes.Values;

src/Mvc/test/Mvc.Api.Analyzers.Test/AddResponseTypeAttributeCodeFixProviderIntegrationTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class AddResponseTypeAttributeCodeFixProviderIntegrationTest
5151
[Fact]
5252
public Task CodeFixAddsStatusCodesFromObjectInitializer() => RunTest();
5353

54+
[Fact]
55+
public Task CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError() => RunTest();
56+
5457
private async Task RunTest([CallerMemberName] string testMethod = "")
5558
{
5659
// Arrange
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Microsoft.AspNetCore.Mvc.Api.Analyzers._INPUT_
5+
{
6+
[ApiController]
7+
[Route("[controller]/[action]")]
8+
public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError : ControllerBase
9+
{
10+
public List<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel> Values { get; } =
11+
new List<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel>();
12+
13+
public ActionResult<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel> GetItem(int id)
14+
{
15+
if (id == 0)
16+
{
17+
return NotFound();
18+
}
19+
20+
var model = Values.FirstOrDefault(m => m.Id == id);
21+
if (model == null)
22+
{
23+
return NotFound();
24+
}
25+
26+
return model;
27+
}
28+
}
29+
30+
public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel
31+
{
32+
public int Id { get; set; }
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace Microsoft.AspNetCore.Mvc.Api.Analyzers._OUTPUT_
6+
{
7+
[ApiController]
8+
[Route("[controller]/[action]")]
9+
public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError : ControllerBase
10+
{
11+
public List<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel> Values { get; } =
12+
new List<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel>();
13+
14+
[ProducesResponseType(StatusCodes.Status200OK)]
15+
[ProducesResponseType(StatusCodes.Status404NotFound)]
16+
[ProducesDefaultResponseType]
17+
public ActionResult<CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel> GetItem(int id)
18+
{
19+
if (id == 0)
20+
{
21+
return NotFound();
22+
}
23+
24+
var model = Values.FirstOrDefault(m => m.Id == id);
25+
if (model == null)
26+
{
27+
return NotFound();
28+
}
29+
30+
return model;
31+
}
32+
}
33+
34+
public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel
35+
{
36+
public int Id { get; set; }
37+
}
38+
}

0 commit comments

Comments
 (0)