chore(swagger): check hocon schema with #{override_env => false}

This commit is contained in:
zhongwencool 2021-09-22 09:07:14 +08:00
parent dab5fbf285
commit 39423665e9
4 changed files with 18 additions and 19 deletions

View File

@ -85,11 +85,11 @@ check_parameter([{Name, Type} | Spec], Bindings, QueryStr, BindingsAcc, QueryStr
Schema = ?INIT_SCHEMA#{roots => [{Name, Type}]}, Schema = ?INIT_SCHEMA#{roots => [{Name, Type}]},
case hocon_schema:field_schema(Type, in) of case hocon_schema:field_schema(Type, in) of
path -> path ->
NewBindings = hocon_schema:check_plain(Schema, Bindings, #{atom_key => true}), NewBindings = hocon_schema:check_plain(Schema, Bindings, #{atom_key => true, override_env => false}),
NewBindingsAcc = maps:merge(BindingsAcc, NewBindings), NewBindingsAcc = maps:merge(BindingsAcc, NewBindings),
check_parameter(Spec, Bindings, QueryStr, NewBindingsAcc, QueryStrAcc); check_parameter(Spec, Bindings, QueryStr, NewBindingsAcc, QueryStrAcc);
query -> query ->
NewQueryStr = hocon_schema:check_plain(Schema, QueryStr), NewQueryStr = hocon_schema:check_plain(Schema, QueryStr, #{override_env => false}),
NewQueryStrAcc = maps:merge(QueryStrAcc, NewQueryStr), NewQueryStrAcc = maps:merge(QueryStrAcc, NewQueryStr),
check_parameter(Spec, Bindings, QueryStr, BindingsAcc, NewQueryStrAcc) check_parameter(Spec, Bindings, QueryStr, BindingsAcc, NewQueryStrAcc)
end. end.
@ -102,7 +102,7 @@ check_requestBody(#{body := Body}, Schema, Module, true) ->
_ -> Type0 _ -> Type0
end, end,
NewSchema = ?INIT_SCHEMA#{roots => [{root, Type}]}, NewSchema = ?INIT_SCHEMA#{roots => [{root, Type}]},
#{<<"root">> := NewBody} = hocon_schema:check_plain(NewSchema, #{<<"root">> => Body}), #{<<"root">> := NewBody} = hocon_schema:check_plain(NewSchema, #{<<"root">> => Body}, #{override_env => false}),
NewBody; NewBody;
%% TODO not support nest object check yet, please use ref! %% TODO not support nest object check yet, please use ref!
%% RequestBody = [ {per_page, mk(integer(), #{}}, %% RequestBody = [ {per_page, mk(integer(), #{}},

View File

@ -17,9 +17,8 @@
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-export([ roots/0, fields/1,namespace/0]). -export([ roots/0, fields/1]).
-import(hoconsc, [mk/2]). -import(hoconsc, [mk/2]).
namespace() -> <<"remote">>.
roots() -> ["root"]. roots() -> ["root"].
fields("root") -> fields("root") ->

View File

@ -91,15 +91,15 @@ t_remote_ref(_Config) ->
Spec = #{ Spec = #{
post => #{parameters => [], post => #{parameters => [],
requestBody => #{<<"content">> => #{<<"application/json">> => requestBody => #{<<"content">> => #{<<"application/json">> =>
#{<<"schema">> => #{<<"$ref">> => <<"#/components/schemas/remote.ref2">>}}}}, #{<<"schema">> => #{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref2">>}}}},
responses => #{<<"200">> => #{description => <<"ok">>}}}}, responses => #{<<"200">> => #{description => <<"ok">>}}}},
Refs = [{emqx_swagger_remote_schema, "ref2"}], Refs = [{emqx_swagger_remote_schema, "ref2"}],
{_, Components} = validate("/ref/remote", Spec, Refs), {_, Components} = validate("/ref/remote", Spec, Refs),
ExpectComponents = [ ExpectComponents = [
#{<<"remote.ref2">> => #{<<"properties">> => [ #{<<"emqx_swagger_remote_schema.ref2">> => #{<<"properties">> => [
{<<"page">>, #{description => <<"good page">>,example => 1, maximum => 100,minimum => 1,type => integer}}, {<<"page">>, #{description => <<"good page">>,example => 1, maximum => 100,minimum => 1,type => integer}},
{<<"another_ref">>, #{<<"$ref">> => <<"#/components/schemas/remote.ref3">>}}], <<"type">> => object}}, {<<"another_ref">>, #{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref3">>}}], <<"type">> => object}},
#{<<"remote.ref3">> => #{<<"properties">> => [ #{<<"emqx_swagger_remote_schema.ref3">> => #{<<"properties">> => [
{<<"ip">>, #{description => <<"IP:Port">>, example => <<"127.0.0.1:80">>,type => string}}, {<<"ip">>, #{description => <<"IP:Port">>, example => <<"127.0.0.1:80">>,type => string}},
{<<"version">>, #{description => "a good version", example => <<"1.0.0">>,type => string}}], {<<"version">>, #{description => "a good version", example => <<"1.0.0">>,type => string}}],
<<"type">> => object}}], <<"type">> => object}}],

View File

@ -84,7 +84,7 @@ t_raw_remote_ref(_Config) ->
Path = "/raw/ref/remote", Path = "/raw/ref/remote",
Object = #{<<"content">> => Object = #{<<"content">> =>
#{<<"application/json">> => #{<<"schema">> => #{ #{<<"application/json">> => #{<<"schema">> => #{
<<"$ref">> => <<"#/components/schemas/remote.ref1">>}}}}, <<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref1">>}}}},
ExpectRefs = [{emqx_swagger_remote_schema, "ref1"}], ExpectRefs = [{emqx_swagger_remote_schema, "ref1"}],
validate(Path, Object, ExpectRefs), validate(Path, Object, ExpectRefs),
ok. ok.
@ -101,7 +101,7 @@ t_remote_ref(_Config) ->
Path = "/ref/remote", Path = "/ref/remote",
Object = #{<<"content">> => Object = #{<<"content">> =>
#{<<"application/json">> => #{<<"schema">> => #{ #{<<"application/json">> => #{<<"schema">> => #{
<<"$ref">> => <<"#/components/schemas/remote.ref1">>}}}}, <<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref1">>}}}},
ExpectRefs = [{emqx_swagger_remote_schema, "ref1"}], ExpectRefs = [{emqx_swagger_remote_schema, "ref1"}],
validate(Path, Object, ExpectRefs), validate(Path, Object, ExpectRefs),
ok. ok.
@ -159,29 +159,29 @@ t_ref_array_without_key(_Config) ->
t_hocon_schema_function(_Config) -> t_hocon_schema_function(_Config) ->
Path = "/ref/hocon/schema/function", Path = "/ref/hocon/schema/function",
Object = #{<<"content">> => #{<<"application/json">> => #{<<"schema">> => Object = #{<<"content">> => #{<<"application/json">> => #{<<"schema">> =>
#{<<"$ref">> => <<"#/components/schemas/remote.root">>}}}}, #{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.root">>}}}},
ExpectComponents = [ ExpectComponents = [
#{<<"remote.ref1">> => #{<<"type">> => object, #{<<"emqx_swagger_remote_schema.ref1">> => #{<<"type">> => object,
<<"properties">> => [ <<"properties">> => [
{<<"protocol">>, #{enum => [http, https], type => string}}, {<<"protocol">>, #{enum => [http, https], type => string}},
{<<"port">>, #{default => 18083, example => 100, type => integer}}] {<<"port">>, #{default => 18083, example => 100, type => integer}}]
}}, }},
#{<<"remote.ref2">> => #{<<"type">> => object, #{<<"emqx_swagger_remote_schema.ref2">> => #{<<"type">> => object,
<<"properties">> => [ <<"properties">> => [
{<<"page">>, #{description => <<"good page">>, example => 1, maximum => 100, minimum => 1, type => integer}}, {<<"page">>, #{description => <<"good page">>, example => 1, maximum => 100, minimum => 1, type => integer}},
{<<"another_ref">>, #{<<"$ref">> => <<"#/components/schemas/remote.ref3">>}} {<<"another_ref">>, #{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref3">>}}
] ]
}}, }},
#{<<"remote.ref3">> => #{<<"type">> => object, #{<<"emqx_swagger_remote_schema.ref3">> => #{<<"type">> => object,
<<"properties">> => [ <<"properties">> => [
{<<"ip">>, #{description => <<"IP:Port">>, example => <<"127.0.0.1:80">>,type => string}}, {<<"ip">>, #{description => <<"IP:Port">>, example => <<"127.0.0.1:80">>,type => string}},
{<<"version">>, #{description => "a good version", example => <<"1.0.0">>, type => string}}] {<<"version">>, #{description => "a good version", example => <<"1.0.0">>, type => string}}]
}}, }},
#{<<"remote.root">> => #{required => [<<"default_password">>, <<"default_username">>], #{<<"emqx_swagger_remote_schema.root">> => #{required => [<<"default_password">>, <<"default_username">>],
<<"properties">> => [{<<"listeners">>, #{items => <<"properties">> => [{<<"listeners">>, #{items =>
#{<<"oneOf">> => #{<<"oneOf">> =>
[#{<<"$ref">> => <<"#/components/schemas/remote.ref2">>}, [#{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref2">>},
#{<<"$ref">> => <<"#/components/schemas/remote.ref1">>}]}, type => array}}, #{<<"$ref">> => <<"#/components/schemas/emqx_swagger_remote_schema.ref1">>}]}, type => array}},
{<<"default_username">>, {<<"default_username">>,
#{default => <<"admin">>, example => <<"string example">>, type => string}}, #{default => <<"admin">>, example => <<"string example">>, type => string}},
{<<"default_password">>, #{default => <<"public">>, example => <<"string example">>, type => string}}, {<<"default_password">>, #{default => <<"public">>, example => <<"string example">>, type => string}},