blob: d19c497fddbfc0725df5f7817034f400a18b2d71 [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
Damien Neil5c5b5312019-05-14 12:44:37 -07005package protojson_test
Herbie Ong7b828bc2019-02-08 19:56:24 -08006
7import (
Damien Neilbc310b52019-04-11 11:46:55 -07008 "bytes"
Herbie Ong7b828bc2019-02-08 19:56:24 -08009 "math"
Herbie Ong7b828bc2019-02-08 19:56:24 -080010 "testing"
11
Herbie Ong7b828bc2019-02-08 19:56:24 -080012 "github.com/google/go-cmp/cmp"
Damien Neil5c5b5312019-05-14 12:44:37 -070013 "google.golang.org/protobuf/encoding/protojson"
Damien Neile89e6242019-05-13 23:55:40 -070014 "google.golang.org/protobuf/internal/encoding/pack"
Damien Neile89e6242019-05-13 23:55:40 -070015 pimpl "google.golang.org/protobuf/internal/impl"
16 "google.golang.org/protobuf/internal/scalar"
17 "google.golang.org/protobuf/proto"
18 preg "google.golang.org/protobuf/reflect/protoregistry"
19 "google.golang.org/protobuf/runtime/protoiface"
Herbie Ong7b828bc2019-02-08 19:56:24 -080020
Damien Neile89e6242019-05-13 23:55:40 -070021 "google.golang.org/protobuf/encoding/testprotos/pb2"
22 "google.golang.org/protobuf/encoding/testprotos/pb3"
Joe Tsaia95b29f2019-05-16 12:47:20 -070023 "google.golang.org/protobuf/types/known/anypb"
24 "google.golang.org/protobuf/types/known/durationpb"
25 "google.golang.org/protobuf/types/known/emptypb"
26 "google.golang.org/protobuf/types/known/fieldmaskpb"
27 "google.golang.org/protobuf/types/known/structpb"
28 "google.golang.org/protobuf/types/known/timestamppb"
29 "google.golang.org/protobuf/types/known/wrapperspb"
Herbie Ong7b828bc2019-02-08 19:56:24 -080030)
31
Joe Tsai378c1322019-04-25 23:48:08 -070032// TODO: Replace this with proto.SetExtension.
Joe Tsai4fddeba2019-03-20 18:29:32 -070033func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
Joe Tsai378c1322019-04-25 23:48:08 -070034 m.ProtoReflect().Set(xd.Type, xd.Type.ValueOf(val))
Herbie Ongf83d5bb2019-03-14 00:01:27 -070035}
36
Herbie Ong7b828bc2019-02-08 19:56:24 -080037func TestMarshal(t *testing.T) {
38 tests := []struct {
Herbie Ong0b0f4032019-03-18 19:06:15 -070039 desc string
Damien Neil5c5b5312019-05-14 12:44:37 -070040 mo protojson.MarshalOptions
Herbie Ong0b0f4032019-03-18 19:06:15 -070041 input proto.Message
42 want string
43 wantErr bool // TODO: Verify error message substring.
Herbie Ong7b828bc2019-02-08 19:56:24 -080044 }{{
45 desc: "proto2 optional scalars not set",
46 input: &pb2.Scalars{},
47 want: "{}",
48 }, {
49 desc: "proto3 scalars not set",
50 input: &pb3.Scalars{},
51 want: "{}",
52 }, {
53 desc: "proto2 optional scalars set to zero values",
54 input: &pb2.Scalars{
55 OptBool: scalar.Bool(false),
56 OptInt32: scalar.Int32(0),
57 OptInt64: scalar.Int64(0),
58 OptUint32: scalar.Uint32(0),
59 OptUint64: scalar.Uint64(0),
60 OptSint32: scalar.Int32(0),
61 OptSint64: scalar.Int64(0),
62 OptFixed32: scalar.Uint32(0),
63 OptFixed64: scalar.Uint64(0),
64 OptSfixed32: scalar.Int32(0),
65 OptSfixed64: scalar.Int64(0),
66 OptFloat: scalar.Float32(0),
67 OptDouble: scalar.Float64(0),
68 OptBytes: []byte{},
69 OptString: scalar.String(""),
70 },
71 want: `{
72 "optBool": false,
73 "optInt32": 0,
74 "optInt64": "0",
75 "optUint32": 0,
76 "optUint64": "0",
77 "optSint32": 0,
78 "optSint64": "0",
79 "optFixed32": 0,
80 "optFixed64": "0",
81 "optSfixed32": 0,
82 "optSfixed64": "0",
83 "optFloat": 0,
84 "optDouble": 0,
85 "optBytes": "",
86 "optString": ""
87}`,
88 }, {
89 desc: "proto2 optional scalars set to some values",
90 input: &pb2.Scalars{
91 OptBool: scalar.Bool(true),
92 OptInt32: scalar.Int32(0xff),
93 OptInt64: scalar.Int64(0xdeadbeef),
94 OptUint32: scalar.Uint32(47),
95 OptUint64: scalar.Uint64(0xdeadbeef),
96 OptSint32: scalar.Int32(-1001),
97 OptSint64: scalar.Int64(-0xffff),
98 OptFixed64: scalar.Uint64(64),
99 OptSfixed32: scalar.Int32(-32),
100 OptFloat: scalar.Float32(1.02),
101 OptDouble: scalar.Float64(1.234),
Herbie Ong87608a72019-03-06 14:32:24 -0800102 OptBytes: []byte("谷歌"),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800103 OptString: scalar.String("谷歌"),
104 },
105 want: `{
106 "optBool": true,
107 "optInt32": 255,
108 "optInt64": "3735928559",
109 "optUint32": 47,
110 "optUint64": "3735928559",
111 "optSint32": -1001,
112 "optSint64": "-65535",
113 "optFixed64": "64",
114 "optSfixed32": -32,
115 "optFloat": 1.02,
116 "optDouble": 1.234,
117 "optBytes": "6LC35q2M",
118 "optString": "谷歌"
119}`,
120 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -0700121 desc: "string",
122 input: &pb3.Scalars{
123 SString: "谷歌",
124 },
125 want: `{
126 "sString": "谷歌"
127}`,
128 }, {
129 desc: "string with invalid UTF8",
130 input: &pb3.Scalars{
131 SString: "abc\xff",
132 },
Herbie Ong0b0f4032019-03-18 19:06:15 -0700133 wantErr: true,
134 }, {
Herbie Ong7b828bc2019-02-08 19:56:24 -0800135 desc: "float nan",
136 input: &pb3.Scalars{
137 SFloat: float32(math.NaN()),
138 },
139 want: `{
140 "sFloat": "NaN"
141}`,
142 }, {
143 desc: "float positive infinity",
144 input: &pb3.Scalars{
145 SFloat: float32(math.Inf(1)),
146 },
147 want: `{
148 "sFloat": "Infinity"
149}`,
150 }, {
151 desc: "float negative infinity",
152 input: &pb3.Scalars{
153 SFloat: float32(math.Inf(-1)),
154 },
155 want: `{
156 "sFloat": "-Infinity"
157}`,
158 }, {
159 desc: "double nan",
160 input: &pb3.Scalars{
161 SDouble: math.NaN(),
162 },
163 want: `{
164 "sDouble": "NaN"
165}`,
166 }, {
167 desc: "double positive infinity",
168 input: &pb3.Scalars{
169 SDouble: math.Inf(1),
170 },
171 want: `{
172 "sDouble": "Infinity"
173}`,
174 }, {
175 desc: "double negative infinity",
176 input: &pb3.Scalars{
177 SDouble: math.Inf(-1),
178 },
179 want: `{
180 "sDouble": "-Infinity"
181}`,
182 }, {
183 desc: "proto2 enum not set",
184 input: &pb2.Enums{},
185 want: "{}",
186 }, {
187 desc: "proto2 enum set to zero value",
188 input: &pb2.Enums{
Joe Tsai6dc168e2019-07-09 23:11:13 -0700189 OptEnum: pb2.Enum(0).Enum(),
190 OptNestedEnum: pb2.Enums_NestedEnum(0).Enum(),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800191 },
192 want: `{
193 "optEnum": 0,
194 "optNestedEnum": 0
195}`,
196 }, {
197 desc: "proto2 enum",
198 input: &pb2.Enums{
199 OptEnum: pb2.Enum_ONE.Enum(),
200 OptNestedEnum: pb2.Enums_UNO.Enum(),
201 },
202 want: `{
203 "optEnum": "ONE",
204 "optNestedEnum": "UNO"
205}`,
206 }, {
207 desc: "proto2 enum set to numeric values",
208 input: &pb2.Enums{
Joe Tsai6dc168e2019-07-09 23:11:13 -0700209 OptEnum: pb2.Enum(2).Enum(),
210 OptNestedEnum: pb2.Enums_NestedEnum(2).Enum(),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800211 },
212 want: `{
213 "optEnum": "TWO",
214 "optNestedEnum": "DOS"
215}`,
216 }, {
217 desc: "proto2 enum set to unnamed numeric values",
218 input: &pb2.Enums{
Joe Tsai6dc168e2019-07-09 23:11:13 -0700219 OptEnum: pb2.Enum(101).Enum(),
220 OptNestedEnum: pb2.Enums_NestedEnum(-101).Enum(),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800221 },
222 want: `{
223 "optEnum": 101,
224 "optNestedEnum": -101
225}`,
226 }, {
227 desc: "proto3 enum not set",
228 input: &pb3.Enums{},
229 want: "{}",
230 }, {
231 desc: "proto3 enum set to zero value",
232 input: &pb3.Enums{
233 SEnum: pb3.Enum_ZERO,
234 SNestedEnum: pb3.Enums_CERO,
235 },
236 want: "{}",
237 }, {
238 desc: "proto3 enum",
239 input: &pb3.Enums{
240 SEnum: pb3.Enum_ONE,
241 SNestedEnum: pb3.Enums_UNO,
242 },
243 want: `{
244 "sEnum": "ONE",
245 "sNestedEnum": "UNO"
246}`,
247 }, {
248 desc: "proto3 enum set to numeric values",
249 input: &pb3.Enums{
250 SEnum: 2,
251 SNestedEnum: 2,
252 },
253 want: `{
254 "sEnum": "TWO",
255 "sNestedEnum": "DOS"
256}`,
257 }, {
258 desc: "proto3 enum set to unnamed numeric values",
259 input: &pb3.Enums{
260 SEnum: -47,
261 SNestedEnum: 47,
262 },
263 want: `{
264 "sEnum": -47,
265 "sNestedEnum": 47
266}`,
267 }, {
268 desc: "proto2 nested message not set",
269 input: &pb2.Nests{},
270 want: "{}",
271 }, {
272 desc: "proto2 nested message set to empty",
273 input: &pb2.Nests{
274 OptNested: &pb2.Nested{},
275 Optgroup: &pb2.Nests_OptGroup{},
276 },
277 want: `{
278 "optNested": {},
279 "optgroup": {}
280}`,
281 }, {
282 desc: "proto2 nested messages",
283 input: &pb2.Nests{
284 OptNested: &pb2.Nested{
285 OptString: scalar.String("nested message"),
286 OptNested: &pb2.Nested{
287 OptString: scalar.String("another nested message"),
288 },
289 },
290 },
291 want: `{
292 "optNested": {
293 "optString": "nested message",
294 "optNested": {
295 "optString": "another nested message"
296 }
297 }
298}`,
299 }, {
300 desc: "proto2 groups",
301 input: &pb2.Nests{
302 Optgroup: &pb2.Nests_OptGroup{
303 OptString: scalar.String("inside a group"),
304 OptNested: &pb2.Nested{
305 OptString: scalar.String("nested message inside a group"),
306 },
307 Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
308 OptFixed32: scalar.Uint32(47),
309 },
310 },
311 },
312 want: `{
313 "optgroup": {
314 "optString": "inside a group",
315 "optNested": {
316 "optString": "nested message inside a group"
317 },
318 "optnestedgroup": {
319 "optFixed32": 47
320 }
321 }
322}`,
323 }, {
324 desc: "proto3 nested message not set",
325 input: &pb3.Nests{},
326 want: "{}",
327 }, {
328 desc: "proto3 nested message set to empty",
329 input: &pb3.Nests{
330 SNested: &pb3.Nested{},
331 },
332 want: `{
333 "sNested": {}
334}`,
335 }, {
336 desc: "proto3 nested message",
337 input: &pb3.Nests{
338 SNested: &pb3.Nested{
339 SString: "nested message",
340 SNested: &pb3.Nested{
341 SString: "another nested message",
342 },
343 },
344 },
345 want: `{
346 "sNested": {
347 "sString": "nested message",
348 "sNested": {
349 "sString": "another nested message"
350 }
351 }
352}`,
353 }, {
354 desc: "oneof not set",
355 input: &pb3.Oneofs{},
356 want: "{}",
357 }, {
358 desc: "oneof set to empty string",
359 input: &pb3.Oneofs{
360 Union: &pb3.Oneofs_OneofString{},
361 },
362 want: `{
363 "oneofString": ""
364}`,
365 }, {
366 desc: "oneof set to string",
367 input: &pb3.Oneofs{
368 Union: &pb3.Oneofs_OneofString{
369 OneofString: "hello",
370 },
371 },
372 want: `{
373 "oneofString": "hello"
374}`,
375 }, {
376 desc: "oneof set to enum",
377 input: &pb3.Oneofs{
378 Union: &pb3.Oneofs_OneofEnum{
379 OneofEnum: pb3.Enum_ZERO,
380 },
381 },
382 want: `{
383 "oneofEnum": "ZERO"
384}`,
385 }, {
386 desc: "oneof set to empty message",
387 input: &pb3.Oneofs{
388 Union: &pb3.Oneofs_OneofNested{
389 OneofNested: &pb3.Nested{},
390 },
391 },
392 want: `{
393 "oneofNested": {}
394}`,
395 }, {
396 desc: "oneof set to message",
397 input: &pb3.Oneofs{
398 Union: &pb3.Oneofs_OneofNested{
399 OneofNested: &pb3.Nested{
400 SString: "nested message",
401 },
402 },
403 },
404 want: `{
405 "oneofNested": {
406 "sString": "nested message"
407 }
408}`,
409 }, {
410 desc: "repeated fields not set",
411 input: &pb2.Repeats{},
412 want: "{}",
413 }, {
414 desc: "repeated fields set to empty slices",
415 input: &pb2.Repeats{
416 RptBool: []bool{},
417 RptInt32: []int32{},
418 RptInt64: []int64{},
419 RptUint32: []uint32{},
420 RptUint64: []uint64{},
421 RptFloat: []float32{},
422 RptDouble: []float64{},
423 RptBytes: [][]byte{},
424 },
425 want: "{}",
426 }, {
427 desc: "repeated fields set to some values",
428 input: &pb2.Repeats{
429 RptBool: []bool{true, false, true, true},
430 RptInt32: []int32{1, 6, 0, 0},
431 RptInt64: []int64{-64, 47},
432 RptUint32: []uint32{0xff, 0xffff},
433 RptUint64: []uint64{0xdeadbeef},
434 RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
435 RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
436 RptString: []string{"hello", "世界"},
437 RptBytes: [][]byte{
438 []byte("hello"),
439 []byte("\xe4\xb8\x96\xe7\x95\x8c"),
440 },
441 },
442 want: `{
443 "rptBool": [
444 true,
445 false,
446 true,
447 true
448 ],
449 "rptInt32": [
450 1,
451 6,
452 0,
453 0
454 ],
455 "rptInt64": [
456 "-64",
457 "47"
458 ],
459 "rptUint32": [
460 255,
461 65535
462 ],
463 "rptUint64": [
464 "3735928559"
465 ],
466 "rptFloat": [
467 "NaN",
468 "Infinity",
469 "-Infinity",
470 1.034
471 ],
472 "rptDouble": [
473 "NaN",
474 "Infinity",
475 "-Infinity",
476 1.23e-308
477 ],
478 "rptString": [
479 "hello",
480 "世界"
481 ],
482 "rptBytes": [
483 "aGVsbG8=",
484 "5LiW55WM"
485 ]
486}`,
487 }, {
488 desc: "repeated enums",
489 input: &pb2.Enums{
490 RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
491 RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
492 },
493 want: `{
494 "rptEnum": [
495 "ONE",
496 "TWO",
497 "TEN",
498 42
499 ],
500 "rptNestedEnum": [
501 "DOS",
502 47,
503 "DIEZ"
504 ]
505}`,
506 }, {
507 desc: "repeated messages set to empty",
508 input: &pb2.Nests{
509 RptNested: []*pb2.Nested{},
510 Rptgroup: []*pb2.Nests_RptGroup{},
511 },
512 want: "{}",
513 }, {
514 desc: "repeated messages",
515 input: &pb2.Nests{
516 RptNested: []*pb2.Nested{
517 {
518 OptString: scalar.String("repeat nested one"),
519 },
520 {
521 OptString: scalar.String("repeat nested two"),
522 OptNested: &pb2.Nested{
523 OptString: scalar.String("inside repeat nested two"),
524 },
525 },
526 {},
527 },
528 },
529 want: `{
530 "rptNested": [
531 {
532 "optString": "repeat nested one"
533 },
534 {
535 "optString": "repeat nested two",
536 "optNested": {
537 "optString": "inside repeat nested two"
538 }
539 },
540 {}
541 ]
542}`,
543 }, {
544 desc: "repeated messages contains nil value",
545 input: &pb2.Nests{
546 RptNested: []*pb2.Nested{nil, {}},
547 },
548 want: `{
549 "rptNested": [
550 {},
551 {}
552 ]
553}`,
554 }, {
555 desc: "repeated groups",
556 input: &pb2.Nests{
557 Rptgroup: []*pb2.Nests_RptGroup{
558 {
559 RptString: []string{"hello", "world"},
560 },
561 {},
562 nil,
563 },
564 },
565 want: `{
566 "rptgroup": [
567 {
568 "rptString": [
569 "hello",
570 "world"
571 ]
572 },
573 {},
574 {}
575 ]
576}`,
577 }, {
578 desc: "map fields not set",
579 input: &pb3.Maps{},
580 want: "{}",
581 }, {
582 desc: "map fields set to empty",
583 input: &pb3.Maps{
584 Int32ToStr: map[int32]string{},
585 BoolToUint32: map[bool]uint32{},
586 Uint64ToEnum: map[uint64]pb3.Enum{},
587 StrToNested: map[string]*pb3.Nested{},
588 StrToOneofs: map[string]*pb3.Oneofs{},
589 },
590 want: "{}",
591 }, {
592 desc: "map fields 1",
593 input: &pb3.Maps{
594 BoolToUint32: map[bool]uint32{
595 true: 42,
596 false: 101,
597 },
598 },
599 want: `{
600 "boolToUint32": {
601 "false": 101,
602 "true": 42
603 }
604}`,
605 }, {
606 desc: "map fields 2",
607 input: &pb3.Maps{
608 Int32ToStr: map[int32]string{
609 -101: "-101",
610 0xff: "0xff",
611 0: "zero",
612 },
613 },
614 want: `{
615 "int32ToStr": {
616 "-101": "-101",
617 "0": "zero",
618 "255": "0xff"
619 }
620}`,
621 }, {
622 desc: "map fields 3",
623 input: &pb3.Maps{
624 Uint64ToEnum: map[uint64]pb3.Enum{
625 1: pb3.Enum_ONE,
626 2: pb3.Enum_TWO,
627 10: pb3.Enum_TEN,
628 47: 47,
629 },
630 },
631 want: `{
632 "uint64ToEnum": {
633 "1": "ONE",
634 "2": "TWO",
635 "10": "TEN",
636 "47": 47
637 }
638}`,
639 }, {
640 desc: "map fields 4",
641 input: &pb3.Maps{
642 StrToNested: map[string]*pb3.Nested{
643 "nested": &pb3.Nested{
644 SString: "nested in a map",
645 },
646 },
647 },
648 want: `{
649 "strToNested": {
650 "nested": {
651 "sString": "nested in a map"
652 }
653 }
654}`,
655 }, {
656 desc: "map fields 5",
657 input: &pb3.Maps{
658 StrToOneofs: map[string]*pb3.Oneofs{
659 "string": &pb3.Oneofs{
660 Union: &pb3.Oneofs_OneofString{
661 OneofString: "hello",
662 },
663 },
664 "nested": &pb3.Oneofs{
665 Union: &pb3.Oneofs_OneofNested{
666 OneofNested: &pb3.Nested{
667 SString: "nested oneof in map field value",
668 },
669 },
670 },
671 },
672 },
673 want: `{
674 "strToOneofs": {
675 "nested": {
676 "oneofNested": {
677 "sString": "nested oneof in map field value"
678 }
679 },
680 "string": {
681 "oneofString": "hello"
682 }
683 }
684}`,
685 }, {
686 desc: "map field contains nil value",
687 input: &pb3.Maps{
688 StrToNested: map[string]*pb3.Nested{
689 "nil": nil,
690 },
691 },
692 want: `{
693 "strToNested": {
694 "nil": {}
695 }
696}`,
697 }, {
Herbie Ong329be5b2019-03-27 14:47:59 -0700698 desc: "required fields not set",
699 input: &pb2.Requireds{},
700 want: `{}`,
701 wantErr: true,
702 }, {
703 desc: "required fields partially set",
704 input: &pb2.Requireds{
705 ReqBool: scalar.Bool(false),
706 ReqSfixed64: scalar.Int64(0),
707 ReqDouble: scalar.Float64(1.23),
708 ReqString: scalar.String("hello"),
709 ReqEnum: pb2.Enum_ONE.Enum(),
710 },
711 want: `{
712 "reqBool": false,
713 "reqSfixed64": "0",
714 "reqDouble": 1.23,
715 "reqString": "hello",
716 "reqEnum": "ONE"
717}`,
718 wantErr: true,
719 }, {
720 desc: "required fields not set with AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -0700721 mo: protojson.MarshalOptions{AllowPartial: true},
Herbie Ong329be5b2019-03-27 14:47:59 -0700722 input: &pb2.Requireds{
723 ReqBool: scalar.Bool(false),
724 ReqSfixed64: scalar.Int64(0),
725 ReqDouble: scalar.Float64(1.23),
726 ReqString: scalar.String("hello"),
727 ReqEnum: pb2.Enum_ONE.Enum(),
728 },
729 want: `{
730 "reqBool": false,
731 "reqSfixed64": "0",
732 "reqDouble": 1.23,
733 "reqString": "hello",
734 "reqEnum": "ONE"
735}`,
736 }, {
737 desc: "required fields all set",
738 input: &pb2.Requireds{
739 ReqBool: scalar.Bool(false),
740 ReqSfixed64: scalar.Int64(0),
741 ReqDouble: scalar.Float64(1.23),
742 ReqString: scalar.String("hello"),
743 ReqEnum: pb2.Enum_ONE.Enum(),
744 ReqNested: &pb2.Nested{},
745 },
746 want: `{
747 "reqBool": false,
748 "reqSfixed64": "0",
749 "reqDouble": 1.23,
750 "reqString": "hello",
751 "reqEnum": "ONE",
752 "reqNested": {}
753}`,
754 }, {
755 desc: "indirect required field",
756 input: &pb2.IndirectRequired{
757 OptNested: &pb2.NestedWithRequired{},
758 },
759 want: `{
760 "optNested": {}
761}`,
762 wantErr: true,
763 }, {
764 desc: "indirect required field with AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -0700765 mo: protojson.MarshalOptions{AllowPartial: true},
Herbie Ong329be5b2019-03-27 14:47:59 -0700766 input: &pb2.IndirectRequired{
767 OptNested: &pb2.NestedWithRequired{},
768 },
769 want: `{
770 "optNested": {}
771}`,
772 }, {
773 desc: "indirect required field in empty repeated",
774 input: &pb2.IndirectRequired{
775 RptNested: []*pb2.NestedWithRequired{},
776 },
777 want: `{}`,
778 }, {
779 desc: "indirect required field in repeated",
780 input: &pb2.IndirectRequired{
781 RptNested: []*pb2.NestedWithRequired{
782 &pb2.NestedWithRequired{},
783 },
784 },
785 want: `{
786 "rptNested": [
787 {}
788 ]
789}`,
790 wantErr: true,
791 }, {
792 desc: "indirect required field in repeated with AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -0700793 mo: protojson.MarshalOptions{AllowPartial: true},
Herbie Ong329be5b2019-03-27 14:47:59 -0700794 input: &pb2.IndirectRequired{
795 RptNested: []*pb2.NestedWithRequired{
796 &pb2.NestedWithRequired{},
797 },
798 },
799 want: `{
800 "rptNested": [
801 {}
802 ]
803}`,
804 }, {
805 desc: "indirect required field in empty map",
806 input: &pb2.IndirectRequired{
807 StrToNested: map[string]*pb2.NestedWithRequired{},
808 },
809 want: "{}",
810 }, {
811 desc: "indirect required field in map",
812 input: &pb2.IndirectRequired{
813 StrToNested: map[string]*pb2.NestedWithRequired{
814 "fail": &pb2.NestedWithRequired{},
815 },
816 },
817 want: `{
818 "strToNested": {
819 "fail": {}
820 }
821}`,
822 wantErr: true,
823 }, {
824 desc: "indirect required field in map with AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -0700825 mo: protojson.MarshalOptions{AllowPartial: true},
Herbie Ong329be5b2019-03-27 14:47:59 -0700826 input: &pb2.IndirectRequired{
827 StrToNested: map[string]*pb2.NestedWithRequired{
828 "fail": &pb2.NestedWithRequired{},
829 },
830 },
831 want: `{
832 "strToNested": {
833 "fail": {}
834 }
835}`,
836 }, {
837 desc: "indirect required field in oneof",
838 input: &pb2.IndirectRequired{
839 Union: &pb2.IndirectRequired_OneofNested{
840 OneofNested: &pb2.NestedWithRequired{},
841 },
842 },
843 want: `{
844 "oneofNested": {}
845}`,
846 wantErr: true,
847 }, {
848 desc: "indirect required field in oneof with AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -0700849 mo: protojson.MarshalOptions{AllowPartial: true},
Herbie Ong329be5b2019-03-27 14:47:59 -0700850 input: &pb2.IndirectRequired{
851 Union: &pb2.IndirectRequired_OneofNested{
852 OneofNested: &pb2.NestedWithRequired{},
853 },
854 },
855 want: `{
856 "oneofNested": {}
857}`,
858 }, {
Herbie Ong7b828bc2019-02-08 19:56:24 -0800859 desc: "unknown fields are ignored",
Joe Tsai28216c72019-06-22 13:20:09 -0700860 input: func() proto.Message {
861 m := &pb2.Scalars{
862 OptString: scalar.String("no unknowns"),
863 }
864 m.ProtoReflect().SetUnknown(pack.Message{
Herbie Ong7b828bc2019-02-08 19:56:24 -0800865 pack.Tag{101, pack.BytesType}, pack.String("hello world"),
Joe Tsai28216c72019-06-22 13:20:09 -0700866 }.Marshal())
867 return m
868 }(),
Herbie Ong7b828bc2019-02-08 19:56:24 -0800869 want: `{
870 "optString": "no unknowns"
871}`,
872 }, {
873 desc: "json_name",
874 input: &pb3.JSONNames{
875 SString: "json_name",
876 },
877 want: `{
878 "foo_bar": "json_name"
879}`,
Herbie Ongf83d5bb2019-03-14 00:01:27 -0700880 }, {
881 desc: "extensions of non-repeated fields",
882 input: func() proto.Message {
883 m := &pb2.Extensions{
884 OptString: scalar.String("non-extension field"),
885 OptBool: scalar.Bool(true),
886 OptInt32: scalar.Int32(42),
887 }
888 setExtension(m, pb2.E_OptExtBool, true)
889 setExtension(m, pb2.E_OptExtString, "extension field")
890 setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
891 setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
892 OptString: scalar.String("nested in an extension"),
893 OptNested: &pb2.Nested{
894 OptString: scalar.String("another nested in an extension"),
895 },
896 })
897 return m
898 }(),
899 want: `{
900 "optString": "non-extension field",
901 "optBool": true,
902 "optInt32": 42,
903 "[pb2.opt_ext_bool]": true,
904 "[pb2.opt_ext_enum]": "TEN",
905 "[pb2.opt_ext_nested]": {
906 "optString": "nested in an extension",
907 "optNested": {
908 "optString": "another nested in an extension"
909 }
910 },
911 "[pb2.opt_ext_string]": "extension field"
912}`,
913 }, {
Herbie Ongf83d5bb2019-03-14 00:01:27 -0700914 desc: "extensions of repeated fields",
915 input: func() proto.Message {
916 m := &pb2.Extensions{}
917 setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
918 setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
919 setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
920 &pb2.Nested{OptString: scalar.String("one")},
921 &pb2.Nested{OptString: scalar.String("two")},
922 &pb2.Nested{OptString: scalar.String("three")},
923 })
924 return m
925 }(),
926 want: `{
927 "[pb2.rpt_ext_enum]": [
928 "TEN",
929 101,
930 "ONE"
931 ],
932 "[pb2.rpt_ext_fixed32]": [
933 42,
934 47
935 ],
936 "[pb2.rpt_ext_nested]": [
937 {
938 "optString": "one"
939 },
940 {
941 "optString": "two"
942 },
943 {
944 "optString": "three"
945 }
946 ]
947}`,
948 }, {
949 desc: "extensions of non-repeated fields in another message",
950 input: func() proto.Message {
951 m := &pb2.Extensions{}
952 setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
953 setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
954 setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
955 setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
956 OptString: scalar.String("nested in an extension"),
957 OptNested: &pb2.Nested{
958 OptString: scalar.String("another nested in an extension"),
959 },
960 })
961 return m
962 }(),
963 want: `{
964 "[pb2.ExtensionsContainer.opt_ext_bool]": true,
965 "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
966 "[pb2.ExtensionsContainer.opt_ext_nested]": {
967 "optString": "nested in an extension",
968 "optNested": {
969 "optString": "another nested in an extension"
970 }
971 },
972 "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
973}`,
974 }, {
975 desc: "extensions of repeated fields in another message",
976 input: func() proto.Message {
977 m := &pb2.Extensions{
978 OptString: scalar.String("non-extension field"),
979 OptBool: scalar.Bool(true),
980 OptInt32: scalar.Int32(42),
981 }
982 setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
983 setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
984 setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
985 &pb2.Nested{OptString: scalar.String("one")},
986 &pb2.Nested{OptString: scalar.String("two")},
987 &pb2.Nested{OptString: scalar.String("three")},
988 })
989 return m
990 }(),
991 want: `{
992 "optString": "non-extension field",
993 "optBool": true,
994 "optInt32": 42,
995 "[pb2.ExtensionsContainer.rpt_ext_enum]": [
996 "TEN",
997 101,
998 "ONE"
999 ],
1000 "[pb2.ExtensionsContainer.rpt_ext_nested]": [
1001 {
1002 "optString": "one"
1003 },
1004 {
1005 "optString": "two"
1006 },
1007 {
1008 "optString": "three"
1009 }
1010 ],
1011 "[pb2.ExtensionsContainer.rpt_ext_string]": [
1012 "hello",
1013 "world"
1014 ]
1015}`,
1016 }, {
1017 desc: "MessageSet",
1018 input: func() proto.Message {
1019 m := &pb2.MessageSet{}
1020 setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
1021 OptString: scalar.String("a messageset extension"),
1022 })
1023 setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
1024 OptString: scalar.String("not a messageset extension"),
1025 })
1026 setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
1027 OptString: scalar.String("just a regular extension"),
1028 })
1029 return m
1030 }(),
1031 want: `{
1032 "[pb2.MessageSetExtension]": {
1033 "optString": "a messageset extension"
1034 },
1035 "[pb2.MessageSetExtension.ext_nested]": {
1036 "optString": "just a regular extension"
1037 },
1038 "[pb2.MessageSetExtension.not_message_set_extension]": {
1039 "optString": "not a messageset extension"
1040 }
1041}`,
1042 }, {
1043 desc: "not real MessageSet 1",
1044 input: func() proto.Message {
1045 m := &pb2.FakeMessageSet{}
1046 setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
1047 OptString: scalar.String("not a messageset extension"),
1048 })
1049 return m
1050 }(),
1051 want: `{
1052 "[pb2.FakeMessageSetExtension.message_set_extension]": {
1053 "optString": "not a messageset extension"
1054 }
1055}`,
1056 }, {
1057 desc: "not real MessageSet 2",
1058 input: func() proto.Message {
1059 m := &pb2.MessageSet{}
1060 setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
1061 OptString: scalar.String("another not a messageset extension"),
1062 })
1063 return m
1064 }(),
1065 want: `{
1066 "[pb2.message_set_extension]": {
1067 "optString": "another not a messageset extension"
1068 }
1069}`,
Herbie Ong0b0f4032019-03-18 19:06:15 -07001070 }, {
1071 desc: "BoolValue empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001072 input: &wrapperspb.BoolValue{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001073 want: `false`,
1074 }, {
1075 desc: "BoolValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001076 input: &wrapperspb.BoolValue{Value: true},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001077 want: `true`,
1078 }, {
1079 desc: "Int32Value empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001080 input: &wrapperspb.Int32Value{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001081 want: `0`,
1082 }, {
1083 desc: "Int32Value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001084 input: &wrapperspb.Int32Value{Value: 42},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001085 want: `42`,
1086 }, {
1087 desc: "Int64Value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001088 input: &wrapperspb.Int64Value{Value: 42},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001089 want: `"42"`,
1090 }, {
1091 desc: "UInt32Value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001092 input: &wrapperspb.UInt32Value{Value: 42},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001093 want: `42`,
1094 }, {
1095 desc: "UInt64Value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001096 input: &wrapperspb.UInt64Value{Value: 42},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001097 want: `"42"`,
1098 }, {
1099 desc: "FloatValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001100 input: &wrapperspb.FloatValue{Value: 1.02},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001101 want: `1.02`,
1102 }, {
Herbie Onge63c4c42019-03-22 22:20:22 -07001103 desc: "FloatValue Infinity",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001104 input: &wrapperspb.FloatValue{Value: float32(math.Inf(-1))},
Herbie Onge63c4c42019-03-22 22:20:22 -07001105 want: `"-Infinity"`,
1106 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001107 desc: "DoubleValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001108 input: &wrapperspb.DoubleValue{Value: 1.02},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001109 want: `1.02`,
1110 }, {
Herbie Onge63c4c42019-03-22 22:20:22 -07001111 desc: "DoubleValue NaN",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001112 input: &wrapperspb.DoubleValue{Value: math.NaN()},
Herbie Onge63c4c42019-03-22 22:20:22 -07001113 want: `"NaN"`,
1114 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001115 desc: "StringValue empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001116 input: &wrapperspb.StringValue{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001117 want: `""`,
1118 }, {
1119 desc: "StringValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001120 input: &wrapperspb.StringValue{Value: "谷歌"},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001121 want: `"谷歌"`,
1122 }, {
1123 desc: "StringValue with invalid UTF8 error",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001124 input: &wrapperspb.StringValue{Value: "abc\xff"},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001125 wantErr: true,
1126 }, {
1127 desc: "StringValue field with invalid UTF8 error",
1128 input: &pb2.KnownTypes{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001129 OptString: &wrapperspb.StringValue{Value: "abc\xff"},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001130 },
Herbie Ong0b0f4032019-03-18 19:06:15 -07001131 wantErr: true,
1132 }, {
1133 desc: "BytesValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001134 input: &wrapperspb.BytesValue{Value: []byte("hello")},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001135 want: `"aGVsbG8="`,
1136 }, {
1137 desc: "Empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001138 input: &emptypb.Empty{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001139 want: `{}`,
1140 }, {
Herbie Ong300b9fe2019-03-29 15:42:20 -07001141 desc: "NullValue field",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001142 input: &pb2.KnownTypes{OptNull: new(structpb.NullValue)},
Herbie Ong300b9fe2019-03-29 15:42:20 -07001143 want: `{
1144 "optNull": null
1145}`,
1146 }, {
Herbie Ong1c7462c2019-03-22 17:56:55 -07001147 desc: "Value empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001148 input: &structpb.Value{},
Herbie Ong1c7462c2019-03-22 17:56:55 -07001149 wantErr: true,
Herbie Ong0b0f4032019-03-18 19:06:15 -07001150 }, {
1151 desc: "Value empty field",
1152 input: &pb2.KnownTypes{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001153 OptValue: &structpb.Value{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001154 },
Herbie Ong1c7462c2019-03-22 17:56:55 -07001155 wantErr: true,
Herbie Ong0b0f4032019-03-18 19:06:15 -07001156 }, {
1157 desc: "Value contains NullValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001158 input: &structpb.Value{Kind: &structpb.Value_NullValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001159 want: `null`,
1160 }, {
1161 desc: "Value contains BoolValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001162 input: &structpb.Value{Kind: &structpb.Value_BoolValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001163 want: `false`,
1164 }, {
1165 desc: "Value contains NumberValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001166 input: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001167 want: `1.02`,
1168 }, {
1169 desc: "Value contains StringValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001170 input: &structpb.Value{Kind: &structpb.Value_StringValue{"hello"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001171 want: `"hello"`,
1172 }, {
1173 desc: "Value contains StringValue with invalid UTF8",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001174 input: &structpb.Value{Kind: &structpb.Value_StringValue{"\xff"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001175 wantErr: true,
1176 }, {
1177 desc: "Value contains Struct",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001178 input: &structpb.Value{
1179 Kind: &structpb.Value_StructValue{
1180 &structpb.Struct{
1181 Fields: map[string]*structpb.Value{
1182 "null": {Kind: &structpb.Value_NullValue{}},
1183 "number": {Kind: &structpb.Value_NumberValue{}},
1184 "string": {Kind: &structpb.Value_StringValue{}},
1185 "struct": {Kind: &structpb.Value_StructValue{}},
1186 "list": {Kind: &structpb.Value_ListValue{}},
1187 "bool": {Kind: &structpb.Value_BoolValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001188 },
1189 },
1190 },
1191 },
1192 want: `{
1193 "bool": false,
1194 "list": [],
1195 "null": null,
1196 "number": 0,
1197 "string": "",
1198 "struct": {}
1199}`,
1200 }, {
1201 desc: "Value contains ListValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001202 input: &structpb.Value{
1203 Kind: &structpb.Value_ListValue{
1204 &structpb.ListValue{
1205 Values: []*structpb.Value{
1206 {Kind: &structpb.Value_BoolValue{}},
1207 {Kind: &structpb.Value_NullValue{}},
1208 {Kind: &structpb.Value_NumberValue{}},
1209 {Kind: &structpb.Value_StringValue{}},
1210 {Kind: &structpb.Value_StructValue{}},
1211 {Kind: &structpb.Value_ListValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001212 },
1213 },
1214 },
1215 },
1216 want: `[
1217 false,
1218 null,
1219 0,
1220 "",
1221 {},
1222 []
1223]`,
1224 }, {
1225 desc: "Struct with nil map",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001226 input: &structpb.Struct{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001227 want: `{}`,
1228 }, {
1229 desc: "Struct with empty map",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001230 input: &structpb.Struct{
1231 Fields: map[string]*structpb.Value{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001232 },
1233 want: `{}`,
1234 }, {
1235 desc: "Struct",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001236 input: &structpb.Struct{
1237 Fields: map[string]*structpb.Value{
1238 "bool": {Kind: &structpb.Value_BoolValue{true}},
1239 "null": {Kind: &structpb.Value_NullValue{}},
1240 "number": {Kind: &structpb.Value_NumberValue{3.1415}},
1241 "string": {Kind: &structpb.Value_StringValue{"hello"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001242 "struct": {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001243 Kind: &structpb.Value_StructValue{
1244 &structpb.Struct{
1245 Fields: map[string]*structpb.Value{
1246 "string": {Kind: &structpb.Value_StringValue{"world"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001247 },
1248 },
1249 },
1250 },
1251 "list": {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001252 Kind: &structpb.Value_ListValue{
1253 &structpb.ListValue{
1254 Values: []*structpb.Value{
1255 {Kind: &structpb.Value_BoolValue{}},
1256 {Kind: &structpb.Value_NullValue{}},
1257 {Kind: &structpb.Value_NumberValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001258 },
1259 },
1260 },
1261 },
1262 },
1263 },
1264 want: `{
1265 "bool": true,
1266 "list": [
1267 false,
1268 null,
1269 0
1270 ],
1271 "null": null,
1272 "number": 3.1415,
1273 "string": "hello",
1274 "struct": {
1275 "string": "world"
1276 }
1277}`,
1278 }, {
1279 desc: "Struct message with invalid UTF8 string",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001280 input: &structpb.Struct{
1281 Fields: map[string]*structpb.Value{
1282 "string": {Kind: &structpb.Value_StringValue{"\xff"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001283 },
1284 },
Herbie Ong0b0f4032019-03-18 19:06:15 -07001285 wantErr: true,
1286 }, {
1287 desc: "ListValue with nil values",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001288 input: &structpb.ListValue{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001289 want: `[]`,
1290 }, {
1291 desc: "ListValue with empty values",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001292 input: &structpb.ListValue{
1293 Values: []*structpb.Value{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001294 },
1295 want: `[]`,
1296 }, {
1297 desc: "ListValue",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001298 input: &structpb.ListValue{
1299 Values: []*structpb.Value{
1300 {Kind: &structpb.Value_BoolValue{true}},
1301 {Kind: &structpb.Value_NullValue{}},
1302 {Kind: &structpb.Value_NumberValue{3.1415}},
1303 {Kind: &structpb.Value_StringValue{"hello"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001304 {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001305 Kind: &structpb.Value_ListValue{
1306 &structpb.ListValue{
1307 Values: []*structpb.Value{
1308 {Kind: &structpb.Value_BoolValue{}},
1309 {Kind: &structpb.Value_NullValue{}},
1310 {Kind: &structpb.Value_NumberValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001311 },
1312 },
1313 },
1314 },
1315 {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001316 Kind: &structpb.Value_StructValue{
1317 &structpb.Struct{
1318 Fields: map[string]*structpb.Value{
1319 "string": {Kind: &structpb.Value_StringValue{"world"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001320 },
1321 },
1322 },
1323 },
1324 },
1325 },
1326 want: `[
1327 true,
1328 null,
1329 3.1415,
1330 "hello",
1331 [
1332 false,
1333 null,
1334 0
1335 ],
1336 {
1337 "string": "world"
1338 }
1339]`,
1340 }, {
1341 desc: "ListValue with invalid UTF8 string",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001342 input: &structpb.ListValue{
1343 Values: []*structpb.Value{
1344 {Kind: &structpb.Value_StringValue{"\xff"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001345 },
1346 },
Herbie Ong0b0f4032019-03-18 19:06:15 -07001347 wantErr: true,
1348 }, {
1349 desc: "Duration empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001350 input: &durationpb.Duration{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001351 want: `"0s"`,
1352 }, {
1353 desc: "Duration with secs",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001354 input: &durationpb.Duration{Seconds: 3},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001355 want: `"3s"`,
1356 }, {
1357 desc: "Duration with -secs",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001358 input: &durationpb.Duration{Seconds: -3},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001359 want: `"-3s"`,
1360 }, {
1361 desc: "Duration with nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001362 input: &durationpb.Duration{Nanos: 1e6},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001363 want: `"0.001s"`,
1364 }, {
1365 desc: "Duration with -nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001366 input: &durationpb.Duration{Nanos: -1e6},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001367 want: `"-0.001s"`,
1368 }, {
1369 desc: "Duration with large secs",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001370 input: &durationpb.Duration{Seconds: 1e10, Nanos: 1},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001371 want: `"10000000000.000000001s"`,
1372 }, {
1373 desc: "Duration with 6-digit nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001374 input: &durationpb.Duration{Nanos: 1e4},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001375 want: `"0.000010s"`,
1376 }, {
1377 desc: "Duration with 3-digit nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001378 input: &durationpb.Duration{Nanos: 1e6},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001379 want: `"0.001s"`,
1380 }, {
1381 desc: "Duration with -secs -nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001382 input: &durationpb.Duration{Seconds: -123, Nanos: -450},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001383 want: `"-123.000000450s"`,
1384 }, {
Herbie Ongad9c1252019-04-24 20:51:28 -07001385 desc: "Duration max value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001386 input: &durationpb.Duration{Seconds: 315576000000, Nanos: 999999999},
Herbie Ongad9c1252019-04-24 20:51:28 -07001387 want: `"315576000000.999999999s"`,
1388 }, {
1389 desc: "Duration min value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001390 input: &durationpb.Duration{Seconds: -315576000000, Nanos: -999999999},
Herbie Ongad9c1252019-04-24 20:51:28 -07001391 want: `"-315576000000.999999999s"`,
1392 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001393 desc: "Duration with +secs -nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001394 input: &durationpb.Duration{Seconds: 1, Nanos: -1},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001395 wantErr: true,
1396 }, {
1397 desc: "Duration with -secs +nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001398 input: &durationpb.Duration{Seconds: -1, Nanos: 1},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001399 wantErr: true,
1400 }, {
1401 desc: "Duration with +secs out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001402 input: &durationpb.Duration{Seconds: 315576000001},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001403 wantErr: true,
1404 }, {
1405 desc: "Duration with -secs out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001406 input: &durationpb.Duration{Seconds: -315576000001},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001407 wantErr: true,
1408 }, {
1409 desc: "Duration with +nanos out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001410 input: &durationpb.Duration{Seconds: 0, Nanos: 1e9},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001411 wantErr: true,
1412 }, {
1413 desc: "Duration with -nanos out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001414 input: &durationpb.Duration{Seconds: 0, Nanos: -1e9},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001415 wantErr: true,
1416 }, {
1417 desc: "Timestamp zero",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001418 input: &timestamppb.Timestamp{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001419 want: `"1970-01-01T00:00:00Z"`,
1420 }, {
1421 desc: "Timestamp",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001422 input: &timestamppb.Timestamp{Seconds: 1553036601},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001423 want: `"2019-03-19T23:03:21Z"`,
1424 }, {
1425 desc: "Timestamp with nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001426 input: &timestamppb.Timestamp{Seconds: 1553036601, Nanos: 1},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001427 want: `"2019-03-19T23:03:21.000000001Z"`,
1428 }, {
1429 desc: "Timestamp with 6-digit nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001430 input: &timestamppb.Timestamp{Nanos: 1e3},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001431 want: `"1970-01-01T00:00:00.000001Z"`,
1432 }, {
1433 desc: "Timestamp with 3-digit nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001434 input: &timestamppb.Timestamp{Nanos: 1e7},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001435 want: `"1970-01-01T00:00:00.010Z"`,
1436 }, {
Herbie Ongad9c1252019-04-24 20:51:28 -07001437 desc: "Timestamp max value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001438 input: &timestamppb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
Herbie Ongad9c1252019-04-24 20:51:28 -07001439 want: `"9999-12-31T23:59:59.999999999Z"`,
1440 }, {
1441 desc: "Timestamp min value",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001442 input: &timestamppb.Timestamp{Seconds: -62135596800},
Herbie Ongad9c1252019-04-24 20:51:28 -07001443 want: `"0001-01-01T00:00:00Z"`,
1444 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001445 desc: "Timestamp with +secs out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001446 input: &timestamppb.Timestamp{Seconds: 253402300800},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001447 wantErr: true,
1448 }, {
1449 desc: "Timestamp with -secs out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001450 input: &timestamppb.Timestamp{Seconds: -62135596801},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001451 wantErr: true,
1452 }, {
1453 desc: "Timestamp with -nanos",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001454 input: &timestamppb.Timestamp{Nanos: -1},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001455 wantErr: true,
1456 }, {
1457 desc: "Timestamp with +nanos out of range",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001458 input: &timestamppb.Timestamp{Nanos: 1e9},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001459 wantErr: true,
1460 }, {
1461 desc: "FieldMask empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001462 input: &fieldmaskpb.FieldMask{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001463 want: `""`,
1464 }, {
1465 desc: "FieldMask",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001466 input: &fieldmaskpb.FieldMask{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001467 Paths: []string{
1468 "foo",
1469 "foo_bar",
1470 "foo.bar_qux",
1471 "_foo",
1472 },
1473 },
1474 want: `"foo,fooBar,foo.barQux,Foo"`,
1475 }, {
1476 desc: "FieldMask error 1",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001477 input: &fieldmaskpb.FieldMask{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001478 Paths: []string{"foo_"},
1479 },
1480 wantErr: true,
1481 }, {
1482 desc: "FieldMask error 2",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001483 input: &fieldmaskpb.FieldMask{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001484 Paths: []string{"foo__bar"},
1485 },
1486 wantErr: true,
1487 }, {
1488 desc: "Any empty",
Joe Tsaia95b29f2019-05-16 12:47:20 -07001489 input: &anypb.Any{},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001490 want: `{}`,
1491 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001492 desc: "Any with non-custom message",
Damien Neil5c5b5312019-05-14 12:44:37 -07001493 mo: protojson.MarshalOptions{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001494 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001495 },
1496 input: func() proto.Message {
1497 m := &pb2.Nested{
1498 OptString: scalar.String("embedded inside Any"),
1499 OptNested: &pb2.Nested{
1500 OptString: scalar.String("inception"),
1501 },
1502 }
1503 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1504 if err != nil {
1505 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1506 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001507 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001508 TypeUrl: "foo/pb2.Nested",
1509 Value: b,
1510 }
1511 }(),
1512 want: `{
1513 "@type": "foo/pb2.Nested",
1514 "optString": "embedded inside Any",
1515 "optNested": {
1516 "optString": "inception"
1517 }
1518}`,
1519 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001520 desc: "Any with empty embedded message",
Damien Neil5c5b5312019-05-14 12:44:37 -07001521 mo: protojson.MarshalOptions{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001522 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001523 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001524 input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001525 want: `{
1526 "@type": "foo/pb2.Nested"
1527}`,
1528 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001529 desc: "Any without registered type",
Damien Neil5c5b5312019-05-14 12:44:37 -07001530 mo: protojson.MarshalOptions{Resolver: preg.NewTypes()},
Joe Tsaia95b29f2019-05-16 12:47:20 -07001531 input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001532 wantErr: true,
1533 }, {
Damien Neil0c9f0a92019-06-19 10:41:09 -07001534 desc: "Any with missing required",
Damien Neil5c5b5312019-05-14 12:44:37 -07001535 mo: protojson.MarshalOptions{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001536 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001537 },
1538 input: func() proto.Message {
1539 m := &pb2.PartialRequired{
1540 OptString: scalar.String("embedded inside Any"),
1541 }
Damien Neil96c229a2019-04-03 12:17:24 -07001542 b, err := proto.MarshalOptions{
1543 AllowPartial: true,
1544 Deterministic: true,
1545 }.Marshal(m)
Herbie Ong0b0f4032019-03-18 19:06:15 -07001546 if err != nil {
1547 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1548 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001549 return &anypb.Any{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001550 TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001551 Value: b,
1552 }
1553 }(),
1554 want: `{
1555 "@type": "pb2.PartialRequired",
1556 "optString": "embedded inside Any"
1557}`,
Herbie Ong0b0f4032019-03-18 19:06:15 -07001558 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001559 desc: "Any with partial required and AllowPartial",
Damien Neil5c5b5312019-05-14 12:44:37 -07001560 mo: protojson.MarshalOptions{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001561 AllowPartial: true,
Joe Tsai0fc49f82019-05-01 12:29:25 -07001562 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001563 },
1564 input: func() proto.Message {
1565 m := &pb2.PartialRequired{
1566 OptString: scalar.String("embedded inside Any"),
1567 }
Damien Neil96c229a2019-04-03 12:17:24 -07001568 b, err := proto.MarshalOptions{
1569 AllowPartial: true,
1570 Deterministic: true,
1571 }.Marshal(m)
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001572 if err != nil {
1573 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1574 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001575 return &anypb.Any{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001576 TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001577 Value: b,
1578 }
1579 }(),
1580 want: `{
1581 "@type": "pb2.PartialRequired",
1582 "optString": "embedded inside Any"
1583}`,
1584 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001585 desc: "Any with invalid UTF8",
Damien Neil5c5b5312019-05-14 12:44:37 -07001586 mo: protojson.MarshalOptions{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001587 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001588 },
1589 input: func() proto.Message {
1590 m := &pb2.Nested{
1591 OptString: scalar.String("abc\xff"),
1592 }
1593 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1594 if err != nil {
1595 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1596 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001597 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001598 TypeUrl: "foo/pb2.Nested",
1599 Value: b,
1600 }
1601 }(),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001602 wantErr: true,
1603 }, {
1604 desc: "Any with invalid value",
Damien Neil5c5b5312019-05-14 12:44:37 -07001605 mo: protojson.MarshalOptions{
Joe Tsai0fc49f82019-05-01 12:29:25 -07001606 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001607 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001608 input: &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001609 TypeUrl: "foo/pb2.Nested",
Joe Tsai6dc168e2019-07-09 23:11:13 -07001610 Value: []byte("\x80"),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001611 },
1612 wantErr: true,
1613 }, {
1614 desc: "Any with BoolValue",
Damien Neil5c5b5312019-05-14 12:44:37 -07001615 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001616 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001617 },
1618 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001619 m := &wrapperspb.BoolValue{Value: true}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001620 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1621 if err != nil {
1622 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1623 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001624 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001625 TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
1626 Value: b,
1627 }
1628 }(),
1629 want: `{
1630 "@type": "type.googleapis.com/google.protobuf.BoolValue",
1631 "value": true
1632}`,
1633 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001634 desc: "Any with Empty",
Damien Neil5c5b5312019-05-14 12:44:37 -07001635 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001636 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001637 },
1638 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001639 m := &emptypb.Empty{}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001640 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1641 if err != nil {
1642 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1643 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001644 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001645 TypeUrl: "type.googleapis.com/google.protobuf.Empty",
1646 Value: b,
1647 }
1648 }(),
1649 want: `{
Herbie Ong82014a52019-03-27 15:21:43 -07001650 "@type": "type.googleapis.com/google.protobuf.Empty",
1651 "value": {}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001652}`,
1653 }, {
1654 desc: "Any with StringValue containing invalid UTF8",
Damien Neil5c5b5312019-05-14 12:44:37 -07001655 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001656 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001657 },
1658 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001659 m := &wrapperspb.StringValue{Value: "abcd"}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001660 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1661 if err != nil {
1662 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1663 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001664 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001665 TypeUrl: "google.protobuf.StringValue",
Damien Neilbc310b52019-04-11 11:46:55 -07001666 Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001667 }
1668 }(),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001669 wantErr: true,
1670 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001671 desc: "Any with Int64Value",
Damien Neil5c5b5312019-05-14 12:44:37 -07001672 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001673 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001674 },
1675 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001676 m := &wrapperspb.Int64Value{Value: 42}
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001677 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1678 if err != nil {
1679 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1680 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001681 return &anypb.Any{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001682 TypeUrl: "google.protobuf.Int64Value",
1683 Value: b,
1684 }
1685 }(),
1686 want: `{
1687 "@type": "google.protobuf.Int64Value",
1688 "value": "42"
1689}`,
1690 }, {
1691 desc: "Any with Duration",
Damien Neil5c5b5312019-05-14 12:44:37 -07001692 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001693 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001694 },
1695 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001696 m := &durationpb.Duration{}
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001697 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1698 if err != nil {
1699 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1700 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001701 return &anypb.Any{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001702 TypeUrl: "type.googleapis.com/google.protobuf.Duration",
1703 Value: b,
1704 }
1705 }(),
1706 want: `{
1707 "@type": "type.googleapis.com/google.protobuf.Duration",
1708 "value": "0s"
1709}`,
1710 }, {
1711 desc: "Any with empty Value",
Damien Neil5c5b5312019-05-14 12:44:37 -07001712 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001713 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001714 },
1715 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001716 m := &structpb.Value{}
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001717 b, err := proto.Marshal(m)
1718 if err != nil {
1719 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1720 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001721 return &anypb.Any{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001722 TypeUrl: "type.googleapis.com/google.protobuf.Value",
1723 Value: b,
1724 }
1725 }(),
1726 wantErr: true,
1727 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001728 desc: "Any with Value of StringValue",
Damien Neil5c5b5312019-05-14 12:44:37 -07001729 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001730 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001731 },
1732 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001733 m := &structpb.Value{Kind: &structpb.Value_StringValue{"abcd"}}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001734 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1735 if err != nil {
1736 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1737 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001738 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001739 TypeUrl: "type.googleapis.com/google.protobuf.Value",
Damien Neilbc310b52019-04-11 11:46:55 -07001740 Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001741 }
1742 }(),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001743 wantErr: true,
1744 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001745 desc: "Any with Value of NullValue",
Damien Neil5c5b5312019-05-14 12:44:37 -07001746 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001747 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001748 },
1749 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001750 m := &structpb.Value{Kind: &structpb.Value_NullValue{}}
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001751 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
Herbie Ong0b0f4032019-03-18 19:06:15 -07001752 if err != nil {
1753 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1754 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001755 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001756 TypeUrl: "type.googleapis.com/google.protobuf.Value",
1757 Value: b,
1758 }
1759 }(),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001760 want: `{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001761 "@type": "type.googleapis.com/google.protobuf.Value",
1762 "value": null
Herbie Ong0b0f4032019-03-18 19:06:15 -07001763}`,
1764 }, {
1765 desc: "Any with Struct",
Damien Neil5c5b5312019-05-14 12:44:37 -07001766 mo: protojson.MarshalOptions{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001767 Resolver: preg.NewTypes(
Joe Tsaia95b29f2019-05-16 12:47:20 -07001768 pimpl.Export{}.MessageTypeOf(&structpb.Struct{}),
1769 pimpl.Export{}.MessageTypeOf(&structpb.Value{}),
1770 pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}),
1771 pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE),
1772 pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001773 ),
1774 },
1775 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001776 m := &structpb.Struct{
1777 Fields: map[string]*structpb.Value{
1778 "bool": {Kind: &structpb.Value_BoolValue{true}},
1779 "null": {Kind: &structpb.Value_NullValue{}},
1780 "string": {Kind: &structpb.Value_StringValue{"hello"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001781 "struct": {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001782 Kind: &structpb.Value_StructValue{
1783 &structpb.Struct{
1784 Fields: map[string]*structpb.Value{
1785 "string": {Kind: &structpb.Value_StringValue{"world"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001786 },
1787 },
1788 },
1789 },
1790 },
1791 }
1792 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1793 if err != nil {
1794 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1795 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001796 return &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001797 TypeUrl: "google.protobuf.Struct",
1798 Value: b,
1799 }
1800 }(),
1801 want: `{
1802 "@type": "google.protobuf.Struct",
1803 "value": {
1804 "bool": true,
1805 "null": null,
1806 "string": "hello",
1807 "struct": {
1808 "string": "world"
1809 }
1810 }
1811}`,
1812 }, {
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001813 desc: "Any with missing type_url",
Damien Neil5c5b5312019-05-14 12:44:37 -07001814 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001815 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001816 },
1817 input: func() proto.Message {
Joe Tsaia95b29f2019-05-16 12:47:20 -07001818 m := &wrapperspb.BoolValue{Value: true}
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001819 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
1820 if err != nil {
1821 t.Fatalf("error in binary marshaling message for Any.value: %v", err)
1822 }
Joe Tsaia95b29f2019-05-16 12:47:20 -07001823 return &anypb.Any{
Herbie Ong8ac9dd22019-03-27 12:20:50 -07001824 Value: b,
1825 }
1826 }(),
1827 wantErr: true,
1828 }, {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001829 desc: "well known types as field values",
Damien Neil5c5b5312019-05-14 12:44:37 -07001830 mo: protojson.MarshalOptions{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001831 Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
Herbie Ong0b0f4032019-03-18 19:06:15 -07001832 },
1833 input: &pb2.KnownTypes{
Joe Tsaia95b29f2019-05-16 12:47:20 -07001834 OptBool: &wrapperspb.BoolValue{Value: false},
1835 OptInt32: &wrapperspb.Int32Value{Value: 42},
1836 OptInt64: &wrapperspb.Int64Value{Value: 42},
1837 OptUint32: &wrapperspb.UInt32Value{Value: 42},
1838 OptUint64: &wrapperspb.UInt64Value{Value: 42},
1839 OptFloat: &wrapperspb.FloatValue{Value: 1.23},
1840 OptDouble: &wrapperspb.DoubleValue{Value: 3.1415},
1841 OptString: &wrapperspb.StringValue{Value: "hello"},
1842 OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")},
1843 OptDuration: &durationpb.Duration{Seconds: 123},
1844 OptTimestamp: &timestamppb.Timestamp{Seconds: 1553036601},
1845 OptStruct: &structpb.Struct{
1846 Fields: map[string]*structpb.Value{
1847 "string": {Kind: &structpb.Value_StringValue{"hello"}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001848 },
1849 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001850 OptList: &structpb.ListValue{
1851 Values: []*structpb.Value{
1852 {Kind: &structpb.Value_NullValue{}},
1853 {Kind: &structpb.Value_StringValue{}},
1854 {Kind: &structpb.Value_StructValue{}},
1855 {Kind: &structpb.Value_ListValue{}},
Herbie Ong0b0f4032019-03-18 19:06:15 -07001856 },
1857 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001858 OptValue: &structpb.Value{
1859 Kind: &structpb.Value_StringValue{"world"},
Herbie Ong1c7462c2019-03-22 17:56:55 -07001860 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001861 OptEmpty: &emptypb.Empty{},
1862 OptAny: &anypb.Any{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001863 TypeUrl: "google.protobuf.Empty",
1864 },
Joe Tsaia95b29f2019-05-16 12:47:20 -07001865 OptFieldmask: &fieldmaskpb.FieldMask{
Herbie Ong0b0f4032019-03-18 19:06:15 -07001866 Paths: []string{"foo_bar", "bar_foo"},
1867 },
1868 },
1869 want: `{
1870 "optBool": false,
1871 "optInt32": 42,
1872 "optInt64": "42",
1873 "optUint32": 42,
1874 "optUint64": "42",
1875 "optFloat": 1.23,
1876 "optDouble": 3.1415,
1877 "optString": "hello",
1878 "optBytes": "aGVsbG8=",
1879 "optDuration": "123s",
1880 "optTimestamp": "2019-03-19T23:03:21Z",
1881 "optStruct": {
1882 "string": "hello"
1883 },
1884 "optList": [
1885 null,
1886 "",
1887 {},
1888 []
1889 ],
Herbie Ong1c7462c2019-03-22 17:56:55 -07001890 "optValue": "world",
Herbie Ong0b0f4032019-03-18 19:06:15 -07001891 "optEmpty": {},
1892 "optAny": {
Herbie Ong82014a52019-03-27 15:21:43 -07001893 "@type": "google.protobuf.Empty",
1894 "value": {}
Herbie Ong0b0f4032019-03-18 19:06:15 -07001895 },
1896 "optFieldmask": "fooBar,barFoo"
1897}`,
Herbie Ong7b828bc2019-02-08 19:56:24 -08001898 }}
1899
1900 for _, tt := range tests {
1901 tt := tt
1902 t.Run(tt.desc, func(t *testing.T) {
Herbie Ong0b0f4032019-03-18 19:06:15 -07001903 // Use 2-space indentation on all MarshalOptions.
1904 tt.mo.Indent = " "
Herbie Ong7b828bc2019-02-08 19:56:24 -08001905 b, err := tt.mo.Marshal(tt.input)
Herbie Ong0b0f4032019-03-18 19:06:15 -07001906 if err != nil && !tt.wantErr {
Herbie Ong7b828bc2019-02-08 19:56:24 -08001907 t.Errorf("Marshal() returned error: %v\n", err)
1908 }
Herbie Ong0b0f4032019-03-18 19:06:15 -07001909 if err == nil && tt.wantErr {
1910 t.Errorf("Marshal() got nil error, want error\n")
1911 }
Herbie Ong7b828bc2019-02-08 19:56:24 -08001912 got := string(b)
1913 if got != tt.want {
1914 t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
Joe Tsai6dc168e2019-07-09 23:11:13 -07001915 if diff := cmp.Diff(tt.want, got); diff != "" {
Herbie Ong7b828bc2019-02-08 19:56:24 -08001916 t.Errorf("Marshal() diff -want +got\n%v\n", diff)
1917 }
1918 }
1919 })
1920 }
1921}