Skip to content

RDF emits incorrect code when ThrowOnBadRequest is disabled with AsParameters and null binding #48547

Open
@captainsafia

Description

@captainsafia

Found a bug in RDF that occurs when the following code scenario is in play:

  • ThrowOnBadRequest is disabled
  • A parameter with AsParameters is used
  • The argument surrogate type contains a parameter that should be bound implicitly from the body

In that scenario, the following code is generated:

.Block(
    Microsoft.AspNetCore.Http.Generators.Tests.ParametersListWithImplicitFromBody $args_local,
    System.Boolean $wasParamCheckFailure) {
    .Block() {
        .If ($bodyValue == null) {
            .Block() {
                $wasParamCheckFailure = True;
                .Call Microsoft.AspNetCore.Http.RequestDelegateFactory+Log.ImplicitBodyNotProvided(
                    $httpContext,
                    "Todo",
                    False)
            }
        } .Else {
            .Default(System.Void)
        }
    };
    $args_local = .New Microsoft.AspNetCore.Http.Generators.Tests.ParametersListWithImplicitFromBody(
        $httpContext,
        (Microsoft.AspNetCore.Http.Generators.Tests.TodoStruct)$bodyValue);
    .If (
        $wasParamCheckFailure
    ) {
        .Block() {
            ($httpContext.Response).StatusCode = 400;
            System.Threading.Tasks.Task.CompletedTask
        }
    } .Else {
        .Block() {
            .Call TestMapActions.<MapTestEndpoints>g__TestAction|0_0($args_local);
            System.Threading.Tasks.Task.CompletedTask
        }
    }
}

Observe that if bodyValue evaluates to null, we log an exception related to the requiredness check but attempt to construct the argument surrogate type (ParametersListWithImplicitFromBody) anyways with a null value that throws an NRE.

When ThrowOnBadRequest is enabled, the call to ImplicitBodyNotProvider will throw an exception and not fall through to the buggy code.

We should be initialized args_local after the wasParamCheckFailure occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions