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

feat: make it so you can optionally have DiscardUnknown not apply to enums #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions encoding/protojson/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type UnmarshalOptions struct {
// If DiscardUnknown is set, unknown fields and enum name values are ignored.
DiscardUnknown bool

// If ErrorOnUnknownEnumValue is set, unknown enum values will return an error.
// this has no effect if DiscardUnknown is false.
ErrorOnUnknownEnumValue bool

// Resolver is used for looking up types when unmarshaling
// google.protobuf.Any messages or extension fields.
// If nil, this defaults to using protoregistry.GlobalTypes.
Expand Down Expand Up @@ -343,7 +347,7 @@ func (d decoder) unmarshalScalar(fd protoreflect.FieldDescriptor) (protoreflect.
}

case protoreflect.EnumKind:
if v, ok := unmarshalEnum(tok, fd, d.opts.DiscardUnknown); ok {
if v, ok := unmarshalEnum(tok, fd, d.opts.DiscardUnknown, d.opts.ErrorOnUnknownEnumValue); ok {
return v, nil
}

Expand Down Expand Up @@ -488,15 +492,15 @@ func unmarshalBytes(tok json.Token) (protoreflect.Value, bool) {
return protoreflect.ValueOfBytes(b), true
}

func unmarshalEnum(tok json.Token, fd protoreflect.FieldDescriptor, discardUnknown bool) (protoreflect.Value, bool) {
func unmarshalEnum(tok json.Token, fd protoreflect.FieldDescriptor, discardUnknown, errorOnUnknown bool) (protoreflect.Value, bool) {
switch tok.Kind() {
case json.String:
// Lookup EnumNumber based on name.
s := tok.ParsedString()
if enumVal := fd.Enum().Values().ByName(protoreflect.Name(s)); enumVal != nil {
return protoreflect.ValueOfEnum(enumVal.Number()), true
}
if discardUnknown {
if discardUnknown && !errorOnUnknown {
return protoreflect.Value{}, true
}

Expand Down
66 changes: 66 additions & 0 deletions encoding/protojson/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,72 @@ func TestUnmarshal(t *testing.T) {
10: 101,
},
},
}, /* test ErrorOnUnknownEnumValue undoes DiscardUnknown for enums, but not others */ {
desc: "DiscardUnknown: Any, ErrorOnUnknownEnumValue: true",
umo: protojson.UnmarshalOptions{
DiscardUnknown: true,
ErrorOnUnknownEnumValue: true,
},
inputMessage: &anypb.Any{},
inputText: `{
"@type": "foo/pb2.Nested",
"unknown": "none"
}`,
wantMessage: &anypb.Any{
TypeUrl: "foo/pb2.Nested",
},
}, {
desc: "DiscardUnknown: Any with Empty, ErrorOnUnknownEnumValue: true",
umo: protojson.UnmarshalOptions{
DiscardUnknown: true,
ErrorOnUnknownEnumValue: true,
},
inputMessage: &anypb.Any{},
inputText: `{
"@type": "type.googleapis.com/google.protobuf.Empty",
"value": {"unknown": 47}
}`,
wantMessage: &anypb.Any{
TypeUrl: "type.googleapis.com/google.protobuf.Empty",
},
}, {
desc: "DiscardUnknown: true, ErrorOnUnknownEnumValue: true - unknown enum name",
inputMessage: &pb3.Enums{},
inputText: `{
"sEnum": "UNNAMED"
}`,
umo: protojson.UnmarshalOptions{
DiscardUnknown: true,
ErrorOnUnknownEnumValue: true,
},
wantErr: `invalid value for enum field sEnum: "UNNAMED"`, // weak_message2 is unknown since the package containing it is not imported
}, {
desc: "DiscardUnknown: true, ErrorOnUnknownEnumValue: true - repeated enum unknown name",
inputMessage: &pb2.Enums{},
inputText: `{
"rptEnum" : ["TEN", 1, 42, "UNNAMED"]
}`,
umo: protojson.UnmarshalOptions{
DiscardUnknown: true,
ErrorOnUnknownEnumValue: true,
},
wantErr: `invalid value for enum field rptEnum: "UNNAMED"`, // weak_message2 is unknown since the package containing it is not imported
}, {
desc: "DiscardUnknown: true, ErrorOnUnknownEnumValue: true - enum map value unknown name",
inputMessage: &pb3.Maps{},
inputText: `{
"uint64ToEnum": {
"1" : "ONE",
"2" : 2,
"10": 101,
"3": "UNNAMED"
}
}`,
umo: protojson.UnmarshalOptions{
DiscardUnknown: true,
ErrorOnUnknownEnumValue: true,
},
wantErr: `invalid value for enum field value: "UNNAMED"`, // weak_message2 is unknown since the package containing it is not imported
}, {
desc: "weak fields",
inputMessage: &testpb.TestWeak{},
Expand Down