Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil requests in GRPC client method builder #2186

Merged
merged 1 commit into from Jul 8, 2019

Conversation

@delkopiso
Copy link
Contributor

commented Jul 8, 2019

What's up

The GRPC client remote method builder function casts the request interface into a pointer to the proto request struct. However, if the endpoint's input is an empty message then the client will be passing in a nil request interface. This causes a panic at the site of the interface cast.

What this does

Checks if the proto request obj is nil before casting. If it's nil, then we return a pointer to the empty proto request struct instead of casting the request interface to it.

Before

// BuildDoStuffFunc builds the remote method to invoke for "dummy" service
// "doStuff" endpoint.
func BuildDoStuffFunc(grpccli dummypb.DummyClient, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
	return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
		for _, opt := range cliopts {
			opts = append(opts, opt)
		}
		return grpccli.DoStuff(ctx, reqpb.(*dummypb.DoStuffRequest), opts...)
	}
}

After

// BuildDoStuffFunc builds the remote method to invoke for "dummy" service
// "doStuff" endpoint.
func BuildDoStuffFunc(grpccli dummypb.DummyClient, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
	return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
		for _, opt := range cliopts {
			opts = append(opts, opt)
		}
		if reqpb != nil {
			return grpccli.DoStuff(ctx, reqpb.(*dummypb.DoStuffRequest), opts...)
		}
		return grpccli.DoStuff(ctx, &dummypb.DoStuffRequest{}, opts...)
	}
}

Fixes #2185

Handle nil requests in GRPC client method builder
What's up

The GRPC client remote method builder function casts the request interface into a pointer to the proto request struct. However, if the endpoint's input is an empty message then the client will be passing in a nil request interface. This causes a panic at the site of the interface cast.

What this does

Checks if the proto request obj is nil before casting. If it's nil, then we return a pointer to the empty proto request struct instead of casting the request interface to it.

Fixes #2185
@raphael

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

This is great, thank you! Do you think you could back-port this fix to v2 as well? (simply create a branch off of v2 and cherry-pick this merge commit).

@raphael raphael merged commit 72fab95 into goadesign:v3 Jul 8, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@delkopiso delkopiso deleted the delkopiso:nil-request-client-method-builder branch Jul 9, 2019

delkopiso added a commit to delkopiso/goa that referenced this pull request Jul 9, 2019
Handle nil requests in GRPC client method builder (goadesign#2186)
What's up

The GRPC client remote method builder function casts the request interface into a pointer to the proto request struct. However, if the endpoint's input is an empty message then the client will be passing in a nil request interface. This causes a panic at the site of the interface cast.

What this does

Checks if the proto request obj is nil before casting. If it's nil, then we return a pointer to the empty proto request struct instead of casting the request interface to it.

Fixes goadesign#2185
@delkopiso

This comment has been minimized.

Copy link
Contributor Author

commented Jul 9, 2019

@raphael yeah, just created #2187

raphael added a commit that referenced this pull request Jul 9, 2019
Handle nil requests in GRPC client method builder (#2186) (#2187)
What's up

The GRPC client remote method builder function casts the request interface into a pointer to the proto request struct. However, if the endpoint's input is an empty message then the client will be passing in a nil request interface. This causes a panic at the site of the interface cast.

What this does

Checks if the proto request obj is nil before casting. If it's nil, then we return a pointer to the empty proto request struct instead of casting the request interface to it.

Fixes #2185
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.