@@ -19,7 +19,97 @@ internal static class RequestDelegateGeneratorSources
19
19
20
20
public static string GeneratedCodeAttribute => $@ "[System.CodeDom.Compiler.GeneratedCodeAttribute(""{ typeof ( RequestDelegateGeneratorSources ) . Assembly . FullName } "", ""{ typeof ( RequestDelegateGeneratorSources ) . Assembly . GetName ( ) . Version } "")]";
21
21
22
- public static string GetGeneratedRouteBuilderExtensionsSource ( string genericThunks , string thunks , string endpoints ) => $$ """
22
+ public static string TryResolveBodyAsyncMethod => """
23
+ private static async ValueTask<(bool, T?)> TryResolveBodyAsync<T>(HttpContext httpContext, bool allowEmpty)
24
+ {
25
+ var feature = httpContext.Features.Get<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
26
+
27
+ if (feature?.CanHaveBody == true)
28
+ {
29
+ if (!httpContext.Request.HasJsonContentType())
30
+ {
31
+ httpContext.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
32
+ return (false, default);
33
+ }
34
+ try
35
+ {
36
+ var bodyValue = await httpContext.Request.ReadFromJsonAsync<T>();
37
+ if (!allowEmpty && bodyValue == null)
38
+ {
39
+ httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
40
+ return (false, bodyValue);
41
+ }
42
+ return (true, bodyValue);
43
+ }
44
+ catch (IOException)
45
+ {
46
+ return (false, default);
47
+ }
48
+ catch (System.Text.Json.JsonException)
49
+ {
50
+ httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
51
+ return (false, default);
52
+ }
53
+ }
54
+ else if (!allowEmpty)
55
+ {
56
+ httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
57
+ }
58
+
59
+ return (false, default);
60
+ }
61
+ """ ;
62
+
63
+ public static string TryParseExplicitMethod => """
64
+ private static bool TryParseExplicit<T>(string? s, IFormatProvider? provider, [MaybeNullWhen(returnValue: false)] out T result) where T: IParsable<T>
65
+ => T.TryParse(s, provider, out result);
66
+ """ ;
67
+
68
+ public static string TryResolveJsonBodyOrServiceAsyncMethod => """
69
+ private static ValueTask<(bool, T?)> TryResolveJsonBodyOrServiceAsync<T>(HttpContext httpContext, bool isOptional, IServiceProviderIsService? serviceProviderIsService = null)
70
+ {
71
+ if (serviceProviderIsService is not null)
72
+ {
73
+ if (serviceProviderIsService.IsService(typeof(T)))
74
+ {
75
+ return new ValueTask<(bool, T?)>((true, httpContext.RequestServices.GetService<T>()));
76
+ }
77
+ }
78
+ return TryResolveBodyAsync<T>(httpContext, isOptional);
79
+ }
80
+ """ ;
81
+
82
+ public static string BindAsyncMethod => """
83
+ private static ValueTask<T?> BindAsync<T>(HttpContext context, ParameterInfo parameter)
84
+ where T : class, IBindableFromHttpContext<T>
85
+ {
86
+ return T.BindAsync(context, parameter);
87
+ }
88
+ """ ;
89
+
90
+ public static string ResolveFromRouteOrQueryMethod => """
91
+ private static Func<HttpContext, StringValues> ResolveFromRouteOrQuery(string parameterName, IEnumerable<string>? routeParameterNames)
92
+ {
93
+ return routeParameterNames?.Contains(parameterName, StringComparer.OrdinalIgnoreCase) == true
94
+ ? (httpContext) => new StringValues((string?)httpContext.Request.RouteValues[parameterName])
95
+ : (httpContext) => httpContext.Request.Query[parameterName];
96
+ }
97
+ """ ;
98
+
99
+ public static string WriteToResponseAsyncMethod => """
100
+ private static Task WriteToResponseAsync<T>(T? value, HttpContext httpContext, JsonTypeInfo<T> jsonTypeInfo, JsonSerializerOptions options)
101
+ {
102
+ var runtimeType = value?.GetType();
103
+ if (runtimeType is null || jsonTypeInfo.Type == runtimeType || jsonTypeInfo.PolymorphismOptions is not null)
104
+ {
105
+ return httpContext.Response.WriteAsJsonAsync(value!, jsonTypeInfo);
106
+ }
107
+
108
+ return httpContext.Response.WriteAsJsonAsync(value!, options.GetTypeInfo(runtimeType));
109
+ }
110
+ """ ;
111
+
112
+ public static string GetGeneratedRouteBuilderExtensionsSource ( string genericThunks , string thunks , string endpoints , string helperMethods ) => $$ """
23
113
{{ SourceHeader }}
24
114
25
115
namespace Microsoft.AspNetCore.Builder
@@ -107,87 +197,7 @@ private static Task ExecuteObjectResult(object? obj, HttpContext httpContext)
107
197
}
108
198
}
109
199
110
- private static Func<HttpContext, StringValues> ResolveFromRouteOrQuery(string parameterName, IEnumerable<string>? routeParameterNames)
111
- {
112
- return routeParameterNames?.Contains(parameterName, StringComparer.OrdinalIgnoreCase) == true
113
- ? (httpContext) => new StringValues((string?)httpContext.Request.RouteValues[parameterName])
114
- : (httpContext) => httpContext.Request.Query[parameterName];
115
- }
116
-
117
- private static Task WriteToResponseAsync<T>(T? value, HttpContext httpContext, JsonTypeInfo<T> jsonTypeInfo, JsonSerializerOptions options)
118
- {
119
- var runtimeType = value?.GetType();
120
- if (runtimeType is null || jsonTypeInfo.Type == runtimeType || jsonTypeInfo.PolymorphismOptions is not null)
121
- {
122
- return httpContext.Response.WriteAsJsonAsync(value!, jsonTypeInfo);
123
- }
124
-
125
- return httpContext.Response.WriteAsJsonAsync(value!, options.GetTypeInfo(runtimeType));
126
- }
127
-
128
- private static async ValueTask<(bool, T?)> TryResolveBodyAsync<T>(HttpContext httpContext, bool allowEmpty)
129
- {
130
- var feature = httpContext.Features.Get<Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature>();
131
-
132
- if (feature?.CanHaveBody == true)
133
- {
134
- if (!httpContext.Request.HasJsonContentType())
135
- {
136
- httpContext.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType;
137
- return (false, default);
138
- }
139
- try
140
- {
141
- var bodyValue = await httpContext.Request.ReadFromJsonAsync<T>();
142
- if (!allowEmpty && bodyValue == null)
143
- {
144
- httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
145
- return (false, bodyValue);
146
- }
147
- return (true, bodyValue);
148
- }
149
- catch (IOException)
150
- {
151
- return (false, default);
152
- }
153
- catch (System.Text.Json.JsonException)
154
- {
155
- httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
156
- return (false, default);
157
- }
158
- }
159
- else if (!allowEmpty)
160
- {
161
- httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
162
- }
163
-
164
- return (false, default);
165
- }
166
-
167
- private static ValueTask<T?> BindAsync<T>(HttpContext context, ParameterInfo parameter)
168
- where T : class, IBindableFromHttpContext<T>
169
- {
170
- return T.BindAsync(context, parameter);
171
- }
172
-
173
- private static ValueTask<(bool, T?)> TryResolveJsonBodyOrServiceAsync<T>(HttpContext httpContext, bool isOptional, IServiceProviderIsService? serviceProviderIsService = null)
174
- {
175
- if (serviceProviderIsService is not null)
176
- {
177
- if (serviceProviderIsService.IsService(typeof(T)))
178
- {
179
- return new ValueTask<(bool, T?)>((true, httpContext.RequestServices.GetService<T>()));
180
- }
181
- }
182
- return TryResolveBodyAsync<T>(httpContext, isOptional);
183
- }
184
- }
185
-
186
- {{ GeneratedCodeAttribute }}
187
- file static class ParsableHelper<T> where T : IParsable<T>
188
- {
189
- public static T Parse(string s, IFormatProvider? provider) => T.Parse(s, provider);
190
- public static bool TryParse(string? s, IFormatProvider? provider, [MaybeNullWhen(returnValue: false)] out T result) => T.TryParse(s, provider, out result);
200
+ {{ helperMethods }}
191
201
}
192
202
}
193
203
""" ;
0 commit comments