From ce20d732df2cc9cfe226a118b979750a8352a7a3 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Mon, 4 Mar 2024 12:25:44 -0800 Subject: [PATCH] Fix nullability context handling in RDG --- .../StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs | 2 +- ...pAction_ImplicitQuery_NullableStringArrayParam.generated.txt | 2 +- ...uery_NullableStringArrayParam_EmptyQueryValues.generated.txt | 2 +- ...Query_NullableStringArrayParam_QueryNotPresent.generated.txt | 2 +- .../MapAction_ImplicitQuery_StringArrayParam.generated.txt | 2 +- ...on_JsonBodyOrService_HandlesBothJsonAndService.generated.txt | 2 +- ...Action_TakesCustomMetadataEmitter_Has_Metadata.generated.txt | 2 +- ...hArrayQueryString_AndBody_ShouldUseQueryString.generated.txt | 2 +- ...hArrayQueryString_AndBody_ShouldUseQueryString.generated.txt | 2 +- ...Put_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt | 2 +- ...ost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt | 2 +- ...ost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt | 2 +- .../MapPost_WithArrayQueryString_ShouldFail.generated.txt | 2 +- .../test/RequestDelegateGenerator/CompileTimeCreationTests.cs | 1 + .../RequestDelegateGenerator/RequestDelegateCreationTestBase.cs | 2 +- 15 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs b/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs index c7c4619d6939..749853dcb5de 100644 --- a/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs +++ b/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs @@ -325,7 +325,7 @@ public static void EmitJsonAcceptsMetadata(this Endpoint endpoint, CodeWriter co { codeWriter.WriteLine($$"""({{(parameter.IsOptional ? "true" : "false")}}, typeof({{parameter.Type.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability)}})),"""); } - codeWriter.WriteLine("#nullable restore"); + codeWriter.WriteLine("#nullable enable"); codeWriter.Indent--; codeWriter.WriteLine("};"); codeWriter.WriteLine("foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)"); diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam.generated.txt index c1b5d54296e6..40169b4a0c6c 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_EmptyQueryValues.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_EmptyQueryValues.generated.txt index c1b5d54296e6..40169b4a0c6c 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_EmptyQueryValues.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_EmptyQueryValues.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_QueryNotPresent.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_QueryNotPresent.generated.txt index c1b5d54296e6..40169b4a0c6c 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_QueryNotPresent.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_NullableStringArrayParam_QueryNotPresent.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_StringArrayParam.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_StringArrayParam.generated.txt index 37e0b8e02588..bb046dbb648d 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_StringArrayParam.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_ImplicitQuery_StringArrayParam.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_JsonBodyOrService_HandlesBothJsonAndService.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_JsonBodyOrService_HandlesBothJsonAndService.generated.txt index 45c7af813c21..1213509d9ad8 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_JsonBodyOrService_HandlesBothJsonAndService.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_JsonBodyOrService_HandlesBothJsonAndService.generated.txt @@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Http.Generated #nullable disable (false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.Todo)), (false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.TestService)), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_TakesCustomMetadataEmitter_Has_Metadata.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_TakesCustomMetadataEmitter_Has_Metadata.generated.txt index a8347fe82216..ddf7b26a6f3e 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_TakesCustomMetadataEmitter_Has_Metadata.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapAction_TakesCustomMetadataEmitter_Has_Metadata.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.CustomMetadataEmitter)), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Get_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Get_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt index a5f52d7c7abb..cd52ab5392d1 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Get_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Get_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndGet_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndGet_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt index a5f52d7c7abb..cd52ab5392d1 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndGet_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndGet_WithArrayQueryString_AndBody_ShouldUseQueryString.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndPut_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndPut_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt index a5f52d7c7abb..cd52ab5392d1 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndPut_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_PostAndPut_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Post_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Post_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt index a5f52d7c7abb..cd52ab5392d1 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Post_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapMethods_Post_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt index edc3fba115df..399fb03f619d 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_AndBody_ShouldUseBody.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_ShouldFail.generated.txt b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_ShouldFail.generated.txt index edc08f6ef942..ddad6697ab20 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_ShouldFail.generated.txt +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/Baselines/MapPost_WithArrayQueryString_ShouldFail.generated.txt @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Http.Generated var jsonBodyOrServiceTypeTuples = new (bool, Type)[] { #nullable disable (false, typeof(global::System.String[])), - #nullable restore + #nullable enable }; foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples) { diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs b/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs index 712b01c2e906..e6e66a302b73 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs @@ -672,6 +672,7 @@ public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder app, int public async Task TestHandlingOfGenericWithNullableReferenceTypes() { var source = """ +#nullable enable using System; using System.Globalization; using Microsoft.AspNetCore.Builder; diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs b/src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs index 350679dad6c9..8906f9125436 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs @@ -309,7 +309,7 @@ internal static Project CreateProject(Func