grpc-gateway icon indicating copy to clipboard operation
grpc-gateway copied to clipboard

Protoc panics while generating openapiv2 files from proto with custom HttpRule in additional_bindings

Open michalgiltaurus opened this issue 1 year ago • 1 comments

🐛 Bug Report

Proto file with the following part:

service HealthService {
    rpc GetAllHealthChecks (GetAllHealthChecksRequest) returns (GetAllHealthChecksReply) {
        option (google.api.http) = {
            get: "/api/rest/v1/health/all"
            additional_bindings: [{ 
                custom: {
                    kind: "HEAD" 
                    path: "/api/rest/v1/health/all"
                }; 
            }];
        };
        option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
            summary: "Get all health checks";
            description: "This endpoint returns all the health checks of all instances of this component";
            tags: "Health checks";
        };
    };
}

openapi files generation run with:

          protoc --proto_path=api/proto/v1  --proto_path=api/proto/third_party \
            --openapiv2_out api/swagger/v1-tags \
            --openapiv2_opt logtostderr=true \
            `basename $file`

results with:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x134978c]

goroutine 1 [running]:
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi.renderServices({0xc00069c678, 0x1, 0xc0004eeb18}, 0xa, 0xc0001e4000, 0xa, 0xa, {0xc00027df40, 0x7, 0x8})
        /Users/x/go/src/projectX/grpc-gateway/protoc-gen-openapiv2/internal/genopenapi/template.go:1083 +0xa2c
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi.applyTemplate({0xc0003fd3b0, 0xc0001e4000})
        /Users/x/go/src/projectX/grpc-gateway/protoc-gen-openapiv2/internal/genopenapi/template.go:1394 +0x2f6
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi.(*generator).Generate(0xc0004efce0, {0xc00069c680, 0x14, 0x1014bd4})
        /Users/x/go/src/projectX/grpc-gateway/protoc-gen-openapiv2/internal/genopenapi/generator.go:194 +0x177
main.main()
        /Users/x/go/src/projectX/grpc-gateway/protoc-gen-openapiv2/main.go:160 +0xd1c
--openapiv2_out: protoc-gen-openapiv2: Plugin failed with status code 2.

Replacing the custom HttpRule with non-custom one such as get: "/path" results with a successful generation.

michalgiltaurus avatar Sep 15 '22 07:09 michalgiltaurus

Hi, thanks for your bug report! This is a bug in our plugin protoc-gen-openapiv2. You can see exactly where the crash is: protoc-gen-openapiv2/internal/genopenapi/template.go:1083. Fixing the crash is easy, we can probably just return an error instead. However, we probably also want to actually support these custom annotations. What can I do to help you bring this contribution in?

johanbrandhorst avatar Sep 17 '22 03:09 johanbrandhorst