Просмотр исходного кода

fix: responses/error_code support ?DESC/x.

Zhongwen Deng 3 лет назад
Родитель
Сommit
c37f01fd3e

+ 5 - 3
apps/emqx_dashboard/src/emqx_dashboard_swagger.erl

@@ -150,13 +150,12 @@ error_codes(Codes) ->
     error_codes(Codes, <<"Error code to troubleshoot problems.">>).
 
 -spec error_codes(nonempty_list(atom()), binary()) -> hocon_schema:fields().
-error_codes(Codes = [_ | _], MsgExample) ->
+error_codes(Codes = [_ | _], MsgDesc) ->
     [
         {code, hoconsc:mk(hoconsc:enum(Codes))},
         {message,
             hoconsc:mk(string(), #{
-                desc => <<"Details description of the error.">>,
-                example => MsgExample
+                desc => MsgDesc
             })}
     ].
 
@@ -443,6 +442,9 @@ responses(Responses, Module, Options) ->
     {Spec, Refs, _, _} = maps:fold(fun response/3, {#{}, [], Module, Options}, Responses),
     {Spec, Refs}.
 
+response(Status, ?DESC(_Mod, _Id) = Schema, {Acc, RefsAcc, Module, Options}) ->
+    Desc = trans_description(#{}, #{desc => Schema}),
+    {Acc#{integer_to_binary(Status) => Desc}, RefsAcc, Module, Options};
 response(Status, Bin, {Acc, RefsAcc, Module, Options}) when is_binary(Bin) ->
     {Acc#{integer_to_binary(Status) => #{description => Bin}}, RefsAcc, Module, Options};
 %% Support swagger raw object(file download).

+ 3 - 4
apps/emqx_dashboard/test/emqx_swagger_response_SUITE.erl

@@ -67,16 +67,15 @@ t_error(_Config) ->
         <<"properties">> =>
         [
             {<<"code">>, #{enum => ['Bad1', 'Bad2'], type => string}},
-            {<<"message">>, #{description => <<"Details description of the error.">>,
-                example => <<"Bad request desc">>, type => string}}]
+            {<<"message">>, #{description => <<"Bad request desc">>, type => string}}]
     }}}},
     Error404 = #{<<"content">> =>
     #{<<"application/json">> => #{<<"schema">> => #{<<"type">> => object,
         <<"properties">> =>
         [
             {<<"code">>, #{enum => ['Not-Found'], type => string}},
-            {<<"message">>, #{description => <<"Details description of the error.">>,
-                example => <<"Error code to troubleshoot problems.">>, type => string}}]
+            {<<"message">>, #{
+                description => <<"Error code to troubleshoot problems.">>, type => string}}]
     }}}},
     {OperationId, Spec, Refs} = emqx_dashboard_swagger:parse_spec_ref(?MODULE, Path, #{}),
     ?assertEqual(test, OperationId),