From 90c1a15b68065c260c2d8478422ae72106c06188 Mon Sep 17 00:00:00 2001 From: Edward Tam Date: Mon, 25 Nov 2024 13:05:44 -0500 Subject: [PATCH] PD-1516 - return 400 when request payload is invalid json (#18) * PD-1516 - return 400 when request payload is invalid json * updated test fixtures --------- Co-authored-by: Edward Tam --- protoc-gen-rails/internal/output.go | 20 ++++++++++--------- .../app/controllers/my_service_controller.rb | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/protoc-gen-rails/internal/output.go b/protoc-gen-rails/internal/output.go index e038f11..d27db47 100644 --- a/protoc-gen-rails/internal/output.go +++ b/protoc-gen-rails/internal/output.go @@ -63,7 +63,7 @@ class {{.ControllerName}}Controller < ActionController::Base rescue_from GRPC::BadStatus do |e| render json: GrpcRest.error_msg(e), status: GrpcRest.grpc_http_status(e.code) end - rescue_from Google::Protobuf::TypeError do |e| + rescue_from ActionDispatch::Http::Parameters::ParseError, Google::Protobuf::TypeError do |e| render json: GrpcRest.error_msg(e), status: :bad_request end METHOD_PARAM_MAP = { @@ -102,20 +102,22 @@ func ProcessService(service *descriptorpb.ServiceDescriptorProto, pkg string) (F return FileResult{}, routes, err } restOpts, err := ExtractRestOptions(m) - if err != nil { return FileResult{}, routes, err } + if err != nil { + return FileResult{}, routes, err + } httpMethod, path, err := MethodAndPath(opts.Pattern) pathInfo, err := ParsedPath(path) if err != nil { return FileResult{}, routes, err } controllerMethod := method{ - Name: strcase.ToSnake(m.GetName()), - RequestType: Classify(m.GetInputType()), - Path: path, - RestOptions: rubyRestOptions(restOpts), - HttpMethod: httpMethod, - Body: opts.Body, - PathInfo: pathInfo, + Name: strcase.ToSnake(m.GetName()), + RequestType: Classify(m.GetInputType()), + Path: path, + RestOptions: rubyRestOptions(restOpts), + HttpMethod: httpMethod, + Body: opts.Body, + PathInfo: pathInfo, } data.Methods = append(data.Methods, controllerMethod) routes = append(routes, Route{ diff --git a/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb b/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb index 8087acf..83ea646 100644 --- a/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb +++ b/protoc-gen-rails/testdata/base/app/controllers/my_service_controller.rb @@ -11,9 +11,10 @@ class MyServiceController < ActionController::Base rescue_from GRPC::BadStatus do |e| render json: GrpcRest.error_msg(e), status: GrpcRest.grpc_http_status(e.code) end - rescue_from Google::Protobuf::TypeError do |e| + rescue_from ActionDispatch::Http::Parameters::ParseError, Google::Protobuf::TypeError do |e| render json: GrpcRest.error_msg(e), status: :bad_request end + METHOD_PARAM_MAP = { "test" => [