Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Recognize the AllowPartial flag when parsing Any #49

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions encoding/protojson/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,18 @@ func TestUnmarshal(t *testing.T) {
wantMessage: &anypb.Any{
TypeUrl: "type.googleapis.com/google.protobuf.Empty",
},
}, {
desc: "Any with Empty and no value and AllowPartial",
umo: protojson.UnmarshalOptions{AllowPartial: true},
inputMessage: &anypb.Any{},
inputText: `{
"@type": "type.googleapis.com/google.protobuf.Empty"
}`,
wantMessage: func() proto.Message {
return &anypb.Any{
TypeUrl: "type.googleapis.com/google.protobuf.Empty",
}
}(),
Comment on lines +2207 to +2211

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function wrapping is unnecessary. Since the function doesn’t do anything other than return an anypb.Any composite literal.

}, {
desc: "Any with missing Empty",
inputMessage: &anypb.Any{},
Expand Down
6 changes: 4 additions & 2 deletions encoding/protojson/well_known_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (d decoder) unmarshalAny(m protoreflect.Message) error {
// Use another decoder to parse the unread bytes for @type field. This
// avoids advancing a read from current decoder because the current JSON
// object may contain the fields of the embedded type.
dec := decoder{d.Clone(), UnmarshalOptions{RecursionLimit: d.opts.RecursionLimit}}
dec := decoder{d.Clone(), UnmarshalOptions{RecursionLimit: d.opts.RecursionLimit, AllowPartial: d.opts.AllowPartial}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I should also plumb through DiscardUnknown (or maybe all the options?). Those are the two options that cloud.google.com/go passes today: e.g. https://github.com/googleapis/google-cloud-go/blob/b23a08492a7724ce6f1f95395b7f71feb9b3de66/ai/generativelanguage/apiv1beta/generative_client.go#L600

tok, err := findTypeURL(dec)
switch err {
case errEmptyObject:
Expand Down Expand Up @@ -348,7 +348,9 @@ func (d decoder) unmarshalAnyValue(unmarshal unmarshalFunc, m protoreflect.Messa
switch tok.Kind() {
case json.ObjectClose:
if !found {
return d.newError(tok.Pos(), `missing "value" field`)
if !d.opts.AllowPartial {
return d.newError(tok.Pos(), `missing "value" field`)
}
}
return nil

Expand Down