Erlang-server: fix dialyzer; fix min and max checks (#6752)

This commit is contained in:
Dimitar Ivanov 2017-10-25 05:57:53 +03:00 committed by wing328
parent f4ac6feae2
commit ab338f8cb9
13 changed files with 85 additions and 106 deletions

View File

@ -4,6 +4,8 @@
-export([request_param_info/2]). -export([request_param_info/2]).
-export([populate_request/3]). -export([populate_request/3]).
-export([validate_response/4]). -export([validate_response/4]).
%% exported to silence swagger complains
-export([get_value/3, validate_response_body/4]).
-type operation_id() :: atom(). -type operation_id() :: atom().
-type request_param() :: atom(). -type request_param() :: atom().
@ -212,7 +214,7 @@ validate(Rule = {enum, Values}, Name, Value, _ValidatorState) ->
end; end;
validate(Rule = {max, Max}, Name, Value, _ValidatorState) -> validate(Rule = {max, Max}, Name, Value, _ValidatorState) ->
case Value >= Max of case Value =< Max of
true -> ok; true -> ok;
false -> validation_error(Rule, Name) false -> validation_error(Rule, Name)
end; end;
@ -224,7 +226,7 @@ validate(Rule = {exclusive_max, ExclusiveMax}, Name, Value, _ValidatorState) ->
end; end;
validate(Rule = {min, Min}, Name, Value, _ValidatorState) -> validate(Rule = {min, Min}, Name, Value, _ValidatorState) ->
case Value =< Min of case Value >= Min of
true -> ok; true -> ok;
false -> validation_error(Rule, Name) false -> validation_error(Rule, Name)
end; end;
@ -290,6 +292,8 @@ validation_error(ViolatedRule, Name) ->
validation_error(ViolatedRule, Name, Info) -> validation_error(ViolatedRule, Name, Info) ->
throw({wrong_param, Name, ViolatedRule, Info}). throw({wrong_param, Name, ViolatedRule, Info}).
-spec get_value(body | qs_val | header | binding, Name :: any(), Req0 :: cowboy_req:req()) ->
{Value :: any(), Req :: cowboy_req:req()}.
get_value(body, _Name, Req0) -> get_value(body, _Name, Req0) ->
{ok, Body, Req} = cowboy_req:body(Req0), {ok, Body, Req} = cowboy_req:body(Req0),
Value = prepare_body(Body), Value = prepare_body(Body),

View File

@ -24,8 +24,12 @@ authorize_api_key(LogicHandler, OperationID, From, KeyParam, Req0) ->
ApiKey ApiKey
), ),
case Result of case Result of
{{#authMethods}}
{{#isApiKey}}
{true, Context} -> {true, Context} ->
{true, Context, Req}; {true, Context, Req};
{{/isApiKey}}
{{/authMethods}}
false -> false ->
AuthHeader = <<"">>, AuthHeader = <<"">>,
{false, AuthHeader, Req} {false, AuthHeader, Req}

View File

@ -94,13 +94,15 @@ is_authorized(
end; end;
{{/isApiKey}} {{/isApiKey}}
{{/authMethods}} {{/authMethods}}
{{/operation}}{{/operations}}
{{^authMethods}} {{^authMethods}}
is_authorized(Req, State) -> is_authorized(Req, State) ->
{true, Req, State}; {true, Req, State}.
{{/authMethods}} {{/authMethods}}
{{/operation}}{{/operations}} {{#authMethods}}
is_authorized(Req, State) -> is_authorized(Req, State) ->
{{false, <<"">>}, Req, State}. {{false, <<"">>}, Req, State}.
{{/authMethods}}
-spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -1,11 +1,7 @@
-module({{packageName}}_logic_handler). -module({{packageName}}_logic_handler).
-export([handle_request/4]). -export([handle_request/4]).
{{#authMethods}}
{{#isApiKey}}
-export([authorize_api_key/3]). -export([authorize_api_key/3]).
{{/isApiKey}}
{{/authMethods}}
-type context() :: #{binary() => any()}. -type context() :: #{binary() => any()}.
-type handler_response() ::{ -type handler_response() ::{
Status :: cowboy:http_status(), Status :: cowboy:http_status(),
@ -48,3 +44,9 @@ authorize_api_key(Handler, OperationID, ApiKey) ->
Handler:authorize_api_key(OperationID, ApiKey). Handler:authorize_api_key(OperationID, ApiKey).
{{/isApiKey}} {{/isApiKey}}
{{/authMethods}} {{/authMethods}}
{{^authMethods}}
-spec authorize_api_key(Handler :: atom(), OperationID :: {{packageName}}_api:operation_id(), ApiKey :: binary()) ->
Result :: false.
authorize_api_key(_Handler, _OperationID, _ApiKey) ->
false.
{{/authMethods}}

View File

@ -9,6 +9,7 @@
-spec child_spec( ID :: any(), #{ -spec child_spec( ID :: any(), #{
ip => inet:ip_address(), ip => inet:ip_address(),
port => inet:port_number(), port => inet:port_number(),
logic_handler => module(),
net_opts => [] net_opts => []
}) -> supervisor:child_spec(). }) -> supervisor:child_spec().

View File

@ -1 +1 @@
2.2.3-SNAPSHOT 2.3.0-SNAPSHOT

View File

@ -9,7 +9,7 @@
"email" : "apiteam@swagger.io" "email" : "apiteam@swagger.io"
}, },
"license" : { "license" : {
"name" : "Apache 2.0", "name" : "Apache-2.0",
"url" : "http://www.apache.org/licenses/LICENSE-2.0.html" "url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
} }
}, },
@ -166,7 +166,8 @@
}, },
"security" : [ { "security" : [ {
"petstore_auth" : [ "write:pets", "read:pets" ] "petstore_auth" : [ "write:pets", "read:pets" ]
} ] } ],
"deprecated" : true
} }
}, },
"/pet/{petId}" : { "/pet/{petId}" : {
@ -683,6 +684,14 @@
}, },
"title" : "Pet Order", "title" : "Pet Order",
"description" : "An order for a pets from the pet store", "description" : "An order for a pets from the pet store",
"example" : {
"petId" : 6,
"quantity" : 1,
"id" : 0,
"shipDate" : "2000-01-23T04:56:07.000+00:00",
"complete" : false,
"status" : "placed"
},
"xml" : { "xml" : {
"name" : "Order" "name" : "Order"
} }
@ -700,6 +709,10 @@
}, },
"title" : "Pet catehgry", "title" : "Pet catehgry",
"description" : "A category for a pet", "description" : "A category for a pet",
"example" : {
"name" : "name",
"id" : 6
},
"xml" : { "xml" : {
"name" : "Category" "name" : "Category"
} }
@ -737,6 +750,16 @@
}, },
"title" : "a User", "title" : "a User",
"description" : "A User who is purchasing from the pet store", "description" : "A User who is purchasing from the pet store",
"example" : {
"firstName" : "firstName",
"lastName" : "lastName",
"password" : "password",
"userStatus" : 6,
"phone" : "phone",
"id" : 0,
"email" : "email",
"username" : "username"
},
"xml" : { "xml" : {
"name" : "User" "name" : "User"
} }
@ -754,6 +777,10 @@
}, },
"title" : "Pet Tag", "title" : "Pet Tag",
"description" : "A tag for a pet", "description" : "A tag for a pet",
"example" : {
"name" : "name",
"id" : 1
},
"xml" : { "xml" : {
"name" : "Tag" "name" : "Tag"
} }
@ -801,6 +828,23 @@
}, },
"title" : "a Pet", "title" : "a Pet",
"description" : "A pet for sale in the pet store", "description" : "A pet for sale in the pet store",
"example" : {
"photoUrls" : [ "photoUrls", "photoUrls" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "name",
"id" : 6
},
"tags" : [ {
"name" : "name",
"id" : 1
}, {
"name" : "name",
"id" : 1
} ],
"status" : "available"
},
"xml" : { "xml" : {
"name" : "Pet" "name" : "Pet"
} }
@ -820,7 +864,12 @@
} }
}, },
"title" : "An uploaded response", "title" : "An uploaded response",
"description" : "Describes the result of uploading an image resource" "description" : "Describes the result of uploading an image resource",
"example" : {
"code" : 0,
"type" : "type",
"message" : "message"
}
} }
}, },
"externalDocs" : { "externalDocs" : {

View File

@ -14,6 +14,6 @@
{env, [ {env, [
]}, ]},
{modules, []}, {modules, []},
{licenses, ["Apache 2.0"]}, {licenses, ["Apache-2.0"]},
{links, []} {links, []}
]}. ]}.

View File

@ -4,6 +4,8 @@
-export([request_param_info/2]). -export([request_param_info/2]).
-export([populate_request/3]). -export([populate_request/3]).
-export([validate_response/4]). -export([validate_response/4]).
%% exported to silence swagger complains
-export([get_value/3, validate_response_body/4]).
-type operation_id() :: atom(). -type operation_id() :: atom().
-type request_param() :: atom(). -type request_param() :: atom().
@ -599,7 +601,7 @@ validate(Rule = {enum, Values}, Name, Value, _ValidatorState) ->
end; end;
validate(Rule = {max, Max}, Name, Value, _ValidatorState) -> validate(Rule = {max, Max}, Name, Value, _ValidatorState) ->
case Value >= Max of case Value =< Max of
true -> ok; true -> ok;
false -> validation_error(Rule, Name) false -> validation_error(Rule, Name)
end; end;
@ -611,7 +613,7 @@ validate(Rule = {exclusive_max, ExclusiveMax}, Name, Value, _ValidatorState) ->
end; end;
validate(Rule = {min, Min}, Name, Value, _ValidatorState) -> validate(Rule = {min, Min}, Name, Value, _ValidatorState) ->
case Value =< Min of case Value >= Min of
true -> ok; true -> ok;
false -> validation_error(Rule, Name) false -> validation_error(Rule, Name)
end; end;
@ -677,6 +679,8 @@ validation_error(ViolatedRule, Name) ->
validation_error(ViolatedRule, Name, Info) -> validation_error(ViolatedRule, Name, Info) ->
throw({wrong_param, Name, ViolatedRule, Info}). throw({wrong_param, Name, ViolatedRule, Info}).
-spec get_value(body | qs_val | header | binding, Name :: any(), Req0 :: cowboy_req:req()) ->
{Value :: any(), Req :: cowboy_req:req()}.
get_value(body, _Name, Req0) -> get_value(body, _Name, Req0) ->
{ok, Body, Req} = cowboy_req:body(Req0), {ok, Body, Req} = cowboy_req:body(Req0),
Value = prepare_body(Body), Value = prepare_body(Body),

View File

@ -204,7 +204,7 @@ is_authorized(
) -> ) ->
is_authorized(Req, State) -> is_authorized(Req, State) ->
{{false, <<"">>}, Req, State}. {true, Req, State}.
-spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -9,6 +9,7 @@
-spec child_spec( ID :: any(), #{ -spec child_spec( ID :: any(), #{
ip => inet:ip_address(), ip => inet:ip_address(),
port => inet:port_number(), port => inet:port_number(),
logic_handler => module(),
net_opts => [] net_opts => []
}) -> supervisor:child_spec(). }) -> supervisor:child_spec().

View File

@ -95,14 +95,6 @@ allowed_methods(Req, State) ->
State :: state() State :: state()
}. }.
is_authorized(
Req0,
State = #state{
operation_id = 'DeleteOrder' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized( is_authorized(
Req0, Req0,
@ -124,26 +116,10 @@ is_authorized(
{false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State}
end; end;
is_authorized(
Req0,
State = #state{
operation_id = 'GetOrderById' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'PlaceOrder' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(Req, State) -> is_authorized(Req, State) ->
{{false, <<"">>}, Req, State}. {true, Req, State}.
-spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -127,80 +127,16 @@ allowed_methods(Req, State) ->
State :: state() State :: state()
}. }.
is_authorized(
Req0,
State = #state{
operation_id = 'CreateUser' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'CreateUsersWithArrayInput' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'CreateUsersWithListInput' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'DeleteUser' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'GetUserByName' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'LoginUser' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'LogoutUser' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(
Req0,
State = #state{
operation_id = 'UpdateUser' = OperationID,
logic_handler = LogicHandler
}
) ->
{true, Req0, State};
is_authorized(Req, State) -> is_authorized(Req, State) ->
{{false, <<"">>}, Req, State}. {true, Req, State}.
-spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {