Skip to content

Fix up formatting for generic types with nullable args #53049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,12 @@ public static void EmitJsonAcceptsMetadata(this Endpoint endpoint, CodeWriter co

codeWriter.WriteLine("var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {");
codeWriter.Indent++;
codeWriter.WriteLine("#nullable disable");
foreach (var parameter in potentialImplicitBodyParameters)
{
codeWriter.WriteLine($$"""({{(parameter.IsOptional ? "true" : "false")}}, typeof({{parameter.Type.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability)}})),""");
}
codeWriter.WriteLine("#nullable restore");
codeWriter.Indent--;
codeWriter.WriteLine("};");
codeWriter.WriteLine("foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.Todo)),
(false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.TestService)),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::Microsoft.AspNetCore.Http.Generators.Tests.CustomMetadataEmitter)),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Http.Generated
file static class GeneratedRouteBuilderExtensionsCore
{
private static readonly JsonOptions FallbackJsonOptions = new();

[InterceptsLocation(@"TestMapActions.cs", 26, 13)]
internal static RouteHandlerBuilder MapMethods0(
this IEndpointRouteBuilder endpoints,
Expand All @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Http.Generated
file static class GeneratedRouteBuilderExtensionsCore
{
private static readonly JsonOptions FallbackJsonOptions = new();

[InterceptsLocation(@"TestMapActions.cs", 26, 13)]
internal static RouteHandlerBuilder MapMethods0(
this IEndpointRouteBuilder endpoints,
Expand All @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Http.Generated
file static class GeneratedRouteBuilderExtensionsCore
{
private static readonly JsonOptions FallbackJsonOptions = new();

[InterceptsLocation(@"TestMapActions.cs", 26, 13)]
internal static RouteHandlerBuilder MapMethods0(
this IEndpointRouteBuilder endpoints,
Expand All @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Http.Generated
file static class GeneratedRouteBuilderExtensionsCore
{
private static readonly JsonOptions FallbackJsonOptions = new();

[InterceptsLocation(@"TestMapActions.cs", 26, 13)]
internal static RouteHandlerBuilder MapMethods0(
this IEndpointRouteBuilder endpoints,
Expand All @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ namespace Microsoft.AspNetCore.Http.Generated
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;
var serviceProviderIsService = serviceProvider.GetRequiredService<IServiceProviderIsService>();
var jsonBodyOrServiceTypeTuples = new (bool, Type)[] {
#nullable disable
(false, typeof(global::System.String[])),
#nullable restore
};
foreach (var (isOptional, type) in jsonBodyOrServiceTypeTuples)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,58 @@ public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder app, int
var result = Assert.IsType<GeneratorRunResult>(Assert.Single(generatorRunResult.Results));
Assert.Empty(GetStaticEndpoints(result, GeneratorSteps.EndpointModelStep));
}

[Fact]
public async Task TestHandlingOfGenericWithNullableReferenceTypes()
{
var source = """
using System;
using System.Globalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;

namespace TestApp
{
public static class TestMapActions
{
public static IEndpointRouteBuilder MapTestEndpoints(this IEndpointRouteBuilder app)
{
app.MapGet("/", (IGenericService<SomeInput, string?> service)
=> "Maybe? " + service.Get(new SomeInput(Random.Shared.Next())));
return app;
}
}

public interface IInputConstraint<TOutput>;

public interface IGenericService<TInput, TOutput> where TInput : IInputConstraint<TOutput>
{
TOutput Get(TInput input);
}

public record SomeInput(int Value) : IInputConstraint<string?>;

public class ConcreteService : IGenericService<SomeInput, string?>
{
public string? Get(SomeInput input) => input.Value % 2 == 0 ? input.Value.ToString(CultureInfo.InvariantCulture) : null;
}
}
""";
var project = CreateProject();
project = project.AddDocument("TestMapActions.cs", SourceText.From(source, Encoding.UTF8)).Project;
var compilation = await project.GetCompilationAsync();

var generator = new RequestDelegateGenerator.RequestDelegateGenerator().AsSourceGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generators: new[]
{
generator
},
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true),
parseOptions: ParseOptions);
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation,
out var _);

var diagnostics = updatedCompilation.GetDiagnostics();
Assert.Empty(diagnostics.Where(d => d.Severity >= DiagnosticSeverity.Warning));
}
}