Parcourir la source

Merge pull request #12124 from thalesmg/fix-connector-fill-defaults-r54-20231207

fix(connectors_api): fill default raw config values before returning
Thales Macedo Garitezi il y a 2 ans
Parent
commit
cf3dd85684

+ 16 - 1
apps/emqx_connector/src/emqx_connector_api.erl

@@ -610,11 +610,12 @@ format_resource(
     #{
         type := Type,
         name := ConnectorName,
-        raw_config := RawConf,
+        raw_config := RawConf0,
         resource_data := ResourceData
     },
     Node
 ) ->
+    RawConf = fill_defaults(Type, RawConf0),
     redact(
         maps:merge(
             RawConf#{
@@ -638,6 +639,20 @@ format_resource_data(added_channels, Channels, Result) ->
 format_resource_data(K, V, Result) ->
     Result#{K => V}.
 
+fill_defaults(Type, RawConf) ->
+    PackedConf = pack_connector_conf(Type, RawConf),
+    FullConf = emqx_config:fill_defaults(emqx_connector_schema, PackedConf, #{}),
+    unpack_connector_conf(Type, FullConf).
+
+pack_connector_conf(Type, RawConf) ->
+    #{<<"connectors">> => #{bin(Type) => #{<<"foo">> => RawConf}}}.
+
+unpack_connector_conf(Type, PackedConf) ->
+    TypeBin = bin(Type),
+    #{<<"connectors">> := Bridges} = PackedConf,
+    #{<<"foo">> := RawConf} = maps:get(TypeBin, Bridges),
+    RawConf.
+
 format_action(ActionId) ->
     element(2, emqx_bridge_v2:parse_id(ActionId)).
 

+ 13 - 0
apps/emqx_connector/test/emqx_connector_api_SUITE.erl

@@ -845,6 +845,19 @@ t_fail_delete_with_action(Config) ->
     ),
     ok.
 
+t_raw_config_response_defaults(Config) ->
+    Params = maps:without([<<"enable">>, <<"resource_opts">>], ?KAFKA_CONNECTOR(?CONNECTOR_NAME)),
+    ?assertMatch(
+        {ok, 201, #{<<"enable">> := true, <<"resource_opts">> := #{}}},
+        request_json(
+            post,
+            uri(["connectors"]),
+            Params,
+            Config
+        )
+    ),
+    ok.
+
 %%% helpers
 listen_on_random_port() ->
     SockOpts = [binary, {active, false}, {packet, raw}, {reuseaddr, true}, {backlog, 1000}],