Skip to content

Commit 8300f2d

Browse files
committed
Process parameters with FromPayload attribute
1 parent de3f295 commit 8300f2d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Source/Routing/MqttRouter.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Reflection;
11+
using System.Text.Json;
1112
using System.Threading.Tasks;
1213

1314
#nullable enable
@@ -122,7 +123,10 @@ internal async Task OnIncomingApplicationMessage(IServiceProvider svcProvider, I
122123

123124
try
124125
{
125-
paramArray = parameters.Select(p => MatchParameterOrThrow(p, routeContext.Parameters)).ToArray();
126+
paramArray = parameters.Select(p =>
127+
MatchParameterOrThrow(p, routeContext.Parameters, controllerContext, svcProvider)
128+
)
129+
.ToArray();
126130

127131
await HandlerInvoker(routeContext.Handler, classInstance, paramArray).ConfigureAwait(false);
128132
}
@@ -174,8 +178,20 @@ private static Task HandlerInvoker(MethodInfo method, object instance, object?[]
174178
throw new InvalidOperationException($"Unsupported Action return type \"{method.ReturnType}\" on method {method.DeclaringType.FullName}.{method.Name}. Only void and {nameof(Task)} are allowed.");
175179
}
176180

177-
private static object? MatchParameterOrThrow(ParameterInfo param, IReadOnlyDictionary<string, object> availableParmeters)
181+
private static object? MatchParameterOrThrow(ParameterInfo param,
182+
IReadOnlyDictionary<string, object> availableParmeters, MqttControllerContext controllerContext,
183+
IServiceProvider serviceProvider)
178184
{
185+
if (param.IsDefined(typeof(FromPayloadAttribute), false))
186+
{
187+
JsonSerializerOptions? defaultOptions =
188+
serviceProvider.GetService<MqttDefaultJsonOptions>()?.SerializerOptions;
189+
return JsonSerializer.Deserialize(controllerContext.MqttContext.ApplicationMessage.Payload,
190+
param.ParameterType,
191+
defaultOptions
192+
);
193+
}
194+
179195
if (!availableParmeters.TryGetValue(param.Name, out object? value))
180196
{
181197
if (param.IsOptional)

0 commit comments

Comments
 (0)