Skip to content

Commit ab94f3f

Browse files
committed
just skip fields that can't be checked; no validation customization
1 parent 5b0ec41 commit ab94f3f

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

jsonpb/jsonpb.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"strconv"
5151
"strings"
5252
"time"
53+
"unicode"
5354

5455
"github.com/golang/protobuf/proto"
5556

@@ -1107,13 +1108,6 @@ func (s mapKeys) Less(i, j int) bool {
11071108
// This function is used by both Marshal and Unmarshal. While required fields only exist in a
11081109
// proto2 message, a proto3 message can contain proto2 message(s).
11091110
func checkRequiredFields(pb proto.Message) error {
1110-
type validatingMessage interface {
1111-
ValidateRecursive() error
1112-
}
1113-
if vm, ok := pb.(validatingMessage); ok {
1114-
return vm.ValidateRecursive()
1115-
}
1116-
11171111
// Most well-known type messages do not contain required fields. The "Any" type may contain
11181112
// a message that has required fields.
11191113
//
@@ -1141,6 +1135,16 @@ func checkRequiredFields(pb proto.Message) error {
11411135
for i := 0; i < v.NumField(); i++ {
11421136
field := v.Field(i)
11431137
sfield := v.Type().Field(i)
1138+
1139+
var exported bool
1140+
for _, r := range sfield.Name {
1141+
exported = unicode.IsUpper(r)
1142+
break
1143+
}
1144+
if !exported {
1145+
continue
1146+
}
1147+
11441148
if strings.HasPrefix(sfield.Name, "XXX_") {
11451149
continue
11461150
}
@@ -1163,8 +1167,12 @@ func checkRequiredFields(pb proto.Message) error {
11631167
sfield = v.Type().Field(0)
11641168
}
11651169

1170+
protoTag := sfield.Tag.Get("protobuf")
1171+
if protoTag == "" {
1172+
continue
1173+
}
11661174
var prop proto.Properties
1167-
prop.Init(sfield.Type, sfield.Name, sfield.Tag.Get("protobuf"), &sfield)
1175+
prop.Init(sfield.Type, sfield.Name, protoTag, &sfield)
11681176

11691177
switch field.Kind() {
11701178
case reflect.Map:

jsonpb/jsonpb_test.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ package jsonpb
3434
import (
3535
"bytes"
3636
"encoding/json"
37-
"errors"
3837
"io"
3938
"math"
4039
"reflect"
@@ -535,7 +534,7 @@ func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
535534
}
536535

537536
func TestMarshalWithCustomValidation(t *testing.T) {
538-
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, dummy1: &dynamicMessage{}}
537+
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, dummy: &dynamicMessage{}}
539538

540539
js, err := new(Marshaler).MarshalToString(&msg)
541540
if err != nil {
@@ -545,13 +544,6 @@ func TestMarshalWithCustomValidation(t *testing.T) {
545544
if err != nil {
546545
t.Errorf("an unexpected error occurred when unmarshalling from json: %v", err)
547546
}
548-
549-
// tickle an error in custom validation
550-
msg.dummy2 = int32(len(msg.rawJson) + 1)
551-
_, err = new(Marshaler).MarshalToString(&msg)
552-
if err == nil {
553-
t.Errorf("marshalling to json should have generated validation error but did not")
554-
}
555547
}
556548

557549
// Test marshaling message containing unset required fields should produce error.
@@ -1028,10 +1020,7 @@ type dynamicMessage struct {
10281020

10291021
// an unexported nested message is present just to ensure that it
10301022
// won't result in a panic (see issue #509)
1031-
dummy1 *dynamicMessage `protobuf:"bytes,2,opt,name=dummy1"`
1032-
1033-
// this is used to implement a custom validation rule
1034-
dummy2 int32 `protobuf:"varint,3,opt,name=dummy2"`
1023+
dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
10351024
}
10361025

10371026
func (m *dynamicMessage) Reset() {
@@ -1054,13 +1043,6 @@ func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
10541043
return nil
10551044
}
10561045

1057-
func (m *dynamicMessage) ValidateRecursive() error {
1058-
if int(m.dummy2) > len(m.rawJson) {
1059-
return errors.New("dummy2 should be <= rawJson length")
1060-
}
1061-
return nil
1062-
}
1063-
10641046
// Test unmarshaling message containing unset required fields should produce error.
10651047
func TestUnmarshalUnsetRequiredFields(t *testing.T) {
10661048
tests := []struct {

0 commit comments

Comments
 (0)