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

View File

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

View File

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

View File

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