From 5814a036d93246dec28ac19e4c8b1db4dda7ba32 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 28 May 2019 10:45:59 -0700 Subject: [PATCH 01/12] 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 4298f63b2a4daf8dc14497a12295afd1143ef2f2 Mon Sep 17 00:00:00 2001 From: Isaac Levin Date: Tue, 4 Jun 2019 11:03:09 -0400 Subject: [PATCH 02/12] remove cookie consent from templates --- .../Pages/Shared/_CookieConsentPartial.cshtml | 25 ------------------- .../Pages/Shared/_Layout.cshtml | 6 ----- .../content/RazorPagesWeb-CSharp/Startup.cs | 7 ------ .../content/StarterWeb-CSharp/Startup.cs | 7 ------ .../Views/Shared/_CookieConsentPartial.cshtml | 25 ------------------- .../Views/Shared/_Layout.cshtml | 6 ----- .../Views/Shared/_CookieConsentPartial.cshtml | 25 ------------------- .../Views/Shared/_Layout.cshtml | 6 ----- .../test/template-baselines.json | 13 ---------- 9 files changed, 120 deletions(-) delete mode 100644 src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_CookieConsentPartial.cshtml delete mode 100644 src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Shared/_CookieConsentPartial.cshtml delete mode 100644 src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-FSharp/Views/Shared/_CookieConsentPartial.cshtml diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_CookieConsentPartial.cshtml b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_CookieConsentPartial.cshtml deleted file mode 100644 index 999a0ed5bf..0000000000 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_CookieConsentPartial.cshtml +++ /dev/null @@ -1,25 +0,0 @@ -@using Microsoft.AspNetCore.Http.Features - -@{ - var consentFeature = Context.Features.Get(); - var showBanner = !consentFeature?.CanTrack ?? false; - var cookieString = consentFeature?.CreateConsentCookie(); -} - -@if (showBanner) -{ - - -} diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_Layout.cshtml b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_Layout.cshtml index a02915b198..0e535e107d 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_Layout.cshtml +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Shared/_Layout.cshtml @@ -34,12 +34,6 @@ -
- -
- @RenderBody() -
-