Skip to content

Add nullable struct support to BindAsync #35839

Closed
@halter73

Description

@halter73

Today, if you want to support an optional parameter with BindAsync, you cannot use a nullable struct. Instead you must use a reference type.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// cloudEvent will be inferred to come from a Json request body because no BindAsync method will
// be found on Nullable<CloudEventStruct> and RequestDelegateFactory doesn't try to unwrap the nullable.

app.MapGet("/webhook", (CloudEventStruct? cloudEvent) =>
{
    redis.Publish(cloudEvent);
});

app.Run();

public struct CloudEventStruct
{
	// This will NOT be matched because it returns returns a ValueTask<CloudEventStruct?>
    // instead of ValueTask<CloudEventStruct>
    public static async ValueTask<CloudEventStruct?> BindAsync(HttpContext context, ParameterInfo parameter)
    {
        return await CloudEventParser.ReadEventAsync(context, parameter.Name);
    }
}

This tracks adding support for nullable-struct parameters and nullable-struct-returning BindAsync methods.

Metadata

Metadata

Labels

Priority:2Work that is important, but not critical for the releasearea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions