blob: 74dd570b5669ad5551235aef88936d043c86cb0c [file] [log] [blame]
Herbie Ong7b828bc2019-02-08 19:56:24 -08001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package jsonpb_test
6
7import (
Herbie Ong0b0f4032019-03-18 19:06:15 -07008 "encoding/hex"
Herbie Ong7b828bc2019-02-08 19:56:24 -08009 "math"
10 "strings"
11 "testing"
12
13 "github.com/golang/protobuf/v2/encoding/jsonpb"
14 "github.com/golang/protobuf/v2/internal/encoding/pack"
Herbie Ongf83d5bb2019-03-14 00:01:27 -070015 "github.com/golang/protobuf/v2/internal/encoding/wire"
Herbie Ong7b828bc2019-02-08 19:56:24 -080016 "github.com/golang/protobuf/v2/internal/scalar"
17 "github.com/golang/protobuf/v2/proto"
Herbie Ong0b0f4032019-03-18 19:06:15 -070018 preg "github.com/golang/protobuf/v2/reflect/protoregistry"
Joe Tsai4fddeba2019-03-20 18:29:32 -070019 "github.com/golang/protobuf/v2/runtime/protoiface"
Herbie Ong7b828bc2019-02-08 19:56:24 -080020 "github.com/google/go-cmp/cmp"
21 "github.com/google/go-cmp/cmp/cmpopts"
22
Herbie Ongf83d5bb2019-03-14 00:01:27 -070023 // This legacy package is still needed when importing legacy message.
24 _ "github.com/golang/protobuf/v2/internal/legacy"
25
Herbie Ong7b828bc2019-02-08 19:56:24 -080026 "github.com/golang/protobuf/v2/encoding/testprotos/pb2"
27 "github.com/golang/protobuf/v2/encoding/testprotos/pb3"
Herbie Ong0b0f4032019-03-18 19:06:15 -070028 knownpb "github.com/golang/protobuf/v2/types/known"
Herbie Ong7b828bc2019-02-08 19:56:24 -080029)
30
31// splitLines is a cmpopts.Option for comparing strings with line breaks.
32var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
33 return strings.Split(s, "\n")
34})
35
36func pb2Enum(i int32) *pb2.Enum {
37 p := new(pb2.Enum)
38 *p = pb2.Enum(i)
39 return p
40}
41
42func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
43 p := new(pb2.Enums_NestedEnum)
44 *p = pb2.Enums_NestedEnum(i)
45 return p
46}
47
Joe Tsai4fddeba2019-03-20 18:29:32 -070048func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
Herbie Ongf83d5bb2019-03-14 00:01:27 -070049 knownFields := m.ProtoReflect().KnownFields()
50 extTypes := knownFields.ExtensionTypes()
51 extTypes.Register(xd.Type)
52 if val == nil {
53 return
54 }
55 pval := xd.Type.ValueOf(val)
56 knownFields.Set(wire.Number(xd.Field), pval)
57}
58
Herbie Ong0b0f4032019-03-18 19:06:15 -070059// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
60func dhex(s string) []byte {
61 b, err := hex.DecodeString(s)
62 if err != nil {
63 panic(err)
64 }
65 return b
66}
67
Herbie Ong7b828bc2019-02-08 19:56:24 -080068func TestMarshal(t *testing.T) {
69 tests := []struct {
Herbie Ong0b0f4032019-03-18 19:06:15 -070070 desc string
71 mo jsonpb.MarshalOptions
72 input proto.Message
73 want string
74 wantErr bool // TODO: Verify error message substring.
Herbie Ong7b828bc2019-02-08 19:56:24 -080075 }{{
76 desc: "proto2 optional scalars not set",
77 input: &pb2.Scalars{},
78 want: "{}",
79 }, {
80 desc: "proto3 scalars not set",
81 input: &pb3.Scalars{},
82 want: "{}",
83 }, {
84 desc: "proto2 optional scalars set to zero values",
85 input: &pb2.Scalars{
86 OptBool: scalar.Bool(false),
87 OptInt32: scalar.Int32(0),
88 OptInt64: scalar.Int64(0),
89 OptUint32: scalar.Uint32(0),
90 OptUint64: scalar.Uint64(0),
91 OptSint32: scalar.Int32(0),
92 OptSint64: scalar.Int64(0),
93 OptFixed32: scalar.Uint32(0),
94 OptFixed64: scalar.Uint64(0),
95 OptSfixed32: scalar.Int32(0),
96 OptSfixed64: scalar.Int64(0),
97 OptFloat: scalar.Float32(0),
98 OptDouble: scalar.Float64(0),
99 OptBytes: []byte{},
100 OptString: scalar.String(""),
101 },
102 want: `{
103 "optBool": false,
104 "optInt32": 0,
105 "optInt64": "0",
106 "optUint32": 0,
107 "optUint64": "0",
108 "optSint32": 0,
109 "optSint64": "0",
110 "optFixed32": 0,
111 "optFixed64": "0",
112 "optSfixed32": 0,
113 "optSfixed64": "0",
114 "optFloat": 0,
115 "optDouble": 0,
116 "optBytes": "",
117 "optString": ""
118}`,
119 }, {
120 desc: "proto2 optional scalars set to some values",
121 input: &pb2.Scalars{
122 OptBool: scalar.Bool(true),
123 OptInt32: scalar.Int32(0xff),
124 OptInt64: scalar.Int64(0xdeadbeef),
125 OptUint32: scalar.Uint32(47),
126 OptUint64: scalar.Uint64(0xdeadbeef),
127 OptSint32: scalar.Int32(-1001),
128 OptSint64: scalar.Int64(-0xffff),
129 OptFixed64: scalar.Uint64(64),
130 OptSfixed32: scalar.Int32(-32),
131 OptFloat: scalar.Float32(1.02),
132 OptDouble: scalar.Float64(1.234),
Herbie Ong87608a72019-03-06 14:32:24 -0800133 OptBytes: []byte("谷歌"),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800134 OptString: scalar.String("谷歌"),
135 },
136 want: `{
137 "optBool": true,
138 "optInt32": 255,
139 "optInt64": "3735928559",
140 "optUint32": 47,
141 "optUint64": "3735928559",
142 "optSint32": -1001,
143 "optSint64": "-65535",
144 "optFixed64": "64",
145 "optSfixed32": -32,
146 "optFloat": 1.02,
147 "optDouble": 1.234,
148 "optBytes": "6LC35q2M",
149 "optString": "谷歌"
150}`,
151 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -0700152 desc: "string",
153 input: &pb3.Scalars{
154 SString: "谷歌",
155 },
156 want: `{
157 "sString": "谷歌"
158}`,
159 }, {
160 desc: "string with invalid UTF8",
161 input: &pb3.Scalars{
162 SString: "abc\xff",
163 },
164 want: "{\n \"sString\": \"abc\xff\"\n}",
165 wantErr: true,
166 }, {
Herbie Ong7b828bc2019-02-08 19:56:24 -0800167 desc: "float nan",
168 input: &pb3.Scalars{
169 SFloat: float32(math.NaN()),
170 },
171 want: `{
172 "sFloat": "NaN"
173}`,
174 }, {
175 desc: "float positive infinity",
176 input: &pb3.Scalars{
177 SFloat: float32(math.Inf(1)),
178 },
179 want: `{
180 "sFloat": "Infinity"
181}`,
182 }, {
183 desc: "float negative infinity",
184 input: &pb3.Scalars{
185 SFloat: float32(math.Inf(-1)),
186 },
187 want: `{
188 "sFloat": "-Infinity"
189}`,
190 }, {
191 desc: "double nan",
192 input: &pb3.Scalars{
193 SDouble: math.NaN(),
194 },
195 want: `{
196 "sDouble": "NaN"
197}`,
198 }, {
199 desc: "double positive infinity",
200 input: &pb3.Scalars{
201 SDouble: math.Inf(1),
202 },
203 want: `{
204 "sDouble": "Infinity"
205}`,
206 }, {
207 desc: "double negative infinity",
208 input: &pb3.Scalars{
209 SDouble: math.Inf(-1),
210 },
211 want: `{
212 "sDouble": "-Infinity"
213}`,
214 }, {
215 desc: "proto2 enum not set",
216 input: &pb2.Enums{},
217 want: "{}",
218 }, {
219 desc: "proto2 enum set to zero value",
220 input: &pb2.Enums{
221 OptEnum: pb2Enum(0),
222 OptNestedEnum: pb2Enums_NestedEnum(0),
223 },
224 want: `{
225 "optEnum": 0,
226 "optNestedEnum": 0
227}`,
228 }, {
229 desc: "proto2 enum",
230 input: &pb2.Enums{
231 OptEnum: pb2.Enum_ONE.Enum(),
232 OptNestedEnum: pb2.Enums_UNO.Enum(),
233 },
234 want: `{
235 "optEnum": "ONE",
236 "optNestedEnum": "UNO"
237}`,
238 }, {
239 desc: "proto2 enum set to numeric values",
240 input: &pb2.Enums{
241 OptEnum: pb2Enum(2),
242 OptNestedEnum: pb2Enums_NestedEnum(2),
243 },
244 want: `{
245 "optEnum": "TWO",
246 "optNestedEnum": "DOS"
247}`,
248 }, {
249 desc: "proto2 enum set to unnamed numeric values",
250 input: &pb2.Enums{
251 OptEnum: pb2Enum(101),
252 OptNestedEnum: pb2Enums_NestedEnum(-101),
253 },
254 want: `{
255 "optEnum": 101,
256 "optNestedEnum": -101
257}`,
258 }, {
259 desc: "proto3 enum not set",
260 input: &pb3.Enums{},
261 want: "{}",
262 }, {
263 desc: "proto3 enum set to zero value",
264 input: &pb3.Enums{
265 SEnum: pb3.Enum_ZERO,
266 SNestedEnum: pb3.Enums_CERO,
267 },
268 want: "{}",
269 }, {
270 desc: "proto3 enum",
271 input: &pb3.Enums{
272 SEnum: pb3.Enum_ONE,
273 SNestedEnum: pb3.Enums_UNO,
274 },
275 want: `{
276 "sEnum": "ONE",
277 "sNestedEnum": "UNO"
278}`,
279 }, {
280 desc: "proto3 enum set to numeric values",
281 input: &pb3.Enums{
282 SEnum: 2,
283 SNestedEnum: 2,
284 },
285 want: `{
286 "sEnum": "TWO",
287 "sNestedEnum": "DOS"
288}`,
289 }, {
290 desc: "proto3 enum set to unnamed numeric values",
291 input: &pb3.Enums{
292 SEnum: -47,
293 SNestedEnum: 47,
294 },
295 want: `{
296 "sEnum": -47,
297 "sNestedEnum": 47
298}`,
299 }, {
300 desc: "proto2 nested message not set",
301 input: &pb2.Nests{},
302 want: "{}",
303 }, {
304 desc: "proto2 nested message set to empty",
305 input: &pb2.Nests{
306 OptNested: &pb2.Nested{},
307 Optgroup: &pb2.Nests_OptGroup{},
308 },
309 want: `{
310 "optNested": {},
311 "optgroup": {}
312}`,
313 }, {
314 desc: "proto2 nested messages",
315 input: &pb2.Nests{
316 OptNested: &pb2.Nested{
317 OptString: scalar.String("nested message"),
318 OptNested: &pb2.Nested{
319 OptString: scalar.String("another nested message"),
320 },
321 },
322 },
323 want: `{
324 "optNested": {
325 "optString": "nested message",
326 "optNested": {
327 "optString": "another nested message"
328 }
329 }
330}`,
331 }, {
332 desc: "proto2 groups",
333 input: &pb2.Nests{
334 Optgroup: &pb2.Nests_OptGroup{
335 OptString: scalar.String("inside a group"),
336 OptNested: &pb2.Nested{
337 OptString: scalar.String("nested message inside a group"),
338 },
339 Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
340 OptFixed32: scalar.Uint32(47),
341 },
342 },
343 },
344 want: `{
345 "optgroup": {
346 "optString": "inside a group",
347 "optNested": {
348 "optString": "nested message inside a group"
349 },
350 "optnestedgroup": {
351 "optFixed32": 47
352 }
353 }
354}`,
355 }, {
356 desc: "proto3 nested message not set",
357 input: &pb3.Nests{},
358 want: "{}",
359 }, {
360 desc: "proto3 nested message set to empty",
361 input: &pb3.Nests{
362 SNested: &pb3.Nested{},
363 },
364 want: `{
365 "sNested": {}
366}`,
367 }, {
368 desc: "proto3 nested message",
369 input: &pb3.Nests{
370 SNested: &pb3.Nested{
371 SString: "nested message",
372 SNested: &pb3.Nested{
373 SString: "another nested message",
374 },
375 },
376 },
377 want: `{
378 "sNested": {
379 "sString": "nested message",
380 "sNested": {
381 "sString": "another nested message"
382 }
383 }
384}`,
385 }, {
386 desc: "oneof not set",
387 input: &pb3.Oneofs{},
388 want: "{}",
389 }, {
390 desc: "oneof set to empty string",
391 input: &pb3.Oneofs{
392 Union: &pb3.Oneofs_OneofString{},
393 },
394 want: `{
395 "oneofString": ""
396}`,
397 }, {
398 desc: "oneof set to string",
399 input: &pb3.Oneofs{
400 Union: &pb3.Oneofs_OneofString{
401 OneofString: "hello",
402 },
403 },
404 want: `{
405 "oneofString": "hello"
406}`,
407 }, {
408 desc: "oneof set to enum",
409 input: &pb3.Oneofs{
410 Union: &pb3.Oneofs_OneofEnum{
411 OneofEnum: pb3.Enum_ZERO,
412 },
413 },
414 want: `{
415 "oneofEnum": "ZERO"
416}`,
417 }, {
418 desc: "oneof set to empty message",
419 input: &pb3.Oneofs{
420 Union: &pb3.Oneofs_OneofNested{
421 OneofNested: &pb3.Nested{},
422 },
423 },
424 want: `{
425 "oneofNested": {}
426}`,
427 }, {
428 desc: "oneof set to message",
429 input: &pb3.Oneofs{
430 Union: &pb3.Oneofs_OneofNested{
431 OneofNested: &pb3.Nested{
432 SString: "nested message",
433 },
434 },
435 },
436 want: `{
437 "oneofNested": {
438 "sString": "nested message"
439 }
440}`,
441 }, {
442 desc: "repeated fields not set",
443 input: &pb2.Repeats{},
444 want: "{}",
445 }, {
446 desc: "repeated fields set to empty slices",
447 input: &pb2.Repeats{
448 RptBool: []bool{},
449 RptInt32: []int32{},
450 RptInt64: []int64{},
451 RptUint32: []uint32{},
452 RptUint64: []uint64{},
453 RptFloat: []float32{},
454 RptDouble: []float64{},
455 RptBytes: [][]byte{},
456 },
457 want: "{}",
458 }, {
459 desc: "repeated fields set to some values",
460 input: &pb2.Repeats{
461 RptBool: []bool{true, false, true, true},
462 RptInt32: []int32{1, 6, 0, 0},
463 RptInt64: []int64{-64, 47},
464 RptUint32: []uint32{0xff, 0xffff},
465 RptUint64: []uint64{0xdeadbeef},
466 RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
467 RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
468 RptString: []string{"hello", "世界"},
469 RptBytes: [][]byte{
470 []byte("hello"),
471 []byte("\xe4\xb8\x96\xe7\x95\x8c"),
472 },
473 },
474 want: `{
475 "rptBool": [
476 true,
477 false,
478 true,
479 true
480 ],
481 "rptInt32": [
482 1,
483 6,
484 0,
485 0
486 ],
487 "rptInt64": [
488 "-64",
489 "47"
490 ],
491 "rptUint32": [
492 255,
493 65535
494 ],
495 "rptUint64": [
496 "3735928559"
497 ],
498 "rptFloat": [
499 "NaN",
500 "Infinity",
501 "-Infinity",
502 1.034
503 ],
504 "rptDouble": [
505 "NaN",
506 "Infinity",
507 "-Infinity",
508 1.23e-308
509 ],
510 "rptString": [
511 "hello",
512 "世界"
513 ],
514 "rptBytes": [
515 "aGVsbG8=",
516 "5LiW55WM"
517 ]
518}`,
519 }, {
520 desc: "repeated enums",
521 input: &pb2.Enums{
522 RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
523 RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
524 },
525 want: `{
526 "rptEnum": [
527 "ONE",
528 "TWO",
529 "TEN",
530 42
531 ],
532 "rptNestedEnum": [
533 "DOS",
534 47,
535 "DIEZ"
536 ]
537}`,
538 }, {
539 desc: "repeated messages set to empty",
540 input: &pb2.Nests{
541 RptNested: []*pb2.Nested{},
542 Rptgroup: []*pb2.Nests_RptGroup{},
543 },
544 want: "{}",
545 }, {
546 desc: "repeated messages",
547 input: &pb2.Nests{
548 RptNested: []*pb2.Nested{
549 {
550 OptString: scalar.String("repeat nested one"),
551 },
552 {
553 OptString: scalar.String("repeat nested two"),
554 OptNested: &pb2.Nested{
555 OptString: scalar.String("inside repeat nested two"),
556 },
557 },
558 {},
559 },
560 },
561 want: `{
562 "rptNested": [
563 {
564 "optString": "repeat nested one"
565 },
566 {
567 "optString": "repeat nested two",
568 "optNested": {
569 "optString": "inside repeat nested two"
570 }
571 },
572 {}
573 ]
574}`,
575 }, {
576 desc: "repeated messages contains nil value",
577 input: &pb2.Nests{
578 RptNested: []*pb2.Nested{nil, {}},
579 },
580 want: `{
581 "rptNested": [
582 {},
583 {}
584 ]
585}`,
586 }, {
587 desc: "repeated groups",
588 input: &pb2.Nests{
589 Rptgroup: []*pb2.Nests_RptGroup{
590 {
591 RptString: []string{"hello", "world"},
592 },
593 {},
594 nil,
595 },
596 },
597 want: `{
598 "rptgroup": [
599 {
600 "rptString": [
601 "hello",
602 "world"
603 ]
604 },
605 {},
606 {}
607 ]
608}`,
609 }, {
610 desc: "map fields not set",
611 input: &pb3.Maps{},
612 want: "{}",
613 }, {
614 desc: "map fields set to empty",
615 input: &pb3.Maps{
616 Int32ToStr: map[int32]string{},
617 BoolToUint32: map[bool]uint32{},
618 Uint64ToEnum: map[uint64]pb3.Enum{},
619 StrToNested: map[string]*pb3.Nested{},
620 StrToOneofs: map[string]*pb3.Oneofs{},
621 },
622 want: "{}",
623 }, {
624 desc: "map fields 1",
625 input: &pb3.Maps{
626 BoolToUint32: map[bool]uint32{
627 true: 42,
628 false: 101,
629 },
630 },
631 want: `{
632 "boolToUint32": {
633 "false": 101,
634 "true": 42
635 }
636}`,
637 }, {
638 desc: "map fields 2",
639 input: &pb3.Maps{
640 Int32ToStr: map[int32]string{
641 -101: "-101",
642 0xff: "0xff",
643 0: "zero",
644 },
645 },
646 want: `{
647 "int32ToStr": {
648 "-101": "-101",
649 "0": "zero",
650 "255": "0xff"
651 }
652}`,
653 }, {
654 desc: "map fields 3",
655 input: &pb3.Maps{
656 Uint64ToEnum: map[uint64]pb3.Enum{
657 1: pb3.Enum_ONE,
658 2: pb3.Enum_TWO,
659 10: pb3.Enum_TEN,
660 47: 47,
661 },
662 },
663 want: `{
664 "uint64ToEnum": {
665 "1": "ONE",
666 "2": "TWO",
667 "10": "TEN",
668 "47": 47
669 }
670}`,
671 }, {
672 desc: "map fields 4",
673 input: &pb3.Maps{
674 StrToNested: map[string]*pb3.Nested{
675 "nested": &pb3.Nested{
676 SString: "nested in a map",
677 },
678 },
679 },
680 want: `{
681 "strToNested": {
682 "nested": {
683 "sString": "nested in a map"
684 }
685 }
686}`,
687 }, {
688 desc: "map fields 5",
689 input: &pb3.Maps{
690 StrToOneofs: map[string]*pb3.Oneofs{
691 "string": &pb3.Oneofs{
692 Union: &pb3.Oneofs_OneofString{
693 OneofString: "hello",
694 },
695 },
696 "nested": &pb3.Oneofs{
697 Union: &pb3.Oneofs_OneofNested{
698 OneofNested: &pb3.Nested{
699 SString: "nested oneof in map field value",
700 },
701 },
702 },
703 },
704 },
705 want: `{
706 "strToOneofs": {
707 "nested": {
708 "oneofNested": {
709 "sString": "nested oneof in map field value"
710 }
711 },
712 "string": {
713 "oneofString": "hello"
714 }
715 }
716}`,
717 }, {
718 desc: "map field contains nil value",
719 input: &pb3.Maps{
720 StrToNested: map[string]*pb3.Nested{
721 "nil": nil,
722 },
723 },
724 want: `{
725 "strToNested": {
726 "nil": {}
727 }
728}`,
729 }, {
730 desc: "unknown fields are ignored",
731 input: &pb2.Scalars{
732 OptString: scalar.String("no unknowns"),
733 XXX_unrecognized: pack.Message{
734 pack.Tag{101, pack.BytesType}, pack.String("hello world"),
735 }.Marshal(),
736 },
737 want: `{
738 "optString": "no unknowns"
739}`,
740 }, {
741 desc: "json_name",
742 input: &pb3.JSONNames{
743 SString: "json_name",
744 },
745 want: `{
746 "foo_bar": "json_name"
747}`,
Herbie Ongf83d5bb2019-03-14 00:01:27 -0700748 }, {
749 desc: "extensions of non-repeated fields",
750 input: func() proto.Message {
751 m := &pb2.Extensions{
752 OptString: scalar.String("non-extension field"),
753 OptBool: scalar.Bool(true),
754 OptInt32: scalar.Int32(42),
755 }
756 setExtension(m, pb2.E_OptExtBool, true)
757 setExtension(m, pb2.E_OptExtString, "extension field")
758 setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
759 setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
760 OptString: scalar.String("nested in an extension"),
761 OptNested: &pb2.Nested{
762 OptString: scalar.String("another nested in an extension"),
763 },
764 })
765 return m
766 }(),
767 want: `{
768 "optString": "non-extension field",
769 "optBool": true,
770 "optInt32": 42,
771 "[pb2.opt_ext_bool]": true,
772 "[pb2.opt_ext_enum]": "TEN",
773 "[pb2.opt_ext_nested]": {
774 "optString": "nested in an extension",
775 "optNested": {
776 "optString": "another nested in an extension"
777 }
778 },
779 "[pb2.opt_ext_string]": "extension field"
780}`,
781 }, {
782 desc: "extension message field set to nil",
783 input: func() proto.Message {
784 m := &pb2.Extensions{}
785 setExtension(m, pb2.E_OptExtNested, nil)
786 return m
787 }(),
788 want: "{}",
789 }, {
790 desc: "extensions of repeated fields",
791 input: func() proto.Message {
792 m := &pb2.Extensions{}
793 setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
794 setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
795 setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
796 &pb2.Nested{OptString: scalar.String("one")},
797 &pb2.Nested{OptString: scalar.String("two")},
798 &pb2.Nested{OptString: scalar.String("three")},
799 })
800 return m
801 }(),
802 want: `{
803 "[pb2.rpt_ext_enum]": [
804 "TEN",
805 101,
806 "ONE"
807 ],
808 "[pb2.rpt_ext_fixed32]": [
809 42,
810 47
811 ],
812 "[pb2.rpt_ext_nested]": [
813 {
814 "optString": "one"
815 },
816 {
817 "optString": "two"
818 },
819 {
820 "optString": "three"
821 }
822 ]
823}`,
824 }, {
825 desc: "extensions of non-repeated fields in another message",
826 input: func() proto.Message {
827 m := &pb2.Extensions{}
828 setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
829 setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
830 setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
831 setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
832 OptString: scalar.String("nested in an extension"),
833 OptNested: &pb2.Nested{
834 OptString: scalar.String("another nested in an extension"),
835 },
836 })
837 return m
838 }(),
839 want: `{
840 "[pb2.ExtensionsContainer.opt_ext_bool]": true,
841 "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
842 "[pb2.ExtensionsContainer.opt_ext_nested]": {
843 "optString": "nested in an extension",
844 "optNested": {
845 "optString": "another nested in an extension"
846 }
847 },
848 "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
849}`,
850 }, {
851 desc: "extensions of repeated fields in another message",
852 input: func() proto.Message {
853 m := &pb2.Extensions{
854 OptString: scalar.String("non-extension field"),
855 OptBool: scalar.Bool(true),
856 OptInt32: scalar.Int32(42),
857 }
858 setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
859 setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
860 setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
861 &pb2.Nested{OptString: scalar.String("one")},
862 &pb2.Nested{OptString: scalar.String("two")},
863 &pb2.Nested{OptString: scalar.String("three")},
864 })
865 return m
866 }(),
867 want: `{
868 "optString": "non-extension field",
869 "optBool": true,
870 "optInt32": 42,
871 "[pb2.ExtensionsContainer.rpt_ext_enum]": [
872 "TEN",
873 101,
874 "ONE"
875 ],
876 "[pb2.ExtensionsContainer.rpt_ext_nested]": [
877 {
878 "optString": "one"
879 },
880 {
881 "optString": "two"
882 },
883 {
884 "optString": "three"
885 }
886 ],
887 "[pb2.ExtensionsContainer.rpt_ext_string]": [
888 "hello",
889 "world"
890 ]
891}`,
892 }, {
893 desc: "MessageSet",
894 input: func() proto.Message {
895 m := &pb2.MessageSet{}
896 setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
897 OptString: scalar.String("a messageset extension"),
898 })
899 setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
900 OptString: scalar.String("not a messageset extension"),
901 })
902 setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
903 OptString: scalar.String("just a regular extension"),
904 })
905 return m
906 }(),
907 want: `{
908 "[pb2.MessageSetExtension]": {
909 "optString": "a messageset extension"
910 },
911 "[pb2.MessageSetExtension.ext_nested]": {
912 "optString": "just a regular extension"
913 },
914 "[pb2.MessageSetExtension.not_message_set_extension]": {
915 "optString": "not a messageset extension"
916 }
917}`,
918 }, {
919 desc: "not real MessageSet 1",
920 input: func() proto.Message {
921 m := &pb2.FakeMessageSet{}
922 setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
923 OptString: scalar.String("not a messageset extension"),
924 })
925 return m
926 }(),
927 want: `{
928 "[pb2.FakeMessageSetExtension.message_set_extension]": {
929 "optString": "not a messageset extension"
930 }
931}`,
932 }, {
933 desc: "not real MessageSet 2",
934 input: func() proto.Message {
935 m := &pb2.MessageSet{}
936 setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
937 OptString: scalar.String("another not a messageset extension"),
938 })
939 return m
940 }(),
941 want: `{
942 "[pb2.message_set_extension]": {
943 "optString": "another not a messageset extension"
944 }
945}`,
Herbie Ong0b0f4032019-03-18 19:06:15 -0700946 }, {
947 desc: "BoolValue empty",
948 input: &knownpb.BoolValue{},
949 want: `false`,
950 }, {
951 desc: "BoolValue",
952 input: &knownpb.BoolValue{Value: true},
953 want: `true`,
954 }, {
955 desc: "Int32Value empty",
956 input: &knownpb.Int32Value{},
957 want: `0`,
958 }, {
959 desc: "Int32Value",
960 input: &knownpb.Int32Value{Value: 42},
961 want: `42`,
962 }, {
963 desc: "Int64Value",
964 input: &knownpb.Int64Value{Value: 42},
965 want: `"42"`,
966 }, {
967 desc: "UInt32Value",
968 input: &knownpb.UInt32Value{Value: 42},
969 want: `42`,
970 }, {
971 desc: "UInt64Value",
972 input: &knownpb.UInt64Value{Value: 42},
973 want: `"42"`,
974 }, {
975 desc: "FloatValue",
976 input: &knownpb.FloatValue{Value: 1.02},
977 want: `1.02`,
978 }, {
979 desc: "DoubleValue",
980 input: &knownpb.DoubleValue{Value: 1.02},
981 want: `1.02`,
982 }, {
983 desc: "StringValue empty",
984 input: &knownpb.StringValue{},
985 want: `""`,
986 }, {
987 desc: "StringValue",
988 input: &knownpb.StringValue{Value: "谷歌"},
989 want: `"谷歌"`,
990 }, {
991 desc: "StringValue with invalid UTF8 error",
992 input: &knownpb.StringValue{Value: "abc\xff"},
993 want: "\"abc\xff\"",
994 wantErr: true,
995 }, {
996 desc: "StringValue field with invalid UTF8 error",
997 input: &pb2.KnownTypes{
998 OptString: &knownpb.StringValue{Value: "abc\xff"},
999 },
1000 want: "{\n \"optString\": \"abc\xff\"\n}",
1001 wantErr: true,
1002 }, {
1003 desc: "BytesValue",
1004 input: &knownpb.BytesValue{Value: []byte("hello")},
1005 want: `"aGVsbG8="`,
1006 }, {
1007 desc: "Empty",
1008 input: &knownpb.Empty{},
1009 want: `{}`,
1010 }, {
1011 desc: "Value empty",
1012 input: &knownpb.Value{},
1013 want: ``,
1014 }, {
1015 desc: "Value empty field",
1016 input: &pb2.KnownTypes{
1017 OptValue: &knownpb.Value{},
1018 },
1019 want: `{}`,
1020 }, {
1021 desc: "Value contains NullValue",
1022 input: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
1023 want: `null`,
1024 }, {
1025 desc: "Value contains BoolValue",
1026 input: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}},
1027 want: `false`,
1028 }, {
1029 desc: "Value contains NumberValue",
1030 input: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
1031 want: `1.02`,
1032 }, {
1033 desc: "Value contains StringValue",
1034 input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}},
1035 want: `"hello"`,
1036 }, {
1037 desc: "Value contains StringValue with invalid UTF8",
1038 input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
1039 want: "\"\xff\"",
1040 wantErr: true,
1041 }, {
1042 desc: "Value contains Struct",
1043 input: &knownpb.Value{
1044 Kind: &knownpb.Value_StructValue{
1045 &knownpb.Struct{
1046 Fields: map[string]*knownpb.Value{
1047 "null": {Kind: &knownpb.Value_NullValue{}},
1048 "number": {Kind: &knownpb.Value_NumberValue{}},
1049 "string": {Kind: &knownpb.Value_StringValue{}},
1050 "struct": {Kind: &knownpb.Value_StructValue{}},
1051 "list": {Kind: &knownpb.Value_ListValue{}},
1052 "bool": {Kind: &knownpb.Value_BoolValue{}},
1053 },
1054 },
1055 },
1056 },
1057 want: `{
1058 "bool": false,
1059 "list": [],
1060 "null": null,
1061 "number": 0,
1062 "string": "",
1063 "struct": {}
1064}`,
1065 }, {
1066 desc: "Value contains ListValue",
1067 input: &knownpb.Value{
1068 Kind: &knownpb.Value_ListValue{
1069 &knownpb.ListValue{
1070 Values: []*knownpb.Value{
1071 {Kind: &knownpb.Value_BoolValue{}},
1072 {Kind: &knownpb.Value_NullValue{}},
1073 {Kind: &knownpb.Value_NumberValue{}},
1074 {Kind: &knownpb.Value_StringValue{}},
1075 {Kind: &knownpb.Value_StructValue{}},
1076 {Kind: &knownpb.Value_ListValue{}},
1077 },
1078 },
1079 },
1080 },
1081 want: `[
1082 false,
1083 null,
1084 0,
1085 "",
1086 {},
1087 []
1088]`,
1089 }, {
1090 desc: "Struct with nil map",
1091 input: &knownpb.Struct{},
1092 want: `{}`,
1093 }, {
1094 desc: "Struct with empty map",
1095 input: &knownpb.Struct{
1096 Fields: map[string]*knownpb.Value{},
1097 },
1098 want: `{}`,
1099 }, {
1100 desc: "Struct",
1101 input: &knownpb.Struct{
1102 Fields: map[string]*knownpb.Value{
1103 "bool": {Kind: &knownpb.Value_BoolValue{true}},
1104 "null": {Kind: &knownpb.Value_NullValue{}},
1105 "number": {Kind: &knownpb.Value_NumberValue{3.1415}},
1106 "string": {Kind: &knownpb.Value_StringValue{"hello"}},
1107 "struct": {
1108 Kind: &knownpb.Value_StructValue{
1109 &knownpb.Struct{
1110 Fields: map[string]*knownpb.Value{
1111 "string": {Kind: &knownpb.Value_StringValue{"world"}},
1112 },
1113 },
1114 },
1115 },
1116 "list": {
1117 Kind: &knownpb.Value_ListValue{
1118 &knownpb.ListValue{
1119 Values: []*knownpb.Value{
1120 {Kind: &knownpb.Value_BoolValue{}},
1121 {Kind: &knownpb.Value_NullValue{}},
1122 {Kind: &knownpb.Value_NumberValue{}},
1123 },
1124 },
1125 },
1126 },
1127 },
1128 },
1129 want: `{
1130 "bool": true,
1131 "list": [
1132 false,
1133 null,
1134 0
1135 ],
1136 "null": null,
1137 "number": 3.1415,
1138 "string": "hello",
1139 "struct": {
1140 "string": "world"
1141 }
1142}`,
1143 }, {
1144 desc: "Struct message with invalid UTF8 string",
1145 input: &knownpb.Struct{
1146 Fields: map[string]*knownpb.Value{
1147 "string": {Kind: &knownpb.Value_StringValue{"\xff"}},
1148 },
1149 },
1150 want: "{\n \"string\": \"\xff\"\n}",
1151 wantErr: true,
1152 }, {
1153 desc: "ListValue with nil values",
1154 input: &knownpb.ListValue{},
1155 want: `[]`,
1156 }, {
1157 desc: "ListValue with empty values",
1158 input: &knownpb.ListValue{
1159 Values: []*knownpb.Value{},
1160 },
1161 want: `[]`,
1162 }, {
1163 desc: "ListValue",
1164 input: &knownpb.ListValue{
1165 Values: []*knownpb.Value{
1166 {Kind: &knownpb.Value_BoolValue{true}},
1167 {Kind: &knownpb.Value_NullValue{}},
1168 {Kind: &knownpb.Value_NumberValue{3.1415}},
1169 {Kind: &knownpb.Value_StringValue{"hello"}},
1170 {
1171 Kind: &knownpb.Value_ListValue{
1172 &knownpb.ListValue{
1173 Values: []*knownpb.Value{
1174 {Kind: &knownpb.Value_BoolValue{}},
1175 {Kind: &knownpb.Value_NullValue{}},
1176 {Kind: &knownpb.Value_NumberValue{}},
1177 },
1178 },
1179 },
1180 },
1181 {
1182 Kind: &knownpb.Value_StructValue{
1183 &knownpb.Struct{
1184 Fields: map[string]*knownpb.Value{
1185 "string": {Kind: &knownpb.Value_StringValue{"world"}},
1186 },
1187 },
1188 },
1189 },
1190 },
1191 },
1192 want: `[
1193 true,
1194 null,
1195 3.1415,
1196 "hello",
1197 [
1198 false,
1199 null,
1200 0
1201 ],
1202 {
1203 "string": "world"
1204 }
1205]`,
1206 }, {
1207 desc: "ListValue with invalid UTF8 string",
1208 input: &knownpb.ListValue{
1209 Values: []*knownpb.Value{
1210 {Kind: &knownpb.Value_StringValue{"\xff"}},
1211 },
1212 },
1213 want: "[\n \"\xff\"\n]",
1214 wantErr: true,
1215 }, {
1216 desc: "Duration empty",
1217 input: &knownpb.Duration{},
1218 want: `"0s"`,
1219 }, {
1220 desc: "Duration with secs",
1221 input: &knownpb.Duration{Seconds: 3},
1222 want: `"3s"`,
1223 }, {
1224 desc: "Duration with -secs",
1225 input: &knownpb.Duration{Seconds: -3},
1226 want: `"-3s"`,
1227 }, {
1228 desc: "Duration with nanos",
1229 input: &knownpb.Duration{Nanos: 1e6},
1230 want: `"0.001s"`,
1231 }, {
1232 desc: "Duration with -nanos",
1233 input: &knownpb.Duration{Nanos: -1e6},
1234 want: `"-0.001s"`,
1235 }, {
1236 desc: "Duration with large secs",
1237 input: &knownpb.Duration{Seconds: 1e10, Nanos: 1},
1238 want: `"10000000000.000000001s"`,
1239 }, {
1240 desc: "Duration with 6-digit nanos",
1241 input: &knownpb.Duration{Nanos: 1e4},
1242 want: `"0.000010s"`,
1243 }, {
1244 desc: "Duration with 3-digit nanos",
1245 input: &knownpb.Duration{Nanos: 1e6},
1246 want: `"0.001s"`,
1247 }, {
1248 desc: "Duration with -secs -nanos",
1249 input: &knownpb.Duration{Seconds: -123, Nanos: -450},
1250 want: `"-123.000000450s"`,
1251 }, {
1252 desc: "Duration with +secs -nanos",
1253 input: &knownpb.Duration{Seconds: 1, Nanos: -1},
1254 wantErr: true,
1255 }, {
1256 desc: "Duration with -secs +nanos",
1257 input: &knownpb.Duration{Seconds: -1, Nanos: 1},
1258 wantErr: true,
1259 }, {
1260 desc: "Duration with +secs out of range",
1261 input: &knownpb.Duration{Seconds: 315576000001},
1262 wantErr: true,
1263 }, {
1264 desc: "Duration with -secs out of range",
1265 input: &knownpb.Duration{Seconds: -315576000001},
1266 wantErr: true,
1267 }, {
1268 desc: "Duration with +nanos out of range",
1269 input: &knownpb.Duration{Seconds: 0, Nanos: 1e9},
1270 wantErr: true,
1271 }, {
1272 desc: "Duration with -nanos out of range",
1273 input: &knownpb.Duration{Seconds: 0, Nanos: -1e9},
1274 wantErr: true,
1275 }, {
1276 desc: "Timestamp zero",
1277 input: &knownpb.Timestamp{},
1278 want: `"1970-01-01T00:00:00Z"`,
1279 }, {
1280 desc: "Timestamp",
1281 input: &knownpb.Timestamp{Seconds: 1553036601},
1282 want: `"2019-03-19T23:03:21Z"`,
1283 }, {
1284 desc: "Timestamp with nanos",
1285 input: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1},
1286 want: `"2019-03-19T23:03:21.000000001Z"`,
1287 }, {
1288 desc: "Timestamp with 6-digit nanos",
1289 input: &knownpb.Timestamp{Nanos: 1e3},
1290 want: `"1970-01-01T00:00:00.000001Z"`,
1291 }, {
1292 desc: "Timestamp with 3-digit nanos",
1293 input: &knownpb.Timestamp{Nanos: 1e7},
1294 want: `"1970-01-01T00:00:00.010Z"`,
1295 }, {
1296 desc: "Timestamp with +secs out of range",
1297 input: &knownpb.Timestamp{Seconds: 253402300800},
1298 wantErr: true,
1299 }, {
1300 desc: "Timestamp with -secs out of range",
1301 input: &knownpb.Timestamp{Seconds: -62135596801},
1302 wantErr: true,
1303 }, {
1304 desc: "Timestamp with -nanos",
1305 input: &knownpb.Timestamp{Nanos: -1},
1306 wantErr: true,
1307 }, {
1308 desc: "Timestamp with +nanos out of range",
1309 input: &knownpb.Timestamp{Nanos: 1e9},
1310 wantErr: true,
1311 }, {
1312 desc: "FieldMask empty",
1313 input: &knownpb.FieldMask{},
1314 want: `""`,
1315 }, {
1316 desc: "FieldMask",
1317 input: &knownpb.FieldMask{
1318 Paths: []string{
1319 "foo",
1320 "foo_bar",
1321 "foo.bar_qux",
1322 "_foo",
1323 },
1324 },
1325 want: `"foo,fooBar,foo.barQux,Foo"`,
1326 }, {
1327 desc: "FieldMask error 1",
1328 input: &knownpb.FieldMask{
1329 Paths: []string{"foo_"},
1330 },
1331 wantErr: true,
1332 }, {
1333 desc: "FieldMask error 2",
1334 input: &knownpb.FieldMask{
1335 Paths: []string{"foo__bar"},
1336 },
1337 wantErr: true,
1338 }, {
1339 desc: "Any empty",
1340 input: &knownpb.Any{},
1341 want: `{}`,
1342 }, {
1343 desc: "Any",
1344 mo: jsonpb.MarshalOptions{
1345 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1346 },
1347 input: func() proto.Message {
1348 m := &pb2.Nested{
1349 OptString: scalar.String("embedded inside Any"),
1350 OptNested: &pb2.Nested{
1351 OptString: scalar.String("inception"),
1352 },
1353 }
1354 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1355 if err != nil {
1356 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1357 }
1358 return &knownpb.Any{
1359 TypeUrl: "foo/pb2.Nested",
1360 Value: b,
1361 }
1362 }(),
1363 want: `{
1364 "@type": "foo/pb2.Nested",
1365 "optString": "embedded inside Any",
1366 "optNested": {
1367 "optString": "inception"
1368 }
1369}`,
1370 }, {
1371 desc: "Any without value",
1372 mo: jsonpb.MarshalOptions{
1373 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1374 },
1375 input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
1376 want: `{
1377 "@type": "foo/pb2.Nested"
1378}`,
1379 }, {
1380 desc: "Any without registered type",
1381 mo: jsonpb.MarshalOptions{Resolver: preg.NewTypes()},
1382 input: func() proto.Message {
1383 return &knownpb.Any{TypeUrl: "foo/pb2.Nested"}
1384 }(),
1385 wantErr: true,
1386 }, {
1387 desc: "Any with missing required error",
1388 mo: jsonpb.MarshalOptions{
1389 Resolver: preg.NewTypes((&pb2.PartialRequired{}).ProtoReflect().Type()),
1390 },
1391 input: func() proto.Message {
1392 m := &pb2.PartialRequired{
1393 OptString: scalar.String("embedded inside Any"),
1394 }
1395 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1396 // TODO: Marshal may fail due to required field not set at some
1397 // point. Need to ignore required not set error here.
1398 if err != nil {
1399 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1400 }
1401 return &knownpb.Any{
1402 TypeUrl: string(m.ProtoReflect().Type().FullName()),
1403 Value: b,
1404 }
1405 }(),
1406 want: `{
1407 "@type": "pb2.PartialRequired",
1408 "optString": "embedded inside Any"
1409}`,
1410 wantErr: true,
1411 }, {
1412 desc: "Any with invalid UTF8",
1413 mo: jsonpb.MarshalOptions{
1414 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1415 },
1416 input: func() proto.Message {
1417 m := &pb2.Nested{
1418 OptString: scalar.String("abc\xff"),
1419 }
1420 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1421 if err != nil {
1422 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1423 }
1424 return &knownpb.Any{
1425 TypeUrl: "foo/pb2.Nested",
1426 Value: b,
1427 }
1428 }(),
1429 want: `{
1430 "@type": "foo/pb2.Nested",
1431 "optString": "` + "abc\xff" + `"
1432}`,
1433 wantErr: true,
1434 }, {
1435 desc: "Any with invalid value",
1436 mo: jsonpb.MarshalOptions{
1437 Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()),
1438 },
1439 input: &knownpb.Any{
1440 TypeUrl: "foo/pb2.Nested",
1441 Value: dhex("80"),
1442 },
1443 wantErr: true,
1444 }, {
1445 desc: "Any with BoolValue",
1446 mo: jsonpb.MarshalOptions{
1447 Resolver: preg.NewTypes((&knownpb.BoolValue{}).ProtoReflect().Type()),
1448 },
1449 input: func() proto.Message {
1450 m := &knownpb.BoolValue{Value: true}
1451 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1452 if err != nil {
1453 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1454 }
1455 return &knownpb.Any{
1456 TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
1457 Value: b,
1458 }
1459 }(),
1460 want: `{
1461 "@type": "type.googleapis.com/google.protobuf.BoolValue",
1462 "value": true
1463}`,
1464 }, {
1465 // TODO: Need clarification on the specification for this. See
1466 // https://github.com/protocolbuffers/protobuf/issues/5390
1467 desc: "Any with Empty",
1468 mo: jsonpb.MarshalOptions{
1469 Resolver: preg.NewTypes((&knownpb.Empty{}).ProtoReflect().Type()),
1470 },
1471 input: func() proto.Message {
1472 m := &knownpb.Empty{}
1473 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1474 if err != nil {
1475 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1476 }
1477 return &knownpb.Any{
1478 TypeUrl: "type.googleapis.com/google.protobuf.Empty",
1479 Value: b,
1480 }
1481 }(),
1482 want: `{
1483 "@type": "type.googleapis.com/google.protobuf.Empty"
1484}`,
1485 }, {
1486 desc: "Any with StringValue containing invalid UTF8",
1487 mo: jsonpb.MarshalOptions{
1488 Resolver: preg.NewTypes((&knownpb.StringValue{}).ProtoReflect().Type()),
1489 },
1490 input: func() proto.Message {
1491 m := &knownpb.StringValue{Value: "abc\xff"}
1492 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1493 if err != nil {
1494 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1495 }
1496 return &knownpb.Any{
1497 TypeUrl: "google.protobuf.StringValue",
1498 Value: b,
1499 }
1500 }(),
1501 want: `{
1502 "@type": "google.protobuf.StringValue",
1503 "value": "` + "abc\xff" + `"
1504}`,
1505 wantErr: true,
1506 }, {
1507 desc: "Any with Value of StringValue",
1508 mo: jsonpb.MarshalOptions{
1509 Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()),
1510 },
1511 input: func() proto.Message {
1512 m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abc\xff"}}
1513 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1514 if err != nil {
1515 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1516 }
1517 return &knownpb.Any{
1518 TypeUrl: "type.googleapis.com/google.protobuf.Value",
1519 Value: b,
1520 }
1521 }(),
1522 want: `{
1523 "@type": "type.googleapis.com/google.protobuf.Value",
1524 "value": "` + "abc\xff" + `"
1525}`,
1526 wantErr: true,
1527 }, {
1528 desc: "Any with empty Value",
1529 mo: jsonpb.MarshalOptions{
1530 Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()),
1531 },
1532 input: func() proto.Message {
1533 m := &knownpb.Value{}
1534 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1535 if err != nil {
1536 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1537 }
1538 return &knownpb.Any{
1539 TypeUrl: "type.googleapis.com/google.protobuf.Value",
1540 Value: b,
1541 }
1542 }(),
1543 want: `{
1544 "@type": "type.googleapis.com/google.protobuf.Value"
1545}`,
1546 }, {
1547 desc: "Any with Duration",
1548 mo: jsonpb.MarshalOptions{
1549 Resolver: preg.NewTypes((&knownpb.Duration{}).ProtoReflect().Type()),
1550 },
1551 input: func() proto.Message {
1552 m := &knownpb.Duration{}
1553 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1554 if err != nil {
1555 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1556 }
1557 return &knownpb.Any{
1558 TypeUrl: "type.googleapis.com/google.protobuf.Duration",
1559 Value: b,
1560 }
1561 }(),
1562 want: `{
1563 "@type": "type.googleapis.com/google.protobuf.Duration",
1564 "value": "0s"
1565}`,
1566 }, {
1567 desc: "Any with Struct",
1568 mo: jsonpb.MarshalOptions{
1569 Resolver: preg.NewTypes(
1570 (&knownpb.Struct{}).ProtoReflect().Type(),
1571 (&knownpb.Value{}).ProtoReflect().Type(),
1572 (&knownpb.BoolValue{}).ProtoReflect().Type(),
1573 knownpb.NullValue_NULL_VALUE.Type(),
1574 (&knownpb.StringValue{}).ProtoReflect().Type(),
1575 ),
1576 },
1577 input: func() proto.Message {
1578 m := &knownpb.Struct{
1579 Fields: map[string]*knownpb.Value{
1580 "bool": {Kind: &knownpb.Value_BoolValue{true}},
1581 "null": {Kind: &knownpb.Value_NullValue{}},
1582 "string": {Kind: &knownpb.Value_StringValue{"hello"}},
1583 "struct": {
1584 Kind: &knownpb.Value_StructValue{
1585 &knownpb.Struct{
1586 Fields: map[string]*knownpb.Value{
1587 "string": {Kind: &knownpb.Value_StringValue{"world"}},
1588 },
1589 },
1590 },
1591 },
1592 },
1593 }
1594 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1595 if err != nil {
1596 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1597 }
1598 return &knownpb.Any{
1599 TypeUrl: "google.protobuf.Struct",
1600 Value: b,
1601 }
1602 }(),
1603 want: `{
1604 "@type": "google.protobuf.Struct",
1605 "value": {
1606 "bool": true,
1607 "null": null,
1608 "string": "hello",
1609 "struct": {
1610 "string": "world"
1611 }
1612 }
1613}`,
1614 }, {
1615 desc: "well known types as field values",
1616 mo: jsonpb.MarshalOptions{
1617 Resolver: preg.NewTypes((&knownpb.Empty{}).ProtoReflect().Type()),
1618 },
1619 input: &pb2.KnownTypes{
1620 OptBool: &knownpb.BoolValue{Value: false},
1621 OptInt32: &knownpb.Int32Value{Value: 42},
1622 OptInt64: &knownpb.Int64Value{Value: 42},
1623 OptUint32: &knownpb.UInt32Value{Value: 42},
1624 OptUint64: &knownpb.UInt64Value{Value: 42},
1625 OptFloat: &knownpb.FloatValue{Value: 1.23},
1626 OptDouble: &knownpb.DoubleValue{Value: 3.1415},
1627 OptString: &knownpb.StringValue{Value: "hello"},
1628 OptBytes: &knownpb.BytesValue{Value: []byte("hello")},
1629 OptDuration: &knownpb.Duration{Seconds: 123},
1630 OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601},
1631 OptStruct: &knownpb.Struct{
1632 Fields: map[string]*knownpb.Value{
1633 "string": {Kind: &knownpb.Value_StringValue{"hello"}},
1634 },
1635 },
1636 OptList: &knownpb.ListValue{
1637 Values: []*knownpb.Value{
1638 {Kind: &knownpb.Value_NullValue{}},
1639 {Kind: &knownpb.Value_StringValue{}},
1640 {Kind: &knownpb.Value_StructValue{}},
1641 {Kind: &knownpb.Value_ListValue{}},
1642 },
1643 },
1644 OptValue: &knownpb.Value{},
1645 OptEmpty: &knownpb.Empty{},
1646 OptAny: &knownpb.Any{
1647 TypeUrl: "google.protobuf.Empty",
1648 },
1649 OptFieldmask: &knownpb.FieldMask{
1650 Paths: []string{"foo_bar", "bar_foo"},
1651 },
1652 },
1653 want: `{
1654 "optBool": false,
1655 "optInt32": 42,
1656 "optInt64": "42",
1657 "optUint32": 42,
1658 "optUint64": "42",
1659 "optFloat": 1.23,
1660 "optDouble": 3.1415,
1661 "optString": "hello",
1662 "optBytes": "aGVsbG8=",
1663 "optDuration": "123s",
1664 "optTimestamp": "2019-03-19T23:03:21Z",
1665 "optStruct": {
1666 "string": "hello"
1667 },
1668 "optList": [
1669 null,
1670 "",
1671 {},
1672 []
1673 ],
1674 "optEmpty": {},
1675 "optAny": {
1676 "@type": "google.protobuf.Empty"
1677 },
1678 "optFieldmask": "fooBar,barFoo"
1679}`,
Herbie Ong7b828bc2019-02-08 19:56:24 -08001680 }}
1681
1682 for _, tt := range tests {
1683 tt := tt
1684 t.Run(tt.desc, func(t *testing.T) {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001685 // Use 2-space indentation on all MarshalOptions.
1686 tt.mo.Indent = " "
Herbie Ong7b828bc2019-02-08 19:56:24 -08001687 b, err := tt.mo.Marshal(tt.input)
Herbie Ong0b0f4032019-03-18 19:06:15 -07001688 if err != nil && !tt.wantErr {
Herbie Ong7b828bc2019-02-08 19:56:24 -08001689 t.Errorf("Marshal() returned error: %v\n", err)
1690 }
Herbie Ong0b0f4032019-03-18 19:06:15 -07001691 if err == nil && tt.wantErr {
1692 t.Errorf("Marshal() got nil error, want error\n")
1693 }
Herbie Ong7b828bc2019-02-08 19:56:24 -08001694 got := string(b)
1695 if got != tt.want {
1696 t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
1697 if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
1698 t.Errorf("Marshal() diff -want +got\n%v\n", diff)
1699 }
1700 }
1701 })
1702 }
1703}