Skip to content

Commit 5d62c34

Browse files
authored
fix(gengapic): remove encoded quotes in query params (#1364)
Removes all quotation marks from protojson encoded well known types that are encoded as strings. Lazily use strings.ReplaceAll to make it easier when the WKT doesn't necessarily have quotations included. Showcase integration still works (some other languages do not encode quotations), and I tested locally with another API's samples. Fixes #1363
1 parent eb5dcf0 commit 5d62c34

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

internal/gengapic/genrest.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,12 @@ func (g *generator) generateQueryString(m *descriptor.MethodDescriptorProto) {
419419
b.WriteString(" return nil, err\n")
420420
}
421421
b.WriteString("}\n")
422-
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s))", key, field.GetJsonName()))
422+
// Only some of the well known types will be encoded as strings, remove the wrapping quotations for those.
423+
if strContains(wellKnownStringTypes, field.GetTypeName()) {
424+
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s[1:len(%[2]s)-1]))", key, field.GetJsonName()))
425+
} else {
426+
b.WriteString(fmt.Sprintf("params.Add(%q, string(%s))", key, field.GetJsonName()))
427+
}
423428
paramAdd = b.String()
424429
} else {
425430
paramAdd = fmt.Sprintf("params.Add(%q, fmt.Sprintf(%q, req%s))", key, "%v", accessor)

internal/gengapic/genrest_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,16 @@ func TestGenRestMethod(t *testing.T) {
506506
Label: descriptor.FieldDescriptorProto_LABEL_REPEATED.Enum(),
507507
}
508508

509+
numericWrapperField := &descriptor.FieldDescriptorProto{
510+
Name: proto.String("numeric_wrapper"),
511+
Type: descriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
512+
JsonName: proto.String("numericWrapper"),
513+
TypeName: proto.String(".google.protobuf.DoubleValue"),
514+
}
515+
509516
updateReq := &descriptor.DescriptorProto{
510517
Name: proto.String("UpdateRequest"),
511-
Field: []*descriptor.FieldDescriptorProto{fooField, maskField, repeatedPrimField},
518+
Field: []*descriptor.FieldDescriptorProto{fooField, maskField, repeatedPrimField, numericWrapperField},
512519
}
513520
updateReqFqn := fmt.Sprintf(".%s.UpdateRequest", pkg)
514521

@@ -704,19 +711,20 @@ func TestGenRestMethod(t *testing.T) {
704711
updateReq: f,
705712
},
706713
ParentElement: map[pbinfo.ProtoType]pbinfo.ProtoType{
707-
opRPC: s,
708-
emptyRPC: s,
709-
unaryRPC: s,
710-
pagingRPC: s,
711-
serverStreamRPC: s,
712-
clientStreamRPC: s,
713-
lroRPC: s,
714-
httpBodyRPC: s,
715-
updateRPC: s,
716-
nameField: op,
717-
sizeField: foo,
718-
otherField: foo,
719-
maskField: updateReq,
714+
opRPC: s,
715+
emptyRPC: s,
716+
unaryRPC: s,
717+
pagingRPC: s,
718+
serverStreamRPC: s,
719+
clientStreamRPC: s,
720+
lroRPC: s,
721+
httpBodyRPC: s,
722+
updateRPC: s,
723+
nameField: op,
724+
sizeField: foo,
725+
otherField: foo,
726+
maskField: updateReq,
727+
numericWrapperField: updateReq,
720728
},
721729
Type: map[string]pbinfo.ProtoType{
722730
opfqn: op,

internal/gengapic/testdata/rest_UpdateRPC.want

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ func (c *fooRESTClient) UpdateRPC(ctx context.Context, req *foopb.UpdateRequest,
1414

1515
params := url.Values{}
1616
params.Add("$alt", "json;enum-encoding=int")
17+
if req.GetNumericWrapper() != nil {
18+
numericWrapper, err := protojson.Marshal(req.GetNumericWrapper())
19+
if err != nil {
20+
return nil, err
21+
}
22+
params.Add("numericWrapper", string(numericWrapper))
23+
}
1724
if items := req.GetPrimitives(); len(items) > 0 {
1825
for _, item := range items {
1926
params.Add("primitives", fmt.Sprintf("%v", item))
@@ -24,7 +31,7 @@ func (c *fooRESTClient) UpdateRPC(ctx context.Context, req *foopb.UpdateRequest,
2431
if err != nil {
2532
return nil, err
2633
}
27-
params.Add("updateMask", string(updateMask))
34+
params.Add("updateMask", string(updateMask[1:len(updateMask)-1]))
2835
}
2936

3037
baseUrl.RawQuery = params.Encode()

internal/gengapic/well_known_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ var wellKnownTypeNames = []string{
4444
".google.protobuf.ListValue",
4545
}
4646

47+
// TODO: Look into if we need to support ListValue/Value fields with
48+
// StringValue or BytesValue values.
49+
var wellKnownStringTypes = []string{
50+
".google.protobuf.FieldMask",
51+
".google.protobuf.Timestamp",
52+
".google.protobuf.Duration",
53+
".google.protobuf.StringValue",
54+
".google.protobuf.BytesValue",
55+
}
56+
4757
var wellKnownTypeFiles = []*descriptor.FileDescriptorProto{
4858
protodesc.ToFileDescriptorProto(emptypb.File_google_protobuf_empty_proto),
4959
protodesc.ToFileDescriptorProto(anypb.File_google_protobuf_any_proto),

0 commit comments

Comments
 (0)