grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
https://grpc-ecosystem.github.io/grpc-gateway/
BSD 3-Clause "New" or "Revised" License
17.98k stars 2.21k forks source link

bug: URL Query parameter not bind to `Optional` proto message field #4660

Open afifurrohman-id opened 2 weeks ago

afifurrohman-id commented 2 weeks ago

🐛 Bug Report

When using optional field as request message and will be expected to bind with HTTP url query parameter the optional field always nil

To Reproduce

inputs:


- `svc.proto`
```proto
syntax = "proto3";

import "google/api/annotations.proto";
import "google/protobuf/empty.proto";

message DeleteFooRequest {
    string foo_id = 1;
    optional bool soft_delete = 2;
}

service ProductService {

    rpc DeleteFoo(DeleteFooRequest) returns (google.protobuf.Empty) {
        option (google.api.http) = {
            delete: "/foo/{foo_id}"
        };
    }

}

Expected behavior

it should either false or true

Actual Behavior

Soft delete it will always nil

Your Environment

Linux GRPC-GATEWAY 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 GNU/Linux

johanbrandhorst commented 2 weeks ago

Hi. Thanks for the example. Does it work if you don't use optional?

afifurrohman-id commented 2 weeks ago

Hi. Thanks for the example. Does it work if you don't use optional?

Yes, it works as expected

johanbrandhorst commented 2 weeks ago

Ah, very cool! We haven't yet put time into supporting the optional keyword, so this sounds like a bug. The code you want to look at is in https://github.com/grpc-ecosystem/grpc-gateway/blob/ffc412077010576dc94b993f0fd3f27b0d125910/runtime/query.go#L48-L65. I would start by adding a failing test case to https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/query_test.go. Contributions accepted!