Skip to content

Commit b4f7074

Browse files
velzevurwing328
authored andcommitted
Erlang server: handle broken JSON (#7129)
1 parent e7b9738 commit b4f7074

File tree

4 files changed

+50
-68
lines changed

4 files changed

+50
-68
lines changed

modules/swagger-codegen/src/main/resources/erlang-server/api.mustache

+24-9
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,15 @@ populate_request_params(OperationID, [FieldParams | T], Req0, ValidatorState, Mo
107107

108108
populate_request_param(OperationID, Name, Req0, ValidatorState) ->
109109
#{rules := Rules, source := Source} = request_param_info(OperationID, Name),
110-
{Value, Req} = get_value(Source, Name, Req0),
111-
case prepare_param(Rules, Name, Value, ValidatorState) of
112-
{ok, Result} -> {ok, Name, Result, Req};
113-
{error, Reason} ->
114-
{error, Reason, Req}
110+
case get_value(Source, Name, Req0) of
111+
{error, Reason, Req} ->
112+
{error, Reason, Req};
113+
{Value, Req} ->
114+
case prepare_param(Rules, Name, Value, ValidatorState) of
115+
{ok, Result} -> {ok, Name, Result, Req};
116+
{error, Reason} ->
117+
{error, Reason, Req}
118+
end
115119
end.
116120

117121
-spec validate_response(
@@ -293,11 +297,16 @@ validation_error(ViolatedRule, Name, Info) ->
293297
throw({wrong_param, Name, ViolatedRule, Info}).
294298

295299
-spec get_value(body | qs_val | header | binding, Name :: any(), Req0 :: cowboy_req:req()) ->
296-
{Value :: any(), Req :: cowboy_req:req()}.
300+
{Value :: any(), Req :: cowboy_req:req()} |
301+
{error, Reason :: any(), Req :: cowboy_req:req()}.
297302
get_value(body, _Name, Req0) ->
298303
{ok, Body, Req} = cowboy_req:body(Req0),
299-
Value = prepare_body(Body),
300-
{Value, Req};
304+
case prepare_body(Body) of
305+
{error, Reason} ->
306+
{error, Reason, Req};
307+
Value ->
308+
{Value, Req}
309+
end;
301310

302311
get_value(qs_val, Name, Req0) ->
303312
{QS, Req} = cowboy_req:qs_vals(Req0),
@@ -317,7 +326,13 @@ get_value(binding, Name, Req0) ->
317326
prepare_body(Body) ->
318327
case Body of
319328
<<"">> -> <<"">>;
320-
_ -> jsx:decode(Body, [return_maps])
329+
_ ->
330+
try
331+
jsx:decode(Body, [return_maps])
332+
catch
333+
error:_ ->
334+
{error, {invalid_body, not_json, Body}}
335+
end
321336
end.
322337

323338
validate_with_schema(Body, Definition, ValidatorState) ->
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0-SNAPSHOT
1+
2.2.3-SNAPSHOT

samples/server/petstore/erlang-server/priv/swagger.json

+1-49
Original file line numberDiff line numberDiff line change
@@ -684,14 +684,6 @@
684684
},
685685
"title" : "Pet Order",
686686
"description" : "An order for a pets from the pet store",
687-
"example" : {
688-
"petId" : 6,
689-
"quantity" : 1,
690-
"id" : 0,
691-
"shipDate" : "2000-01-23T04:56:07.000+00:00",
692-
"complete" : false,
693-
"status" : "placed"
694-
},
695687
"xml" : {
696688
"name" : "Order"
697689
}
@@ -709,10 +701,6 @@
709701
},
710702
"title" : "Pet catehgry",
711703
"description" : "A category for a pet",
712-
"example" : {
713-
"name" : "name",
714-
"id" : 6
715-
},
716704
"xml" : {
717705
"name" : "Category"
718706
}
@@ -750,16 +738,6 @@
750738
},
751739
"title" : "a User",
752740
"description" : "A User who is purchasing from the pet store",
753-
"example" : {
754-
"firstName" : "firstName",
755-
"lastName" : "lastName",
756-
"password" : "password",
757-
"userStatus" : 6,
758-
"phone" : "phone",
759-
"id" : 0,
760-
"email" : "email",
761-
"username" : "username"
762-
},
763741
"xml" : {
764742
"name" : "User"
765743
}
@@ -777,10 +755,6 @@
777755
},
778756
"title" : "Pet Tag",
779757
"description" : "A tag for a pet",
780-
"example" : {
781-
"name" : "name",
782-
"id" : 1
783-
},
784758
"xml" : {
785759
"name" : "Tag"
786760
}
@@ -828,23 +802,6 @@
828802
},
829803
"title" : "a Pet",
830804
"description" : "A pet for sale in the pet store",
831-
"example" : {
832-
"photoUrls" : [ "photoUrls", "photoUrls" ],
833-
"name" : "doggie",
834-
"id" : 0,
835-
"category" : {
836-
"name" : "name",
837-
"id" : 6
838-
},
839-
"tags" : [ {
840-
"name" : "name",
841-
"id" : 1
842-
}, {
843-
"name" : "name",
844-
"id" : 1
845-
} ],
846-
"status" : "available"
847-
},
848805
"xml" : {
849806
"name" : "Pet"
850807
}
@@ -864,12 +821,7 @@
864821
}
865822
},
866823
"title" : "An uploaded response",
867-
"description" : "Describes the result of uploading an image resource",
868-
"example" : {
869-
"code" : 0,
870-
"type" : "type",
871-
"message" : "message"
872-
}
824+
"description" : "Describes the result of uploading an image resource"
873825
}
874826
},
875827
"externalDocs" : {

samples/server/petstore/erlang-server/src/swagger_api.erl

+24-9
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,15 @@ populate_request_params(OperationID, [FieldParams | T], Req0, ValidatorState, Mo
407407

408408
populate_request_param(OperationID, Name, Req0, ValidatorState) ->
409409
#{rules := Rules, source := Source} = request_param_info(OperationID, Name),
410-
{Value, Req} = get_value(Source, Name, Req0),
411-
case prepare_param(Rules, Name, Value, ValidatorState) of
412-
{ok, Result} -> {ok, Name, Result, Req};
413-
{error, Reason} ->
414-
{error, Reason, Req}
410+
case get_value(Source, Name, Req0) of
411+
{error, Reason, Req} ->
412+
{error, Reason, Req};
413+
{Value, Req} ->
414+
case prepare_param(Rules, Name, Value, ValidatorState) of
415+
{ok, Result} -> {ok, Name, Result, Req};
416+
{error, Reason} ->
417+
{error, Reason, Req}
418+
end
415419
end.
416420

417421
-spec validate_response(
@@ -680,11 +684,16 @@ validation_error(ViolatedRule, Name, Info) ->
680684
throw({wrong_param, Name, ViolatedRule, Info}).
681685

682686
-spec get_value(body | qs_val | header | binding, Name :: any(), Req0 :: cowboy_req:req()) ->
683-
{Value :: any(), Req :: cowboy_req:req()}.
687+
{Value :: any(), Req :: cowboy_req:req()} |
688+
{error, Reason :: any(), Req :: cowboy_req:req()}.
684689
get_value(body, _Name, Req0) ->
685690
{ok, Body, Req} = cowboy_req:body(Req0),
686-
Value = prepare_body(Body),
687-
{Value, Req};
691+
case prepare_body(Body) of
692+
{error, Reason} ->
693+
{error, Reason, Req};
694+
Value ->
695+
{Value, Req}
696+
end;
688697

689698
get_value(qs_val, Name, Req0) ->
690699
{QS, Req} = cowboy_req:qs_vals(Req0),
@@ -704,7 +713,13 @@ get_value(binding, Name, Req0) ->
704713
prepare_body(Body) ->
705714
case Body of
706715
<<"">> -> <<"">>;
707-
_ -> jsx:decode(Body, [return_maps])
716+
_ ->
717+
try
718+
jsx:decode(Body, [return_maps])
719+
catch
720+
error:_ ->
721+
{error, {invalid_body, not_json, Body}}
722+
end
708723
end.
709724

710725
validate_with_schema(Body, Definition, ValidatorState) ->

0 commit comments

Comments
 (0)