chore: is_content_type_json to validate_content_type_json

This commit is contained in:
zhongwencool 2024-05-22 08:23:53 +08:00
parent c013366b27
commit 3aa1f86d9e
4 changed files with 11 additions and 11 deletions

View File

@ -83,7 +83,7 @@ namespace() -> "bridge".
api_spec() -> api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{ emqx_dashboard_swagger:spec(?MODULE, #{
check_schema => fun emqx_dashboard_swagger:is_content_type_json/2 check_schema => fun emqx_dashboard_swagger:validate_content_type_json/2
}). }).
paths() -> paths() ->

View File

@ -30,7 +30,7 @@
-export([base_path/0]). -export([base_path/0]).
-export([relative_uri/1, get_relative_uri/1]). -export([relative_uri/1, get_relative_uri/1]).
-export([compose_filters/2]). -export([compose_filters/2]).
-export([is_content_type_json/2, validate_content_type/3]). -export([validate_content_type_json/2, validate_content_type/3]).
-export([ -export([
filter_check_request/2, filter_check_request/2,
@ -153,18 +153,22 @@ spec(Module, Options) ->
), ),
{ApiSpec, components(lists:usort(AllRefs), Options)}. {ApiSpec, components(lists:usort(AllRefs), Options)}.
is_content_type_json(Params, Meta) -> validate_content_type_json(Params, Meta) ->
validate_content_type(Params, Meta, <<"application/json">>). validate_content_type(Params, Meta, <<"application/json">>).
%% tip: Skip content-type check if body is empty. %% tip: Skip content-type check if body is empty.
validate_content_type(#{body := Body} = Params, #{method := Method}, Expect) when validate_content_type(
#{body := Body, headers := Headers} = Params,
#{method := Method},
Expect
) when
(Method =:= put orelse (Method =:= put orelse
Method =:= post orelse Method =:= post orelse
method =:= patch) andalso method =:= patch) andalso
Body =/= #{} Body =/= #{}
-> ->
ExpectSize = byte_size(Expect), ExpectSize = byte_size(Expect),
case find_content_type(Params) of case maps:get(<<"content-type">>, Headers, undefined) of
<<Expect:ExpectSize/binary, _/binary>> -> <<Expect:ExpectSize/binary, _/binary>> ->
{ok, Params}; {ok, Params};
_ -> _ ->
@ -173,10 +177,6 @@ validate_content_type(#{body := Body} = Params, #{method := Method}, Expect) whe
validate_content_type(Params, _Meta, _Expect) -> validate_content_type(Params, _Meta, _Expect) ->
{ok, Params}. {ok, Params}.
find_content_type(#{headers := #{<<"content-type">> := Type}}) -> Type;
find_content_type(#{headers := #{<<"Content-Type">> := Type}}) -> Type;
find_content_type(_Headers) -> error.
-spec namespace() -> hocon_schema:name(). -spec namespace() -> hocon_schema:name().
namespace() -> "public". namespace() -> "public".

View File

@ -29,7 +29,7 @@ namespace() -> "license_http_api".
api_spec() -> api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{ emqx_dashboard_swagger:spec(?MODULE, #{
check_schema => fun emqx_dashboard_swagger:is_content_type_json/2 check_schema => fun emqx_dashboard_swagger:validate_content_type_json/2
}). }).
paths() -> paths() ->

View File

@ -138,7 +138,7 @@ namespace() -> "rule".
api_spec() -> api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{ emqx_dashboard_swagger:spec(?MODULE, #{
check_schema => fun emqx_dashboard_swagger:is_content_type_json/2 check_schema => fun emqx_dashboard_swagger:validate_content_type_json/2
}). }).
paths() -> paths() ->