Grpc-gateway: protoc-gen-swagger throws the error: Any JSON doesn't have '@type'

Created on 9 Oct 2018  路  8Comments  路  Source: grpc-ecosystem/grpc-gateway

I defined a message which includes the 'google.protobuf.Any' type

message TestRequest {
int64 id = 1;
google.protobuf.Any items = 2;
}
and generate the related pb.gw.go file, and when to execute below auto-gen code, it failed with the error: Any JSON doesn't have '@type'
if err := marshaler.NewDecoder(req.Body).Decode(&protoReq); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}

bug waiting on reporter

All 8 comments

Curious, this definitely should be working, what does the JSON that it's trying to unmarshal look like?

The struct of TestRequest is like below one, it is auto-generated.

type TestRequest struct {
Id int64 protobuf:"varint,1,opt,name=id" json:"id,omitempty"
Items *google_protobuf1.Any protobuf:"bytes,2,opt,name=items" json:"items,omitempty"
}

What does the JSON input look like?

Pass below through request' body, @johanbrandhorst
{
"items": {
"include": [{"id": 3, "test_name": "Test_Value"}]
}

}

Yeah so you haven't specified the type URL in the JSON input. Please see documentation for the google.protobuf.Any type to see how to use it with JSON. This is not a bug.

Sorry, I didn't find any official example from documentation, could you please supply one for me to refer to? thx @johanbrandhorst

thx

Was this page helpful?
0 / 5 - 0 ratings