dotnet / dotnet/aspnetcore

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

Open
#48,547 0 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-rdf
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

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.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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.