test: fix schema tests after types change

This commit is contained in:
Zaiming (Stone) Shi 2023-11-10 11:43:04 +01:00
parent e653c6b4e0
commit 101990b8fc
2 changed files with 83 additions and 70 deletions

View File

@ -345,15 +345,7 @@ parse_spec_ref(Module, Path, Options) ->
erlang:apply(Module, schema, [Path]) erlang:apply(Module, schema, [Path])
catch catch
Error:Reason:Stacktrace -> Error:Reason:Stacktrace ->
%% This error is intended to fail the build failed_to_generate_swagger_spec(Module, Path, Error, Reason, Stacktrace)
%% hence print to standard_error
io:format(
standard_error,
"Failed to generate swagger for path ~p in module ~p~n"
"error:~p~nreason:~p~n~p~n",
[Module, Path, Error, Reason, Stacktrace]
),
error({failed_to_generate_swagger_spec, Module, Path})
end, end,
OperationId = maps:get('operationId', Schema), OperationId = maps:get('operationId', Schema),
{Specs, Refs} = maps:fold( {Specs, Refs} = maps:fold(
@ -369,6 +361,24 @@ parse_spec_ref(Module, Path, Options) ->
RouteOpts = generate_route_opts(Schema, Options), RouteOpts = generate_route_opts(Schema, Options),
{OperationId, Specs, Refs, RouteOpts}. {OperationId, Specs, Refs, RouteOpts}.
-ifdef(TEST).
-spec failed_to_generate_swagger_spec(_, _, _, _, _) -> no_return().
failed_to_generate_swagger_spec(Module, Path, _Error, _Reason, _Stacktrace) ->
error({failed_to_generate_swagger_spec, Module, Path}).
-else.
-spec failed_to_generate_swagger_spec(_, _, _, _, _) -> no_return().
failed_to_generate_swagger_spec(Module, Path, Error, Reason, Stacktrace) ->
%% This error is intended to fail the build
%% hence print to standard_error
io:format(
standard_error,
"Failed to generate swagger for path ~p in module ~p~n"
"error:~p~nreason:~p~n~p~n",
[Module, Path, Error, Reason, Stacktrace]
),
error({failed_to_generate_swagger_spec, Module, Path}).
-endif.
generate_route_opts(Schema, Options) -> generate_route_opts(Schema, Options) ->
#{filter => compose_filters(filter(Options), custom_filter(Schema))}. #{filter => compose_filters(filter(Options), custom_filter(Schema))}.

View File

@ -317,43 +317,54 @@ t_sub_fields(_Config) ->
validate(Path, Object, ExpectRefs), validate(Path, Object, ExpectRefs),
ok. ok.
t_complicated_type(_Config) -> t_complex_type(_Config) ->
Path = "/ref/complex_type", Path = "/ref/complex_type",
Object = #{ {OperationId, Spec, Refs, #{}} = emqx_dashboard_swagger:parse_spec_ref(?MODULE, Path, #{}),
<<"content">> => #{ ?assertEqual(test, OperationId),
<<"application/json">> => Response = maps:get(responses, maps:get(post, Spec)),
#{ ResponseBody = maps:get(<<"200">>, Response),
<<"schema">> => #{ Content = maps:get(<<"content">>, ResponseBody),
<<"properties">> => JsonContent = maps:get(<<"application/json">>, Content),
Schema = maps:get(<<"schema">>, JsonContent),
?assertMatch(#{<<"type">> := object}, Schema),
Properties = maps:get(<<"properties">>, Schema),
?assertMatch(
[ [
{<<"no_neg_integer">>, #{minimum => 0, type => integer}}, {<<"no_neg_integer">>, #{minimum := 0, type := integer}},
{<<"url">>, #{example => <<"http://127.0.0.1">>, type => string}}, {<<"url">>, #{
{<<"server">>, #{example => <<"127.0.0.1:80">>, type => string}}, example := <<"http://127.0.0.1">>, type := string
}},
{<<"server">>, #{
example := <<"127.0.0.1:80">>, type := string
}},
{<<"connect_timeout">>, #{ {<<"connect_timeout">>, #{
example => infinity, example := _, type := string
<<"oneOf">> => [ }},
#{example => infinity, type => string}, {<<"pool_type">>, #{
#{type => integer} enum := [random, hash], type := string
]
}}, }},
{<<"pool_type">>, #{enum => [random, hash], type => string}},
{<<"timeout">>, #{ {<<"timeout">>, #{
example => infinity, example := infinity,
<<"oneOf">> => [ <<"oneOf">> := [
#{example => infinity, type => string}, #{type => integer} #{example := infinity, type := string},
#{type := integer}
] ]
}}, }},
{<<"bytesize">>, #{example => <<"32MB">>, type => string}}, {<<"bytesize">>, #{
{<<"wordsize">>, #{example => <<"1024KB">>, type => string}}, example := <<"32MB">>, type := string
{<<"maps">>, #{example => #{}, type => object}}, }},
{<<"wordsize">>, #{
example := <<"1024KB">>, type := string
}},
{<<"maps">>, #{example := #{}, type := object}},
{<<"comma_separated_list">>, #{ {<<"comma_separated_list">>, #{
example => <<"item1,item2">>, type => string example := <<"item1,item2">>, type := string
}}, }},
{<<"comma_separated_atoms">>, #{ {<<"comma_separated_atoms">>, #{
example => <<"item1,item2">>, type => string example := <<"item1,item2">>, type := string
}}, }},
{<<"log_level">>, #{ {<<"log_level">>, #{
enum => [ enum := [
debug, debug,
info, info,
notice, notice,
@ -364,21 +375,14 @@ t_complicated_type(_Config) ->
emergency, emergency,
all all
], ],
type => string type := string
}}, }},
{<<"fix_integer">>, #{ {<<"fix_integer">>, #{
default => 100, enum => [100], type => integer default := 100, enum := [100], type := integer
}} }}
], ],
<<"type">> => object Properties
} ),
}
}
},
{OperationId, Spec, Refs, #{}} = emqx_dashboard_swagger:parse_spec_ref(?MODULE, Path, #{}),
?assertEqual(test, OperationId),
Response = maps:get(responses, maps:get(post, Spec)),
?assertEqual(Object, maps:get(<<"200">>, Response)),
?assertEqual([], Refs), ?assertEqual([], Refs),
ok. ok.
@ -647,9 +651,8 @@ schema("/ref/complex_type") ->
{no_neg_integer, hoconsc:mk(non_neg_integer(), #{})}, {no_neg_integer, hoconsc:mk(non_neg_integer(), #{})},
{url, hoconsc:mk(url(), #{})}, {url, hoconsc:mk(url(), #{})},
{server, hoconsc:mk(emqx_schema:ip_port(), #{})}, {server, hoconsc:mk(emqx_schema:ip_port(), #{})},
{connect_timeout, {connect_timeout, hoconsc:mk(emqx_schema:timeout_duration(), #{})},
hoconsc:mk(emqx_bridge_http_connector:connect_timeout(), #{})}, {pool_type, hoconsc:mk(hoconsc:enum([random, hash]), #{})},
{pool_type, hoconsc:mk(emqx_bridge_http_connector:pool_type(), #{})},
{timeout, hoconsc:mk(timeout(), #{})}, {timeout, hoconsc:mk(timeout(), #{})},
{bytesize, hoconsc:mk(emqx_schema:bytesize(), #{})}, {bytesize, hoconsc:mk(emqx_schema:bytesize(), #{})},
{wordsize, hoconsc:mk(emqx_schema:wordsize(), #{})}, {wordsize, hoconsc:mk(emqx_schema:wordsize(), #{})},