owncloud / protoc-gen-microweb

Protoc generator for Micro web services
Apache License 2.0
7 stars 6 forks source link

Broken generation for google.protobuf.Empty types #1

Closed mirekys closed 4 years ago

mirekys commented 4 years ago

When using empty request body in api calls, the resulting generated code won't compile, e.g.:

#service.proto
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
...
service CoolService {
    rpc GetServiceName(google.protobuf.Empty) returns (GetServiceNameResponse) {
        option (google.api.http) = {
            get: "/v1/servicename"
        };
    }
}

when generated using:

protoc \
        -I=/usr/local/include/ \
                -I=pkg/proto/v1/ \
        -I${GOPATH}/src/github.com/golang/protobuf \
        -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
        -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway \
        --microweb_out=logtostderr=true:pkg/proto/v1 service.proto

results in the following handler func:

#service.pb.web.go
...
func (h *webSiteManagerHandler) GetServiceName(w http.ResponseWriter, r *http.Request) {
    req := &Empty{}
    resp := &GetServiceNameResponse{}

    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        http.Error(w, err.Error(), http.StatusPreconditionFailed)
        return
    }

    if err := h.h.GetServiceName(
...

which leads to the following compile error:

# github.com/blah/cool-service/pkg/proto/v1
../../pkg/proto/v1/service.pb.web.go:27:10: undefined: Empty
../../pkg/proto/v1/service.pb.web.go:49:10: undefined: Empty

this can be easily fixed by adding:

empty "github.com/golang/protobuf/ptypes/empty"

to the imports when Empty is being used in the proto file (not sure how to do that programatically though) together with updating all uses of Empty struct with empty.Empty wherever .Input.Name or .Output.Name equals Empty in the template context:

func (h *webSiteManagerHandler) GetServiceName(w http.ResponseWriter, r *http.Request) {
    req := &empty.Empty{}
    resp := &GetSiteNameResponse{}
...

There is an another problem with JSON decoding of the request with empty body. The http.StatusPreconditionFailed EOF error is being returned. Removing of the JSON decoding of the request when the type is Empty fixes the issue.

tboerger commented 4 years ago

Ok, something like Empty is not integrated so far. Are you able to provide a PR to get this resolved?