Skip to content

Commit 96d8e5a

Browse files
authored
Fix slice appendings in generating field mask paths (#1555)
1 parent 888be51 commit 96d8e5a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

runtime/fieldmask.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@ func FieldMaskFromRequestBody(r io.Reader, md *descriptor.DescriptorProto) (*fie
5858
if subMsg, ok := v.(descriptor2.Message); ok {
5959
_, subMd = descriptor2.ForMessage(subMsg)
6060
}
61-
queue = append(queue, fieldMaskPathItem{path: append(item.path, protoName), node: v, md: subMd})
61+
62+
var path string
63+
if item.path == "" {
64+
path = protoName
65+
} else {
66+
path = item.path + "." + protoName
67+
}
68+
queue = append(queue, fieldMaskPathItem{path: path, node: v, md: subMd})
6269
}
6370
} else if len(item.path) > 0 {
6471
// otherwise, it's a leaf node so print its path
65-
fm.Paths = append(fm.Paths, strings.Join(item.path, "."))
72+
fm.Paths = append(fm.Paths, item.path)
6673
}
6774
}
6875

@@ -71,8 +78,8 @@ func FieldMaskFromRequestBody(r io.Reader, md *descriptor.DescriptorProto) (*fie
7178

7279
// fieldMaskPathItem stores a in-progress deconstruction of a path for a fieldmask
7380
type fieldMaskPathItem struct {
74-
// the list of prior fields leading up to node
75-
path []string
81+
// the list of prior fields leading up to node connected by dots
82+
path string
7683

7784
// a generic decoded json object the current item to inspect for further path extraction
7885
node interface{}

runtime/fieldmask_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestFieldMaskFromRequestBody(t *testing.T) {
5454
{name: "simple", input: `{"foo":1, "bar":"baz"}`, expected: newFieldMask("foo", "bar")},
5555
{name: "nested", input: `{"foo": {"bar":1, "baz": 2}, "qux": 3}`, expected: newFieldMask("foo.bar", "foo.baz", "qux")},
5656
{name: "canonical", input: `{"f": {"b": {"d": 1, "x": 2}, "c": 1}}`, expected: newFieldMask("f.b.d", "f.b.x", "f.c")},
57+
{name: "deeply-nested", input: `{"foo": {"bar": {"baz": {"a": 1, "b": 2}}}}`, expected: newFieldMask("foo.bar.baz.a", "foo.bar.baz.b")},
5758
} {
5859
t.Run(tc.name, func(t *testing.T) {
5960
actual, err := FieldMaskFromRequestBody(bytes.NewReader([]byte(tc.input)), nil)

0 commit comments

Comments
 (0)