From 5814a036d93246dec28ac19e4c8b1db4dda7ba32 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 28 May 2019 10:45:59 -0700 Subject: [PATCH 1/2] Port do not throw when a status code with a codefix appears multiple times in the method body (#10235) Fixes https://github.com/aspnet/AspNetCore/issues/4480 --- .../AddResponseTypeAttributeCodeFixAction.cs | 7 +++- ...AttributeCodeFixProviderIntegrationTest.cs | 3 ++ ...pleIdenticalStatusCodesAreInError.Input.cs | 34 +++++++++++++++++ ...leIdenticalStatusCodesAreInError.Output.cs | 38 +++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Input.cs create mode 100644 src/Mvc/Mvc.Api.Analyzers/test/TestFiles/AddResponseTypeAttributeCodeFixProviderIntegrationTest/CodeFixWorksWhenMultipleIdenticalStatusCodesAreInError.Output.cs 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; } + } +} From 99a12a6882563fc20b62adc4be7cb33aada73781 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 4 Jun 2019 12:42:05 -0700 Subject: [PATCH 2/2] Add Mvc.Api.Analyzers to 2.2.6 patch (#10621) --- eng/PatchConfig.props | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/eng/PatchConfig.props b/eng/PatchConfig.props index 7f0ab92b18..8dab2675a7 100644 --- a/eng/PatchConfig.props +++ b/eng/PatchConfig.props @@ -66,5 +66,10 @@ Later on, this will be checked using this condition: Microsoft.AspNetCore.AzureAppServices.SiteExtension; + + + Microsoft.AspNetCore.Mvc.Api.Analyzers; + +