From f8573ae8d7dade0592328fa17ffac27e92153ef4 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Sun, 19 Mar 2023 12:34:06 +0800 Subject: [PATCH 1/2] fix: swagger doc 500 crash --- apps/emqx_conf/src/emqx_conf.erl | 2 +- apps/emqx_dashboard/src/emqx_dashboard.erl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/emqx_conf/src/emqx_conf.erl b/apps/emqx_conf/src/emqx_conf.erl index c64062861..33214946d 100644 --- a/apps/emqx_conf/src/emqx_conf.erl +++ b/apps/emqx_conf/src/emqx_conf.erl @@ -162,7 +162,7 @@ gen_schema_json(Dir, I18nFile, SchemaModule, Lang) -> ok = file:write_file(SchemaJsonFile, IoData). gen_api_schema_json(Dir, I18nFile, Lang) -> - emqx_dashboard:init_i18n(I18nFile, Lang), + emqx_dashboard:init_i18n(I18nFile, list_to_binary(Lang)), gen_api_schema_json_hotconf(Dir, Lang), gen_api_schema_json_bridge(Dir, Lang), emqx_dashboard:clear_i18n(). diff --git a/apps/emqx_dashboard/src/emqx_dashboard.erl b/apps/emqx_dashboard/src/emqx_dashboard.erl index 060045603..f0344dd5a 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard.erl @@ -133,8 +133,8 @@ get_i18n() -> application:get_env(emqx_dashboard, i18n). init_i18n(File, Lang) when is_atom(Lang) -> - init_i18n(File, atom_to_list(Lang)); -init_i18n(File, Lang) when is_list(Lang) -> + init_i18n(File, atom_to_binary(Lang)); +init_i18n(File, Lang) when is_binary(Lang) -> Cache = hocon_schema:new_desc_cache(File), application:set_env(emqx_dashboard, i18n, #{lang => Lang, cache => Cache}). From b529a0bce15f71c0cee46af28ce40b0764bb18a3 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Sun, 19 Mar 2023 13:05:32 +0800 Subject: [PATCH 2/2] test: add swagger.json api test --- apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl b/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl index 8190b7c54..906d57e9d 100644 --- a/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl +++ b/apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl @@ -155,6 +155,18 @@ t_rest_api(_Config) -> emqx_dashboard_admin:add_user(<<"admin">>, Password, <<"administrator">>), ok. +t_swagger_json(_Config) -> + Url = ?HOST ++ "/api-docs/swagger.json", + %% with auth + Auth = auth_header_(<<"admin">>, <<"public_www1">>), + {ok, 200, Body1} = request_api(get, Url, Auth), + ?assert(jsx:is_json(Body1)), + %% without auth + {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Body2}} = + httpc:request(get, {Url, []}, [], [{body_format, binary}]), + ?assertEqual(Body1, Body2), + ok. + t_cli(_Config) -> [mria:dirty_delete(?ADMIN, Admin) || Admin <- mnesia:dirty_all_keys(?ADMIN)], emqx_dashboard_cli:admins(["add", "username", "password_ww2"]),