Merge pull request #10914 from sstrigler/EMQX-9954-broken-openapi-spec-in-swagger-json

fix broken openapi spec in swagger json
This commit is contained in:
Stefan Strigler 2023-06-02 13:35:07 +02:00 committed by GitHub
commit 5f24526ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 24 deletions

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*- %% -*- mode: erlang -*-
{application, emqx_authn, [ {application, emqx_authn, [
{description, "EMQX Authentication"}, {description, "EMQX Authentication"},
{vsn, "0.1.20"}, {vsn, "0.1.21"},
{modules, []}, {modules, []},
{registered, [emqx_authn_sup, emqx_authn_registry]}, {registered, [emqx_authn_sup, emqx_authn_registry]},
{applications, [kernel, stdlib, emqx_resource, emqx_connector, ehttpc, epgsql, mysql, jose]}, {applications, [kernel, stdlib, emqx_resource, emqx_connector, ehttpc, epgsql, mysql, jose]},

View File

@ -158,12 +158,15 @@ verify_claims(type) ->
verify_claims(desc) -> verify_claims(desc) ->
?DESC(?FUNCTION_NAME); ?DESC(?FUNCTION_NAME);
verify_claims(default) -> verify_claims(default) ->
#{}; [];
verify_claims(validator) -> verify_claims(validator) ->
[fun do_check_verify_claims/1]; [fun do_check_verify_claims/1];
verify_claims(converter) -> verify_claims(converter) ->
fun(VerifyClaims) -> fun
[{to_binary(K), V} || {K, V} <- maps:to_list(VerifyClaims)] (VerifyClaims) when is_map(VerifyClaims) ->
[{to_binary(K), V} || {K, V} <- maps:to_list(VerifyClaims)];
(VerifyClaims) ->
VerifyClaims
end; end;
verify_claims(required) -> verify_claims(required) ->
false; false;

View File

@ -59,6 +59,11 @@ start_listeners(Listeners) ->
scheme => basic, scheme => basic,
description => description =>
<<"Authorize with [API Keys](https://www.emqx.io/docs/en/v5.0/admin/api.html#api-keys)">> <<"Authorize with [API Keys](https://www.emqx.io/docs/en/v5.0/admin/api.html#api-keys)">>
},
'bearerAuth' => #{
type => http,
scheme => bearer,
description => <<"Authorize with Bearer Token">>
} }
} }
} }

View File

@ -536,6 +536,7 @@ init_prop(Keys, Init, Type) ->
format_prop(deprecated, Value) when is_boolean(Value) -> Value; format_prop(deprecated, Value) when is_boolean(Value) -> Value;
format_prop(deprecated, _) -> true; format_prop(deprecated, _) -> true;
format_prop(default, []) -> [];
format_prop(_, Schema) -> to_bin(Schema). format_prop(_, Schema) -> to_bin(Schema).
trans_required(Spec, true, _) -> Spec#{required => true}; trans_required(Spec, true, _) -> Spec#{required => true};
@ -567,18 +568,7 @@ trans_description(Spec, Hocon, Options) ->
Spec; Spec;
Desc -> Desc ->
Desc1 = binary:replace(Desc, [<<"\n">>], <<"<br/>">>, [global]), Desc1 = binary:replace(Desc, [<<"\n">>], <<"<br/>">>, [global]),
maybe_add_summary_from_label(Spec#{description => Desc1}, Hocon, Options) Spec#{description => Desc1}
end.
maybe_add_summary_from_label(Spec, Hocon, Options) ->
Label =
case desc_struct(Hocon) of
?DESC(_, _) = Struct -> get_i18n(<<"label">>, Struct, undefined, Options);
_ -> undefined
end,
case Label of
undefined -> Spec;
_ -> Spec#{summary => Label}
end. end.
get_i18n(Tag, ?DESC(Namespace, Id), Default, Options) -> get_i18n(Tag, ?DESC(Namespace, Id), Default, Options) ->
@ -970,13 +960,13 @@ to_bin(List) when is_list(List) ->
to_bin(Boolean) when is_boolean(Boolean) -> Boolean; to_bin(Boolean) when is_boolean(Boolean) -> Boolean;
to_bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8); to_bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
to_bin({Type, Args}) -> to_bin({Type, Args}) ->
unicode:characters_to_binary(io_lib:format("~ts(~p)", [Type, Args])); unicode:characters_to_binary(io_lib:format("~ts-~p", [Type, Args]));
to_bin(X) -> to_bin(X) ->
X. X.
parse_object(PropList = [_ | _], Module, Options) when is_list(PropList) -> parse_object(PropList = [_ | _], Module, Options) when is_list(PropList) ->
{Props, Required, Refs} = parse_object_loop(PropList, Module, Options), {Props, Required, Refs} = parse_object_loop(PropList, Module, Options),
Object = #{<<"type">> => object, <<"properties">> => Props}, Object = #{<<"type">> => object, <<"properties">> => fix_empty_props(Props)},
case Required of case Required of
[] -> {Object, Refs}; [] -> {Object, Refs};
_ -> {maps:put(required, Required, Object), Refs} _ -> {maps:put(required, Required, Object), Refs}
@ -1012,7 +1002,10 @@ parse_object_loop([{Name, Hocon} | Rest], Module, Options, Props, Required, Refs
HoconType = hocon_schema:field_schema(Hocon, type), HoconType = hocon_schema:field_schema(Hocon, type),
Init0 = init_prop([default | ?DEFAULT_FIELDS], #{}, Hocon), Init0 = init_prop([default | ?DEFAULT_FIELDS], #{}, Hocon),
SchemaToSpec = schema_converter(Options), SchemaToSpec = schema_converter(Options),
Init = trans_desc(Init0, Hocon, SchemaToSpec, NameBin, Options), Init = maps:remove(
summary,
trans_desc(Init0, Hocon, SchemaToSpec, NameBin, Options)
),
{Prop, Refs1} = SchemaToSpec(HoconType, Module), {Prop, Refs1} = SchemaToSpec(HoconType, Module),
NewRequiredAcc = NewRequiredAcc =
case is_required(Hocon) of case is_required(Hocon) of
@ -1043,6 +1036,11 @@ is_hidden(Hocon) ->
is_required(Hocon) -> is_required(Hocon) ->
hocon_schema:field_schema(Hocon, required) =:= true. hocon_schema:field_schema(Hocon, required) =:= true.
fix_empty_props([]) ->
#{};
fix_empty_props(Props) ->
Props.
content(ApiSpec) -> content(ApiSpec) ->
content(ApiSpec, undefined). content(ApiSpec, undefined).

View File

@ -344,9 +344,7 @@ fields(bytes) ->
description => ?DESC(max_response_bytes), description => ?DESC(max_response_bytes),
in => query, in => query,
required => false, required => false,
default => 1000, default => 1000
minimum => 0,
maximum => ?MAX_SINT32
} }
)} )}
]; ];

View File

@ -2,7 +2,7 @@
{application, emqx_prometheus, [ {application, emqx_prometheus, [
{description, "Prometheus for EMQX"}, {description, "Prometheus for EMQX"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.11"}, {vsn, "5.0.12"},
{modules, []}, {modules, []},
{registered, [emqx_prometheus_sup]}, {registered, [emqx_prometheus_sup]},
{applications, [kernel, stdlib, prometheus, emqx, emqx_management]}, {applications, [kernel, stdlib, prometheus, emqx, emqx_management]},

View File

@ -59,7 +59,7 @@ fields("prometheus") ->
?HOCON( ?HOCON(
list({string(), string()}), list({string(), string()}),
#{ #{
default => #{}, default => [],
required => false, required => false,
converter => fun ?MODULE:convert_headers/1, converter => fun ?MODULE:convert_headers/1,
desc => ?DESC(headers) desc => ?DESC(headers)
@ -149,6 +149,8 @@ fields("prometheus") ->
desc("prometheus") -> ?DESC(prometheus); desc("prometheus") -> ?DESC(prometheus);
desc(_) -> undefined. desc(_) -> undefined.
convert_headers(<<>>) ->
[];
convert_headers(Headers) when is_map(Headers) -> convert_headers(Headers) when is_map(Headers) ->
maps:fold( maps:fold(
fun(K, V, Acc) -> fun(K, V, Acc) ->