diff --git a/modules/swagger-codegen/src/main/resources/erlang-server/api.mustache b/modules/swagger-codegen/src/main/resources/erlang-server/api.mustache index ed49d5a0a78..6e337c7773e 100644 --- a/modules/swagger-codegen/src/main/resources/erlang-server/api.mustache +++ b/modules/swagger-codegen/src/main/resources/erlang-server/api.mustache @@ -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) -> diff --git a/samples/server/petstore/erlang-server/.swagger-codegen/VERSION b/samples/server/petstore/erlang-server/.swagger-codegen/VERSION index f9f7450d135..7fea99011a6 100644 --- a/samples/server/petstore/erlang-server/.swagger-codegen/VERSION +++ b/samples/server/petstore/erlang-server/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.0-SNAPSHOT \ No newline at end of file +2.2.3-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/erlang-server/priv/swagger.json b/samples/server/petstore/erlang-server/priv/swagger.json index 808835928b6..357c31b4013 100644 --- a/samples/server/petstore/erlang-server/priv/swagger.json +++ b/samples/server/petstore/erlang-server/priv/swagger.json @@ -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" : { diff --git a/samples/server/petstore/erlang-server/src/swagger_api.erl b/samples/server/petstore/erlang-server/src/swagger_api.erl index 02e15583133..af447cf7c9a 100644 --- a/samples/server/petstore/erlang-server/src/swagger_api.erl +++ b/samples/server/petstore/erlang-server/src/swagger_api.erl @@ -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) ->