Parcourir la source

fix(schema): remove unuse to_bin function; simplify body schema.

Zhongwen Deng il y a 4 ans
Parent
commit
9f9bf059b9

+ 1 - 10
apps/emqx_authn/src/simple_authn/emqx_authn_http.erl

@@ -63,7 +63,7 @@ common_fields() ->
     [ {mechanism, emqx_authn_schema:mechanism('password-based')}
     , {backend, emqx_authn_schema:backend(http)}
     , {url, fun url/1}
-    , {body, fun body/1}
+    , {body, map([{fuzzy, term(), binary()}])}
     , {request_timeout, fun request_timeout/1}
     ] ++ emqx_authn_schema:common_fields()
     ++ maps:to_list(maps:without([ base_url
@@ -96,10 +96,6 @@ headers_no_content_type(converter) ->
 headers_no_content_type(default) -> default_headers_no_content_type();
 headers_no_content_type(_) -> undefined.
 
-body(type) -> map();
-body(validator) -> [fun check_body/1];
-body(_) -> undefined.
-
 request_timeout(type) -> emqx_schema:duration_ms();
 request_timeout(default) -> <<"5s">>;
 request_timeout(_) -> undefined.
@@ -209,11 +205,6 @@ destroy(#{resource_id := ResourceId}) ->
 parse_fullpath(RawURL) ->
     cow_http:parse_fullpath(to_bin(RawURL)).
 
-check_body(Body) ->
-    lists:all(
-      fun erlang:is_binary/1,
-      maps:values(Body)).
-
 default_headers() ->
     maps:put(<<"content-type">>,
              <<"application/json">>,

+ 2 - 20
apps/emqx_authz/src/emqx_authz_api_schema.erl

@@ -28,10 +28,9 @@ fields(http) ->
     authz_common_fields(http)
         ++ [ {url, fun url/1}
            , {method, #{ type => enum([get, post])
-                       , default => get
-                       , converter => fun to_bin/1}}
+                       , default => get}}
            , {headers, fun headers/1}
-           , {body, fun body/1}
+           , {body, map([{fuzzy, term(), binary()}])}
            , {request_timeout, mk_duration("Request timeout", #{default => "30s"})}]
         ++ maps:to_list(maps:without([ base_url
                                      , pool_type],
@@ -97,12 +96,7 @@ headers(converter) ->
 headers(default) -> default_headers();
 headers(_) -> undefined.
 
-body(type) -> map();
-body(validator) -> [fun check_body/1];
-body(_) -> undefined.
-
 %% headers
-
 default_headers() ->
     maps:put(<<"content-type">>,
              <<"application/json">>,
@@ -121,13 +115,6 @@ transform_header_name(Headers) ->
                   maps:put(K, V, Acc)
               end, #{}, Headers).
 
-%% body
-
-check_body(Body) ->
-    lists:all(
-      fun erlang:is_binary/1,
-      maps:values(Body)).
-
 %%------------------------------------------------------------------------------
 %% MonogDB type funcs
 
@@ -159,7 +146,6 @@ authz_common_fields(Type) when is_atom(Type)->
     , {type, #{ type => enum([Type])
               , default => Type
               , in => body
-              , converter => fun to_bin/1
               }
       }
     ].
@@ -193,7 +179,3 @@ to_list(A) when is_atom(A) ->
     atom_to_list(A);
 to_list(B) when is_binary(B) ->
     binary_to_list(B).
-
-to_bin(List) when is_list(List) -> list_to_binary(List);
-to_bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
-to_bin(X) -> X.

+ 4 - 6
apps/emqx_authz/src/emqx_authz_api_sources.erl

@@ -160,7 +160,7 @@ schema("/authorization/sources/:type/move") ->
 
 sources(Method, #{bindings := #{type := Type} = Bindings } = Req)
   when is_atom(Type) ->
-    sources(Method, Req#{bindings => Bindings#{type => bin(Type)}});
+    sources(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}});
 sources(get, _) ->
     Sources = lists:foldl(fun (#{<<"type">> := <<"file">>,
                                  <<"enable">> := Enable, <<"path">> := Path}, AccIn) ->
@@ -199,7 +199,7 @@ sources(put, #{body := Body}) when is_list(Body) ->
 
 source(Method, #{bindings := #{type := Type} = Bindings } = Req)
   when is_atom(Type) ->
-    source(Method, Req#{bindings => Bindings#{type => bin(Type)}});
+    source(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}});
 source(get, #{bindings := #{type := Type}}) ->
     case get_raw_source(Type) of
         [] -> {404, #{message => <<"Not found ", Type/binary>>}};
@@ -240,7 +240,7 @@ source(delete, #{bindings := #{type := Type}}) ->
 
 move_source(Method, #{bindings := #{type := Type} = Bindings } = Req)
   when is_atom(Type) ->
-    move_source(Method, Req#{bindings => Bindings#{type => bin(Type)}});
+    move_source(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}});
 move_source(post, #{bindings := #{type := Type}, body := #{<<"position">> := Position}}) ->
     case emqx_authz:move(Type, Position) of
         {ok, _} -> {204};
@@ -342,9 +342,7 @@ do_write_file(Filename, Bytes) ->
            error(Reason)
     end.
 
-bin(List) when is_list(List) -> list_to_binary(List);
-bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
-bin(X) -> X.
+bin(Term) -> erlang:iolist_to_binary(io_lib:format("~p", [Term])).
 
 acl_conf_file() ->
     emqx_authz:acl_conf_file().

+ 1 - 0
apps/emqx_dashboard/src/emqx_dashboard_swagger.erl

@@ -459,6 +459,7 @@ typename_to_spec("timeout()", _Mod) -> #{<<"oneOf">> => [#{type => string, examp
 typename_to_spec("bytesize()", _Mod) -> #{type => string, example => <<"32MB">>};
 typename_to_spec("wordsize()", _Mod) -> #{type => string, example => <<"1024KB">>};
 typename_to_spec("map()", _Mod) -> #{type => object, example => #{}};
+typename_to_spec("#{" ++ _, Mod) -> typename_to_spec("map()", Mod);
 typename_to_spec("qos()", _Mod) -> #{type => string, enum => [0, 1, 2], example => 0};
 typename_to_spec("{binary(), binary()}", _Mod) -> #{type => object, example => #{}};
 typename_to_spec("comma_separated_list()", _Mod) ->