فهرست منبع

Merge pull request #10167 from zhongwencool/fix-swagger-doc-500-crash

fix: swagger doc 500 crash
zhongwencool 3 سال پیش
والد
کامیت
6a668c35db
3فایلهای تغییر یافته به همراه15 افزوده شده و 3 حذف شده
  1. 1 1
      apps/emqx_conf/src/emqx_conf.erl
  2. 2 2
      apps/emqx_dashboard/src/emqx_dashboard.erl
  3. 12 0
      apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl

+ 1 - 1
apps/emqx_conf/src/emqx_conf.erl

@@ -162,7 +162,7 @@ gen_schema_json(Dir, I18nFile, SchemaModule, Lang) ->
     ok = file:write_file(SchemaJsonFile, IoData).
     ok = file:write_file(SchemaJsonFile, IoData).
 
 
 gen_api_schema_json(Dir, I18nFile, Lang) ->
 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_hotconf(Dir, Lang),
     gen_api_schema_json_bridge(Dir, Lang),
     gen_api_schema_json_bridge(Dir, Lang),
     emqx_dashboard:clear_i18n().
     emqx_dashboard:clear_i18n().

+ 2 - 2
apps/emqx_dashboard/src/emqx_dashboard.erl

@@ -133,8 +133,8 @@ get_i18n() ->
     application:get_env(emqx_dashboard, i18n).
     application:get_env(emqx_dashboard, i18n).
 
 
 init_i18n(File, Lang) when is_atom(Lang) ->
 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),
     Cache = hocon_schema:new_desc_cache(File),
     application:set_env(emqx_dashboard, i18n, #{lang => Lang, cache => Cache}).
     application:set_env(emqx_dashboard, i18n, #{lang => Lang, cache => Cache}).
 
 

+ 12 - 0
apps/emqx_dashboard/test/emqx_dashboard_SUITE.erl

@@ -155,6 +155,18 @@ t_rest_api(_Config) ->
     emqx_dashboard_admin:add_user(<<"admin">>, Password, <<"administrator">>),
     emqx_dashboard_admin:add_user(<<"admin">>, Password, <<"administrator">>),
     ok.
     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) ->
 t_cli(_Config) ->
     [mria:dirty_delete(?ADMIN, Admin) || Admin <- mnesia:dirty_all_keys(?ADMIN)],
     [mria:dirty_delete(?ADMIN, Admin) || Admin <- mnesia:dirty_all_keys(?ADMIN)],
     emqx_dashboard_cli:admins(["add", "username", "password_ww2"]),
     emqx_dashboard_cli:admins(["add", "username", "password_ww2"]),