Przeglądaj źródła

fix(redis): json decode if arg for map_to_redis_hset_args is string

zmstone 1 rok temu
rodzic
commit
9479c8d33b

+ 14 - 1
apps/emqx_rule_engine/src/emqx_rule_funcs.erl

@@ -825,8 +825,21 @@ join_to_string(Sep, List) -> emqx_variform_bif:join_to_string(Sep, List).
 %% - String values are always quoted
 %% - No escape sequence for keys and values
 %% - Float point values are formatted with fixed (6) decimal point compact-formatting
+map_to_redis_hset_args(Payload) when erlang:is_binary(Payload) ->
+    try
+        Map = json_decode(Payload),
+        map_to_redis_hset_args(Map)
+    catch
+        _:_ ->
+            %% Discard invalid JSON
+            [map_to_redis_hset_args]
+    end;
 map_to_redis_hset_args(Map) when erlang:is_map(Map) ->
-    [map_to_redis_hset_args | maps:fold(fun redis_hset_acc/3, [], Map)].
+    Fields = maps:fold(fun redis_hset_acc/3, [], Map),
+    %% Fields can be [], the final template may have other fields for concatenation
+    [map_to_redis_hset_args | Fields];
+map_to_redis_hset_args(_Other) ->
+    [map_to_redis_hset_args].
 
 redis_hset_acc(K, V, IoData) ->
     try

+ 2 - 0
apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl

@@ -1395,6 +1395,8 @@ t_map_to_redis_hset_args(_Config) ->
                 true
         end
     ),
+    ?assertEqual([], Do(<<"not json">>)),
+    ?assertEqual([], Do([<<"not map">>, <<"not json either">>])),
     ok.
 
 %%------------------------------------------------------------------------------