Skip to content

Commit

Permalink
Erlang server: handle broken JSON (#7129)
Browse files Browse the repository at this point in the history
  • Loading branch information
velzevur authored and wing328 committed Dec 9, 2017
1 parent e7b9738 commit b4f7074
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
2.2.3-SNAPSHOT
50 changes: 1 addition & 49 deletions samples/server/petstore/erlang-server/priv/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -709,10 +701,6 @@
},
"title" : "Pet catehgry",
"description" : "A category for a pet",
"example" : {
"name" : "name",
"id" : 6
},
"xml" : {
"name" : "Category"
}
Expand Down Expand Up @@ -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"
}
Expand All @@ -777,10 +755,6 @@
},
"title" : "Pet Tag",
"description" : "A tag for a pet",
"example" : {
"name" : "name",
"id" : 1
},
"xml" : {
"name" : "Tag"
}
Expand Down Expand Up @@ -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"
}
Expand All @@ -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" : {
Expand Down
33 changes: 24 additions & 9 deletions samples/server/petstore/erlang-server/src/swagger_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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) ->
Expand Down

0 comments on commit b4f7074

Please sign in to comment.