1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System ;
5
- using System . Collections . Generic ;
6
- using System . IO ;
7
- using System . Linq ;
8
4
using System . Linq . Expressions ;
9
5
using System . Reflection ;
10
6
using System . Security . Claims ;
11
- using System . Threading ;
12
- using System . Threading . Tasks ;
13
7
using Microsoft . AspNetCore . Http . Features ;
14
8
using Microsoft . AspNetCore . Http . Metadata ;
15
9
using Microsoft . Extensions . DependencyInjection ;
@@ -168,7 +162,7 @@ public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, ob
168
162
169
163
var arguments = CreateArguments ( methodInfo . GetParameters ( ) , factoryContext ) ;
170
164
171
- var responseWritingMethodCall = factoryContext . CheckParams . Count > 0 ?
165
+ var responseWritingMethodCall = factoryContext . ParamCheckExpressions . Count > 0 ?
172
166
CreateParamCheckingResponseWritingMethodCall ( methodInfo , targetExpression , arguments , factoryContext ) :
173
167
CreateResponseWritingMethodCall ( methodInfo , targetExpression , arguments ) ;
174
168
@@ -325,15 +319,21 @@ private static Expression CreateParamCheckingResponseWritingMethodCall(
325
319
// };
326
320
// }
327
321
328
- var localVariables = new ParameterExpression [ factoryContext . CheckParams . Count + 1 ] ;
329
- var checkParamAndCallMethod = new Expression [ factoryContext . CheckParams . Count + 1 ] ;
322
+ var localVariables = new ParameterExpression [ factoryContext . ExtraLocals . Count + 1 ] ;
323
+ var checkParamAndCallMethod = new Expression [ factoryContext . ParamCheckExpressions . Count + 1 ] ;
330
324
331
- for ( var i = 0 ; i < factoryContext . CheckParams . Count ; i ++ )
325
+
326
+ for ( var i = 0 ; i < factoryContext . ExtraLocals . Count ; i ++ )
327
+ {
328
+ localVariables [ i ] = factoryContext . ExtraLocals [ i ] ;
329
+ }
330
+
331
+ for ( var i = 0 ; i < factoryContext . ParamCheckExpressions . Count ; i ++ )
332
332
{
333
- ( localVariables [ i ] , checkParamAndCallMethod [ i ] ) = factoryContext . CheckParams [ i ] ;
333
+ checkParamAndCallMethod [ i ] = factoryContext . ParamCheckExpressions [ i ] ;
334
334
}
335
335
336
- localVariables [ factoryContext . CheckParams . Count ] = WasParamCheckFailureExpr ;
336
+ localVariables [ factoryContext . ExtraLocals . Count ] = WasParamCheckFailureExpr ;
337
337
338
338
var set400StatusAndReturnCompletedTask = Expression . Block (
339
339
Expression . Assign ( StatusCodeExpr , Expression . Constant ( 400 ) ) ,
@@ -345,7 +345,7 @@ private static Expression CreateParamCheckingResponseWritingMethodCall(
345
345
set400StatusAndReturnCompletedTask ,
346
346
AddResponseWritingToMethodCall ( methodCall , methodInfo . ReturnType ) ) ;
347
347
348
- checkParamAndCallMethod [ factoryContext . CheckParams . Count ] = checkWasParamCheckFailure ;
348
+ checkParamAndCallMethod [ factoryContext . ParamCheckExpressions . Count ] = checkWasParamCheckFailure ;
349
349
350
350
return Expression . Block ( localVariables , checkParamAndCallMethod ) ;
351
351
}
@@ -561,7 +561,8 @@ private static Expression BindParameterFromValue(ParameterInfo parameter, Expres
561
561
)
562
562
) ;
563
563
564
- factoryContext . CheckParams . Add ( ( argument , checkRequiredStringParameterBlock ) ) ;
564
+ factoryContext . ExtraLocals . Add ( argument ) ;
565
+ factoryContext . ParamCheckExpressions . Add ( checkRequiredStringParameterBlock ) ;
565
566
return argument ;
566
567
}
567
568
@@ -652,7 +653,7 @@ private static Expression BindParameterFromValue(ParameterInfo parameter, Expres
652
653
// if (tempSourceString == null)
653
654
// {
654
655
// wasParamCheckFailure = true;
655
- // Log.RequiredParameterNotProvided(httpContext, "Int32", "param1");
656
+ // Log.RequiredParameterNotProvided(httpContext, "Int32", "param1");
656
657
// }
657
658
var checkRequiredParaseableParameterBlock = Expression . Block (
658
659
Expression . IfThen ( TempSourceStringNullExpr ,
@@ -692,7 +693,8 @@ private static Expression BindParameterFromValue(ParameterInfo parameter, Expres
692
693
// if (tempSourceString != null) { ... }
693
694
ifNotNullTryParse ) ;
694
695
695
- factoryContext . CheckParams . Add ( ( argument , fullParamCheckBlock ) ) ;
696
+ factoryContext . ExtraLocals . Add ( argument ) ;
697
+ factoryContext . ParamCheckExpressions . Add ( fullParamCheckBlock ) ;
696
698
697
699
return argument ;
698
700
}
@@ -720,36 +722,30 @@ private static Expression BindParameterFromBody(ParameterInfo parameter, bool al
720
722
factoryContext . JsonRequestBodyType = parameter . ParameterType ;
721
723
factoryContext . AllowEmptyRequestBody = allowEmpty || isOptional ;
722
724
723
- var convertedBodyValue = Expression . Convert ( BodyValueExpr , parameter . ParameterType ) ;
724
- var argument = Expression . Variable ( parameter . ParameterType , $ "{ parameter . Name } _local") ;
725
-
726
725
if ( ! factoryContext . AllowEmptyRequestBody )
727
726
{
728
727
// If the parameter is required or the user has not explicitly
729
728
// set allowBody to be empty then validate that it is required.
730
729
//
731
- // Todo body_local = Convert(bodyValue, ToDo);
732
- // if (body_local == null)
730
+ // if (bodyValue == null)
733
731
// {
734
732
// wasParamCheckFailure = true;
735
733
// Log.RequiredParameterNotProvided(httpContext, "Todo", "body");
736
734
// }
737
735
var checkRequiredBodyBlock = Expression . Block (
738
- Expression . Assign ( argument , convertedBodyValue ) ,
739
736
Expression . IfThen (
740
- Expression . Equal ( argument , Expression . Constant ( null ) ) ,
737
+ Expression . Equal ( BodyValueExpr , Expression . Constant ( null ) ) ,
741
738
Expression . Block (
742
739
Expression . Assign ( WasParamCheckFailureExpr , Expression . Constant ( true ) ) ,
743
740
Expression . Call ( LogRequiredParameterNotProvidedMethod ,
744
741
HttpContextExpr , Expression . Constant ( parameter . ParameterType . Name ) , Expression . Constant ( parameter . Name ) )
745
742
)
746
743
)
747
744
) ;
748
- factoryContext . CheckParams . Add ( ( argument , checkRequiredBodyBlock ) ) ;
749
- return argument ;
750
- }
751
745
752
- if ( parameter . HasDefaultValue )
746
+ factoryContext . ParamCheckExpressions . Add ( checkRequiredBodyBlock ) ;
747
+ }
748
+ else if ( parameter . HasDefaultValue )
753
749
{
754
750
// Convert(bodyValue ?? SomeDefault, Todo)
755
751
return Expression . Convert (
@@ -758,7 +754,7 @@ private static Expression BindParameterFromBody(ParameterInfo parameter, bool al
758
754
}
759
755
760
756
// Convert(bodyValue, Todo)
761
- return convertedBodyValue ;
757
+ return Expression . Convert ( BodyValueExpr , parameter . ParameterType ) ;
762
758
}
763
759
764
760
private static MethodInfo GetMethodInfo < T > ( Expression < T > expr )
@@ -953,7 +949,8 @@ private class FactoryContext
953
949
public List < string > ? RouteParameters { get ; set ; }
954
950
955
951
public bool UsingTempSourceString { get ; set ; }
956
- public List < ( ParameterExpression , Expression ) > CheckParams { get ; } = new ( ) ;
952
+ public List < ParameterExpression > ExtraLocals { get ; } = new ( ) ;
953
+ public List < Expression > ParamCheckExpressions { get ; } = new ( ) ;
957
954
}
958
955
959
956
private static partial class Log
0 commit comments