From 213b7c2501409d066b6e13133b648143af904e3c Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 18 Aug 2021 18:53:53 +0800 Subject: [PATCH] fix(config_api): improve the schema for config update APIs --- apps/emqx/src/emqx_map_lib.erl | 1 + .../src/emqx_mgmt_api_configs.erl | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apps/emqx/src/emqx_map_lib.erl b/apps/emqx/src/emqx_map_lib.erl index cda4f3a85..e5baeb850 100644 --- a/apps/emqx/src/emqx_map_lib.erl +++ b/apps/emqx/src/emqx_map_lib.erl @@ -24,6 +24,7 @@ , safe_atom_key_map/1 , unsafe_atom_key_map/1 , jsonable_map/1 + , jsonable_value/1 , deep_convert/2 ]). diff --git a/apps/emqx_management/src/emqx_mgmt_api_configs.erl b/apps/emqx_management/src/emqx_mgmt_api_configs.erl index b54e357d6..6ff401048 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_configs.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_configs.erl @@ -24,6 +24,8 @@ , config_reset/2 ]). +-export([get_conf_schema/2]). + -define(PARAM_CONF_PATH, [#{ name => conf_path, in => query, @@ -171,15 +173,19 @@ get_conf_schema(BasePath, [{Key, Conf} | Confs], Result, MaxDepth) -> %% TODO: generate from hocon schema gen_schema(Conf) when is_boolean(Conf) -> - #{type => boolean}; + with_default_value(#{type => boolean}, Conf); gen_schema(Conf) when is_binary(Conf); is_atom(Conf) -> - #{type => string}; + with_default_value(#{type => string}, Conf); gen_schema(Conf) when is_number(Conf) -> - #{type => number}; + with_default_value(#{type => number}, Conf); gen_schema(Conf) when is_list(Conf) -> #{type => array, items => case Conf of [] -> #{}; %% don't know the type - _ -> gen_schema(hd(Conf)) + _ -> + case io_lib:printable_unicode_list(Conf) of + true -> gen_schema(unicode:characters_to_binary(Conf)); + false -> gen_schema(hd(Conf)) + end end}; gen_schema(Conf) when is_map(Conf) -> #{type => object, properties => @@ -189,6 +195,9 @@ gen_schema(_Conf) -> %% by the hocon schema #{type => string}. +with_default_value(Type, Value) -> + Type#{example => emqx_map_lib:jsonable_value(Value)}. + path_join(Path) -> path_join(Path, "/").