From f98cb972e2bd305ffebd97664730f7779f45146e Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Fri, 15 Apr 2022 13:43:26 +0800 Subject: [PATCH] fix: dashboard ct failed --- apps/emqx_dashboard/src/emqx_dashboard.erl | 1 + .../src/emqx_dashboard_schema.erl | 2 +- .../src/emqx_dashboard_swagger.erl | 10 ++-- .../test/emqx_swagger_parameter_SUITE.erl | 22 +++++++ .../test/emqx_swagger_requestBody_SUITE.erl | 59 +++++++++---------- .../test/emqx_swagger_response_SUITE.erl | 39 +++++++----- 6 files changed, 82 insertions(+), 51 deletions(-) diff --git a/apps/emqx_dashboard/src/emqx_dashboard.erl b/apps/emqx_dashboard/src/emqx_dashboard.erl index 870003c1c..c0f9cddf8 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard.erl @@ -27,6 +27,7 @@ -export([ init_i18n/2, + init_i18n/0, get_i18n/0, clear_i18n/0 ]). diff --git a/apps/emqx_dashboard/src/emqx_dashboard_schema.erl b/apps/emqx_dashboard/src/emqx_dashboard_schema.erl index 5531b16d5..bbe198f46 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_schema.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_schema.erl @@ -197,7 +197,7 @@ cors(_) -> i18n_lang(type) -> ?ENUM([en, zh]); i18n_lang(default) -> zh; i18n_lang('readOnly') -> true; -i18n_lang(desc) -> "i18n language"; +i18n_lang(desc) -> "Internationalization language support."; i18n_lang(_) -> undefined. sc(Type, Meta) -> hoconsc:mk(Type, Meta). diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index 644654306..7ef374ec3 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl @@ -306,7 +306,7 @@ check_request_body(#{body := Body}, Spec, _Module, _CheckFun, false) when is_map %% tags, description, summary, security, deprecated meta_to_spec(Meta, Module, Options) -> {Params, Refs1} = parameters(maps:get(parameters, Meta, []), Module), - {RequestBody, Refs2} = request_body(maps:get('requestBody', Meta, []), Module), + {RequestBody, Refs2} = request_body(maps:get('requestBody', Meta, []), Module, Options), {Responses, Refs3} = responses(maps:get(responses, Meta, #{}), Module, Options), { generate_method_desc(to_spec(Meta, Params, RequestBody, Responses)), @@ -424,11 +424,11 @@ resolve_desc(Key, Struct) -> false -> Desc end. -request_body(#{content := _} = Content, _Module) -> +request_body(#{content := _} = Content, _Module, _Options) -> {Content, []}; -request_body([], _Module) -> +request_body([], _Module, _Options) -> {[], []}; -request_body(Schema, Module) -> +request_body(Schema, Module, Options) -> {{Props, Refs}, Examples} = case hoconsc:is_schema(Schema) of true -> @@ -436,7 +436,7 @@ request_body(Schema, Module) -> SchemaExamples = hocon_schema:field_schema(Schema, examples), {hocon_schema_to_spec(HoconSchema, Module), SchemaExamples}; false -> - {parse_object(Schema, Module, #{}), undefined} + {parse_object(Schema, Module, Options), undefined} end, {#{<<"content">> => content(Props, Examples)}, Refs}. diff --git a/apps/emqx_dashboard/test/emqx_swagger_parameter_SUITE.erl b/apps/emqx_dashboard/test/emqx_swagger_parameter_SUITE.erl index 3b54c54c4..614b3064e 100644 --- a/apps/emqx_dashboard/test/emqx_swagger_parameter_SUITE.erl +++ b/apps/emqx_dashboard/test/emqx_swagger_parameter_SUITE.erl @@ -4,6 +4,7 @@ %% API -export([paths/0, api_spec/0, schema/1, fields/1]). +-export([init_per_suite/1, end_per_suite/1]). -export([t_in_path/1, t_in_query/1, t_in_mix/1, t_without_in/1, t_ref/1, t_public_ref/1]). -export([t_require/1, t_nullable/1, t_method/1, t_api_spec/1]). -export([t_in_path_trans/1, t_in_query_trans/1, t_in_mix_trans/1, t_ref_trans/1]). @@ -26,6 +27,27 @@ groups() -> [ t_in_path_trans_error, t_in_query_trans_error, t_in_mix_trans_error]} ]. +init_per_suite(Config) -> + mria:start(), + application:load(emqx_dashboard), + emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1), + emqx_dashboard:init_i18n(), + Config. + +set_special_configs(emqx_dashboard) -> + emqx_dashboard_api_test_helpers:set_default_config(), + ok; +set_special_configs(_) -> + ok. + +end_per_suite(Config) -> + end_suite(), + Config. + +end_suite() -> + application:unload(emqx_management), + emqx_common_test_helpers:stop_apps([emqx_dashboard]). + t_in_path(_Config) -> Expect = [#{description => <<"Indicates which sorts of issues to return">>, diff --git a/apps/emqx_dashboard/test/emqx_swagger_requestBody_SUITE.erl b/apps/emqx_dashboard/test/emqx_swagger_requestBody_SUITE.erl index bf48046c2..5501ee780 100644 --- a/apps/emqx_dashboard/test/emqx_swagger_requestBody_SUITE.erl +++ b/apps/emqx_dashboard/test/emqx_swagger_requestBody_SUITE.erl @@ -3,41 +3,36 @@ -behaviour(minirest_api). -behaviour(hocon_schema). -%% API --export([paths/0, api_spec/0, schema/1, fields/1]). --export([t_object/1, t_nest_object/1, t_api_spec/1, - t_local_ref/1, t_remote_ref/1, t_bad_ref/1, t_none_ref/1, t_nest_ref/1, - t_ref_array_with_key/1, t_ref_array_without_key/1, t_sub_fields/1 -]). --export([ - t_object_trans/1, t_object_notrans/1, t_nest_object_trans/1, t_local_ref_trans/1, - t_remote_ref_trans/1, t_nest_ref_trans/1, - t_ref_array_with_key_trans/1, t_ref_array_without_key_trans/1, - t_ref_trans_error/1, t_object_trans_error/1 -]). --export([all/0, suite/0, groups/0]). +-compile(nowarn_export_all). +-compile(export_all). -include_lib("eunit/include/eunit.hrl"). -include_lib("typerefl/include/types.hrl"). -include_lib("hocon/include/hoconsc.hrl"). -import(hoconsc, [mk/2]). -all() -> [{group, spec}, {group, validation}]. +all() -> emqx_common_test_helpers:all(?MODULE). -suite() -> [{timetrap, {minutes, 1}}]. -groups() -> [ - {spec, [parallel], [ - t_api_spec, t_object, t_nest_object, - t_local_ref, t_remote_ref, t_bad_ref, t_none_ref, - t_ref_array_with_key, t_ref_array_without_key, t_nest_ref]}, - {validation, [parallel], - [ - t_object_trans, t_object_notrans, t_local_ref_trans, t_remote_ref_trans, - t_ref_array_with_key_trans, t_ref_array_without_key_trans, t_nest_ref_trans, - t_ref_trans_error, t_object_trans_error - %% t_nest_object_trans, - ]} -]. +init_per_suite(Config) -> + mria:start(), + application:load(emqx_dashboard), + emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1), + emqx_dashboard:init_i18n(), + Config. + +set_special_configs(emqx_dashboard) -> + emqx_dashboard_api_test_helpers:set_default_config(), + ok; +set_special_configs(_) -> + ok. + +end_per_suite(Config) -> + end_suite(), + Config. + +end_suite() -> + application:unload(emqx_management), + emqx_common_test_helpers:stop_apps([emqx_dashboard]). t_object(_Config) -> Spec = #{ @@ -281,7 +276,7 @@ t_object_notrans(_Config) -> ?assertEqual(Body, ActualBody), ok. -t_nest_object_trans(_Config) -> +todo_t_nest_object_check(_Config) -> Path = "/nest/object", Body = #{ <<"timeout">> => "10m", @@ -306,7 +301,7 @@ t_nest_object_trans(_Config) -> body => #{<<"per_page">> => 10, <<"timeout">> => 600} }, - {ok, NewRequest} = trans_requestBody(Path, Body), + {ok, NewRequest} = check_requestBody(Path, Body), ?assertEqual(Expect, NewRequest), ok. @@ -487,6 +482,10 @@ trans_requestBody(Path, Body) -> trans_requestBody(Path, Body, fun emqx_dashboard_swagger:filter_check_request_and_translate_body/2). +check_requestBody(Path, Body) -> + trans_requestBody(Path, Body, + fun emqx_dashboard_swagger:filter_check_request/2). + trans_requestBody(Path, Body, Filter) -> Meta = #{module => ?MODULE, method => post, path => Path}, Request = #{bindings => #{}, query_string => #{}, body => Body}, diff --git a/apps/emqx_dashboard/test/emqx_swagger_response_SUITE.erl b/apps/emqx_dashboard/test/emqx_swagger_response_SUITE.erl index 3b68c8eff..1444370fb 100644 --- a/apps/emqx_dashboard/test/emqx_swagger_response_SUITE.erl +++ b/apps/emqx_dashboard/test/emqx_swagger_response_SUITE.erl @@ -10,22 +10,31 @@ -include_lib("hocon/include/hoconsc.hrl"). -import(hoconsc, [mk/2]). --export([all/0, suite/0, groups/0]). --export([paths/0, api_spec/0, schema/1, fields/1]). --export([t_simple_binary/1, t_object/1, t_nest_object/1, t_empty/1, t_error/1, - t_raw_local_ref/1, t_raw_remote_ref/1, t_hocon_schema_function/1, t_complicated_type/1, - t_local_ref/1, t_remote_ref/1, t_bad_ref/1, t_none_ref/1, t_nest_ref/1, t_sub_fields/1, - t_ref_array_with_key/1, t_ref_array_without_key/1, t_api_spec/1]). +-compile(nowarn_export_all). +-compile(export_all). -all() -> [{group, spec}]. -suite() -> [{timetrap, {minutes, 1}}]. -groups() -> [ - {spec, [parallel], [ - t_api_spec, t_simple_binary, t_object, t_nest_object, t_error, t_complicated_type, - t_raw_local_ref, t_raw_remote_ref, t_empty, t_hocon_schema_function, - t_local_ref, t_remote_ref, t_bad_ref, t_none_ref, t_sub_fields, - t_ref_array_with_key, t_ref_array_without_key, t_nest_ref]} -]. +all() -> emqx_common_test_helpers:all(?MODULE). + +init_per_suite(Config) -> + mria:start(), + application:load(emqx_dashboard), + emqx_common_test_helpers:start_apps([emqx_conf, emqx_dashboard], fun set_special_configs/1), + emqx_dashboard:init_i18n(), + Config. + +set_special_configs(emqx_dashboard) -> + emqx_dashboard_api_test_helpers:set_default_config(), + ok; +set_special_configs(_) -> + ok. + +end_per_suite(Config) -> + end_suite(), + Config. + +end_suite() -> + application:unload(emqx_management), + emqx_common_test_helpers:stop_apps([emqx_dashboard]). t_simple_binary(_config) -> Path = "/simple/bin",