Support optionality via nullability and default values#34505
Conversation
1eb4b65 to
c746a3a
Compare
| factoryContext.JsonRequestBodyType = parameter.ParameterType; | ||
| factoryContext.AllowEmptyRequestBody = allowEmpty || isOptional; | ||
|
|
||
| var argument = Expression.Variable(parameter.ParameterType, $"{parameter.Name}_local"); |
There was a problem hiding this comment.
Why do we need this? Can't we just null-check BodyValueExpr?
There was a problem hiding this comment.
We need this because the way the try-parse logic is generated. From what I've been able to reason from removing this argument and tracing through the code, the try-parse logic assembles a code block that looks like this when mapped to a lambda.
(target, httpContext, bodyValue) => {
(param1_local, param2_local) {
// if statements for validation here
// check tryParseFailedValueHere
// call action here
}
}
If we try to do something like:
(target, httpContext, bodyValue) => {
(param1_local, bodyValue) {
// if statements for validation here
// check tryParseFailedValueHere
// call action here
}
}
Then the original value of bodyValue is shadowed and set to null so the null-check doesn't behave as we'd expect.
There was a problem hiding this comment.
The try-parse logic shouldn't touch bodyValue since the body is never TryParsed.
There was a problem hiding this comment.
try-parse logic
try-parse here refers to the expression blocks that are added to the TryParseParams list and executed. Without this argument the checkBodyParamRequired expression would've shadowed bodyValue.
There was a problem hiding this comment.
I'm a bit confused here too. The TryParse should never matter for the body argument.
There was a problem hiding this comment.
If you want to check if the body is null after casting, and don't want to cast twice, storing the casted value in the local helps. Not sure if that's necessary, but one extra local doesn't seem that bad.
There was a problem hiding this comment.
I'm a bit confused here too. The TryParse should never matter for the body argument.
Previously, it didn't. This PR updates the TryParse logic to be a generic codepath for validating parameters so now we are using it for body arguments.
If you want to check if the body is null after casting, and don't want to cast twice, storing the casted value in the local helps. Not sure if that's necessary, but one extra local doesn't seem that bad.
Yeah, it's not the worse. Can you validate my understanding of what is happening mentioned in #34505 (comment)?
f6e1d0c to
d566d30
Compare
d566d30 to
b2333d7
Compare
| factoryContext.JsonRequestBodyType = parameter.ParameterType; | ||
| factoryContext.AllowEmptyRequestBody = allowEmpty || isOptional; | ||
|
|
||
| var argument = Expression.Variable(parameter.ParameterType, $"{parameter.Name}_local"); |
There was a problem hiding this comment.
If you want to check if the body is null after casting, and don't want to cast twice, storing the casted value in the local helps. Not sure if that's necessary, but one extra local doesn't seem that bad.
[FromBody(AllowEmpty = true)]to override required parameters defined in methodFixes #32375