map without body possible?

20 views
Skip to first unread message

Markus Stopper

unread,
Nov 4, 2019, 8:34:49 AM11/4/19
to Protocol Buffers
Hello,
is it possible to generate a message without body descriptor?

message Any{
  map<string,string> data = 1;
}


expected output in json supposed to be the simplest possible json:
{
  "key":"value",
  "key":"value",
}

but always get the descriptor of the object as well which I do not want:
{
  "data":{
    "key":"value",
    "key":"value",
  }
}

Can I omit the outer structure somehow?

all the best

Markus Stopper

unread,
Nov 5, 2019, 11:28:20 AM11/5/19
to Protocol Buffers
I found a solution which actually imposes two steps:
1. hook on the generated gateway forwarding methods to flag certain data to convert to simple json
2. intercept the http multiplexer to catch the flagged stream and do the work.

1.1 create (**) a file in the apifolder /projectname/api/testapi/{ test.proto, **test_reflag.go**, test.pb.go, test.pb.gw.go}
1.2 file content:
func forwardSimpleTest(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
w.Header().Set("json", "simplify") //flagged <<<<
runtime.ForwardResponseMessage(ctx, mux, marshaler, w, req, resp, opts...)
}

func init() {
forward_MyTest_GetSimpleTests_0 = forwardSimpleTest
   //... for all calls which need to be flagged
}


2. hook the interceptor into the Responsewriter where I detect the simplified and rewrite the JSON structure from:
{
  simple:{
    "key":"value",
    "key":"value",
 }
}

to ->

{
  "key":"value",
  "key":"value",
}

sad that it is not possible with the grpc gateway nor configurable with the protobuf library...

Anyway, if somebody has the same issue this is a nice and fast workaround
Reply all
Reply to author
Forward
0 new messages