From a12478225afd6c92a8d48127b3681711eae6d184 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Wed, 20 Jul 2022 16:56:07 +0800 Subject: [PATCH] fix: fix bad swagger format --- .../src/emqx_authn_user_import_api.erl | 20 +--- .../emqx_authz/src/emqx_authz_api_sources.erl | 108 +++++++++--------- .../emqx_dashboard/src/emqx_dashboard_api.erl | 1 - .../src/emqx_dashboard_swagger.erl | 15 +++ .../emqx_gateway_api_authn_user_import.erl | 20 +--- 5 files changed, 76 insertions(+), 88 deletions(-) diff --git a/apps/emqx_authn/src/emqx_authn_user_import_api.erl b/apps/emqx_authn/src/emqx_authn_user_import_api.erl index 30417acf7..48e623438 100644 --- a/apps/emqx_authn/src/emqx_authn_user_import_api.erl +++ b/apps/emqx_authn/src/emqx_authn_user_import_api.erl @@ -66,15 +66,7 @@ schema("/authentication/:id/import_users") -> tags => ?API_TAGS_GLOBAL, description => ?DESC(authentication_id_import_users_post), parameters => [emqx_authn_api:param_auth_id()], - 'requestBody' => #{ - content => #{ - 'multipart/form-data' => #{ - schema => #{ - filename => file - } - } - } - }, + 'requestBody' => emqx_dashboard_swagger:file_schema(filename), responses => #{ 204 => <<"Users imported">>, 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), @@ -89,15 +81,7 @@ schema("/listeners/:listener_id/authentication/:id/import_users") -> tags => ?API_TAGS_SINGLE, description => ?DESC(listeners_listener_id_authentication_id_import_users_post), parameters => [emqx_authn_api:param_listener_id(), emqx_authn_api:param_auth_id()], - 'requestBody' => #{ - content => #{ - 'multipart/form-data' => #{ - schema => #{ - filename => file - } - } - } - }, + 'requestBody' => emqx_dashboard_swagger:file_schema(filename), responses => #{ 204 => <<"Users imported">>, 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>), diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index 2dfc6da33..5f3873998 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -565,58 +565,64 @@ bin(Term) -> erlang:iolist_to_binary(io_lib:format("~p", [Term])). status_metrics_example() -> #{ - resource_metrics => #{ - matched => 0, - success => 0, - failed => 0, - rate => 0.0, - rate_last5m => 0.0, - rate_max => 0.0 - }, - node_resource_metrics => [ - #{ - node => node(), - metrics => #{ - matched => 0, - success => 0, - failed => 0, - rate => 0.0, - rate_last5m => 0.0, - rate_max => 0.0 - } - } - ], - metrics => #{ - total => 0, - allow => 0, - deny => 0, - nomatch => 0, - rate => 0.0, - rate_last5m => 0.0, - rate_max => 0.0 - }, - node_metrics => [ - #{ - node => node(), - metrics => #{ - total => 0, - allow => 0, - deny => 0, - nomatch => 0, - rate => 0.0, - rate_last5m => 0.0, - rate_max => 0.0 - } - } - ], + 'metrics_example' => #{ + summary => <<"Showing a typical metrics example">>, + value => + #{ + resource_metrics => #{ + matched => 0, + success => 0, + failed => 0, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.0 + }, + node_resource_metrics => [ + #{ + node => node(), + metrics => #{ + matched => 0, + success => 0, + failed => 0, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.0 + } + } + ], + metrics => #{ + total => 0, + allow => 0, + deny => 0, + nomatch => 0, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.0 + }, + node_metrics => [ + #{ + node => node(), + metrics => #{ + total => 0, + allow => 0, + deny => 0, + nomatch => 0, + rate => 0.0, + rate_last5m => 0.0, + rate_max => 0.0 + } + } + ], - status => connected, - node_status => [ - #{ - node => node(), - status => connected - } - ] + status => connected, + node_status => [ + #{ + node => node(), + status => connected + } + ] + } + } }. create_authz_file(Body) -> diff --git a/apps/emqx_dashboard/src/emqx_dashboard_api.erl b/apps/emqx_dashboard/src/emqx_dashboard_api.erl index b6231e7c6..a8fa5d187 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_api.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_api.erl @@ -180,7 +180,6 @@ field(username_in_path) -> {username, mk(binary(), #{ desc => ?DESC(username), - 'maxLength' => 100, example => <<"admin">>, in => path, required => true diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index 80b4e9624..3d65e569c 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl @@ -24,6 +24,7 @@ -export([namespace/0, namespace/1, fields/1]). -export([schema_with_example/2, schema_with_examples/2]). -export([error_codes/1, error_codes/2]). +-export([file_schema/1]). -export([filter_check_request/2, filter_check_request_and_translate_body/2]). @@ -165,6 +166,20 @@ error_codes(Codes = [_ | _], MsgDesc) -> })} ]. +file_schema(FileName) -> + #{ + content => #{ + 'multipart/form-data' => #{ + schema => #{ + type => object, + properties => #{ + FileName => #{type => string, format => binary} + } + } + } + } + }. + %%------------------------------------------------------------------------------ %% Private functions %%------------------------------------------------------------------------------ diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl index b86dc4510..c324262ee 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl @@ -124,15 +124,7 @@ schema("/gateway/:name/authentication/import_users") -> #{ desc => ?DESC(emqx_gateway_api_authn, import_users), parameters => params_gateway_name_in_path(), - 'requestBody' => #{ - content => #{ - 'multipart/form-data' => #{ - schema => #{ - filename => file - } - } - } - }, + 'requestBody' => emqx_dashboard_swagger:file_schema(filename), responses => ?STANDARD_RESP(#{204 => <<"Imported">>}) } @@ -145,15 +137,7 @@ schema("/gateway/:name/listeners/:id/authentication/import_users") -> desc => ?DESC(emqx_gateway_api_listeners, import_users), parameters => params_gateway_name_in_path() ++ params_listener_id_in_path(), - 'requestBody' => #{ - content => #{ - 'multipart/form-data' => #{ - schema => #{ - filename => file - } - } - } - }, + 'requestBody' => emqx_dashboard_swagger:file_schema(filename), responses => ?STANDARD_RESP(#{204 => <<"Imported">>}) }