diff --git a/src/Mvc/Mvc.Api.Analyzers/src/AddResponseTypeAttributeCodeFixAction.cs b/src/Mvc/Mvc.Api.Analyzers/src/AddResponseTypeAttributeCodeFixAction.cs index e7601b6b7f..d138d6afb1 100644 --- a/src/Mvc/Mvc.Api.Analyzers/src/AddResponseTypeAttributeCodeFixAction.cs +++ b/src/Mvc/Mvc.Api.Analyzers/src/AddResponseTypeAttributeCodeFixAction.cs @@ -169,7 +169,12 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers } var statusCode = metadata.IsDefaultResponse ? 200 : metadata.StatusCode; - statusCodes.Add(statusCode, (statusCode, metadata.ReturnType)); + if (!statusCodes.ContainsKey(statusCode)) + { + // If a status code appears multiple times in the actual metadata, pick the first one to + // appear in the codefix + statusCodes.Add(statusCode, (statusCode, metadata.ReturnType)); + } } return statusCodes.Values; diff --git a/src/Mvc/Mvc.Api.Analyzers/test/AddResponseTypeAttributeCodeFixProviderIntegrationTest.cs b/src/Mvc/Mvc.Api.Analyzers/test/AddResponseTypeAttributeCodeFixProviderIntegrationTest.cs index f0cfbff1e5..9ba677d101 100644 --- a/src/Mvc/Mvc.Api.Analyzers/test/AddResponseTypeAttributeCodeFixProviderIntegrationTest.cs +++ b/src/Mvc/Mvc.Api.Analyzers/test/AddResponseTypeAttributeCodeFixProviderIntegrationTest.cs @@ -51,6 +51,9 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers [Fact] public Task CodeFixAddsStatusCodesFromObjectInitializer() => RunTest(); + [Fact] + public Task CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError() => RunTest(); + private async Task RunTest([CallerMemberName] string testMethod = "") { // Arrange diff --git a/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Input.cs b/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Input.cs new file mode 100644 index 0000000000..19d51ee710 --- /dev/null +++ b/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Input.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.AspNetCore.Mvc.Api.Analyzers._INPUT_ +{ + [ApiController] + [Route("[controller]/[action]")] + public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError : ControllerBase + { + public List Values { get; } = + new List(); + + public ActionResult GetItem(int id) + { + if (id == 0) + { + return NotFound(); + } + + var model = Values.FirstOrDefault(m => m.Id == id); + if (model == null) + { + return NotFound(); + } + + return model; + } + } + + public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel + { + public int Id { get; set; } + } +} diff --git a/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Output.cs b/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Output.cs new file mode 100644 index 0000000000..cd7b5dad58 --- /dev/null +++ b/src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Output.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Http; + +namespace Microsoft.AspNetCore.Mvc.Api.Analyzers._OUTPUT_ +{ + [ApiController] + [Route("[controller]/[action]")] + public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError : ControllerBase + { + public List Values { get; } = + new List(); + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesDefaultResponseType] + public ActionResult GetItem(int id) + { + if (id == 0) + { + return NotFound(); + } + + var model = Values.FirstOrDefault(m => m.Id == id); + if (model == null) + { + return NotFound(); + } + + return model; + } + } + + public class CodeFixWorksWhenMultipleIdenticalStatusCodesAreInErrorModel + { + public int Id { get; set; } + } +}