Skip to content

Non-primitive value type as endpoint parameter can't be optional #64495

@kemocca

Description

@kemocca

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Any non-nullable non-primitive value type parameter in endpoint declaration can not be optional.

Expected Behavior

Parameter must be just optional and have default value if not specified.

Steps To Reproduce

Map endpoint like that:

app.MapGet("/test", ([FromQuery] Guid optionalGuid = default) => { });

Exceptions (if any)

System.ArgumentException: "Argument types do not match":

System.Linq.Expressions.Expression.Constant(object, System.Type)
    Microsoft.AspNetCore.Http.RequestDelegateFactory.BindParameterFromValue(System.Reflection.ParameterInfo, System.Linq.Expressions.Expression, Microsoft.AspNetCore.Http.RequestDelegateFactoryContext, string)
    Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgument(System.Reflection.ParameterInfo, Microsoft.AspNetCore.Http.RequestDelegateFactoryContext)
    Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArguments(System.Reflection.ParameterInfo[], Microsoft.AspNetCore.Http.RequestDelegateFactoryContext)
    Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateArgumentsAndInferMetadata(System.Reflection.MethodInfo, Microsoft.AspNetCore.Http.RequestDelegateFactoryContext)
    Microsoft.AspNetCore.Http.RequestDelegateFactory.InferMetadata(System.Reflection.MethodInfo, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions)
    Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(Microsoft.AspNetCore.Routing.RouteEndpointDataSource.RouteEntry, Microsoft.AspNetCore.Routing.Patterns.RoutePattern, System.Collections.Generic.IReadOnlyList<System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder>>, System.Collections.Generic.IReadOnlyList<System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder>>)
    Microsoft.AspNetCore.Routing.RouteEndpointDataSource.Endpoints.get()
    Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized()
    Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized()
    ...

.NET Version

8.0.22

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions