Erlang server: handle broken JSON (#7129)

This commit is contained in:
Dimitar Ivanov
2017-12-09 11:57:48 +02:00
committed by William Cheng
parent e7b97383d1
commit b4f707450a
4 changed files with 50 additions and 68 deletions

View File

@@ -107,11 +107,15 @@ populate_request_params(OperationID, [FieldParams | T], Req0, ValidatorState, Mo
populate_request_param(OperationID, Name, Req0, ValidatorState) ->
#{rules := Rules, source := Source} = request_param_info(OperationID, Name),
{Value, Req} = get_value(Source, Name, Req0),
case prepare_param(Rules, Name, Value, ValidatorState) of
{ok, Result} -> {ok, Name, Result, Req};
{error, Reason} ->
{error, Reason, Req}
case get_value(Source, Name, Req0) of
{error, Reason, Req} ->
{error, Reason, Req};
{Value, Req} ->
case prepare_param(Rules, Name, Value, ValidatorState) of
{ok, Result} -> {ok, Name, Result, Req};
{error, Reason} ->
{error, Reason, Req}
end
end.
-spec validate_response(
@@ -293,11 +297,16 @@ validation_error(ViolatedRule, Name, 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()}.
{Value :: any(), Req :: cowboy_req:req()} |
{error, Reason :: any(), Req :: cowboy_req:req()}.
get_value(body, _Name, Req0) ->
{ok, Body, Req} = cowboy_req:body(Req0),
Value = prepare_body(Body),
{Value, Req};
case prepare_body(Body) of
{error, Reason} ->
{error, Reason, Req};
Value ->
{Value, Req}
end;
get_value(qs_val, Name, Req0) ->
{QS, Req} = cowboy_req:qs_vals(Req0),
@@ -317,7 +326,13 @@ get_value(binding, Name, Req0) ->
prepare_body(Body) ->
case Body of
<<"">> -> <<"">>;
_ -> jsx:decode(Body, [return_maps])
_ ->
try
jsx:decode(Body, [return_maps])
catch
error:_ ->
{error, {invalid_body, not_json, Body}}
end
end.
validate_with_schema(Body, Definition, ValidatorState) ->

View File

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

View File

@@ -684,14 +684,6 @@
},
"title" : "Pet Order",
"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" : {
"name" : "Order"
}
@@ -709,10 +701,6 @@
},
"title" : "Pet catehgry",
"description" : "A category for a pet",
"example" : {
"name" : "name",
"id" : 6
},
"xml" : {
"name" : "Category"
}
@@ -750,16 +738,6 @@
},
"title" : "a User",
"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" : {
"name" : "User"
}
@@ -777,10 +755,6 @@
},
"title" : "Pet Tag",
"description" : "A tag for a pet",
"example" : {
"name" : "name",
"id" : 1
},
"xml" : {
"name" : "Tag"
}
@@ -828,23 +802,6 @@
},
"title" : "a Pet",
"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" : {
"name" : "Pet"
}
@@ -864,12 +821,7 @@
}
},
"title" : "An uploaded response",
"description" : "Describes the result of uploading an image resource",
"example" : {
"code" : 0,
"type" : "type",
"message" : "message"
}
"description" : "Describes the result of uploading an image resource"
}
},
"externalDocs" : {

View File

@@ -407,11 +407,15 @@ populate_request_params(OperationID, [FieldParams | T], Req0, ValidatorState, Mo
populate_request_param(OperationID, Name, Req0, ValidatorState) ->
#{rules := Rules, source := Source} = request_param_info(OperationID, Name),
{Value, Req} = get_value(Source, Name, Req0),
case prepare_param(Rules, Name, Value, ValidatorState) of
{ok, Result} -> {ok, Name, Result, Req};
{error, Reason} ->
{error, Reason, Req}
case get_value(Source, Name, Req0) of
{error, Reason, Req} ->
{error, Reason, Req};
{Value, Req} ->
case prepare_param(Rules, Name, Value, ValidatorState) of
{ok, Result} -> {ok, Name, Result, Req};
{error, Reason} ->
{error, Reason, Req}
end
end.
-spec validate_response(
@@ -680,11 +684,16 @@ validation_error(ViolatedRule, Name, 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()}.
{Value :: any(), Req :: cowboy_req:req()} |
{error, Reason :: any(), Req :: cowboy_req:req()}.
get_value(body, _Name, Req0) ->
{ok, Body, Req} = cowboy_req:body(Req0),
Value = prepare_body(Body),
{Value, Req};
case prepare_body(Body) of
{error, Reason} ->
{error, Reason, Req};
Value ->
{Value, Req}
end;
get_value(qs_val, Name, Req0) ->
{QS, Req} = cowboy_req:qs_vals(Req0),
@@ -704,7 +713,13 @@ get_value(binding, Name, Req0) ->
prepare_body(Body) ->
case Body of
<<"">> -> <<"">>;
_ -> jsx:decode(Body, [return_maps])
_ ->
try
jsx:decode(Body, [return_maps])
catch
error:_ ->
{error, {invalid_body, not_json, Body}}
end
end.
validate_with_schema(Body, Definition, ValidatorState) ->