Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | package jsonpb_test |
| 6 | |
| 7 | import ( |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 8 | "encoding/hex" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 9 | "math" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/golang/protobuf/v2/encoding/jsonpb" |
| 14 | "github.com/golang/protobuf/v2/internal/encoding/pack" |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 15 | "github.com/golang/protobuf/v2/internal/encoding/wire" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 16 | "github.com/golang/protobuf/v2/internal/scalar" |
| 17 | "github.com/golang/protobuf/v2/proto" |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 18 | preg "github.com/golang/protobuf/v2/reflect/protoregistry" |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 19 | "github.com/golang/protobuf/v2/runtime/protoiface" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 20 | "github.com/google/go-cmp/cmp" |
| 21 | "github.com/google/go-cmp/cmp/cmpopts" |
| 22 | |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 23 | // This legacy package is still needed when importing legacy message. |
| 24 | _ "github.com/golang/protobuf/v2/internal/legacy" |
| 25 | |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 26 | "github.com/golang/protobuf/v2/encoding/testprotos/pb2" |
| 27 | "github.com/golang/protobuf/v2/encoding/testprotos/pb3" |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 28 | knownpb "github.com/golang/protobuf/v2/types/known" |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | // splitLines is a cmpopts.Option for comparing strings with line breaks. |
| 32 | var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string { |
| 33 | return strings.Split(s, "\n") |
| 34 | }) |
| 35 | |
| 36 | func pb2Enum(i int32) *pb2.Enum { |
| 37 | p := new(pb2.Enum) |
| 38 | *p = pb2.Enum(i) |
| 39 | return p |
| 40 | } |
| 41 | |
| 42 | func 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 Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 48 | func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) { |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 49 | 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 Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 59 | // dhex decodes a hex-string and returns the bytes and panics if s is invalid. |
| 60 | func dhex(s string) []byte { |
| 61 | b, err := hex.DecodeString(s) |
| 62 | if err != nil { |
| 63 | panic(err) |
| 64 | } |
| 65 | return b |
| 66 | } |
| 67 | |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 68 | func TestMarshal(t *testing.T) { |
| 69 | tests := []struct { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 70 | desc string |
| 71 | mo jsonpb.MarshalOptions |
| 72 | input proto.Message |
| 73 | want string |
| 74 | wantErr bool // TODO: Verify error message substring. |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 75 | }{{ |
| 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 Ong | 87608a7 | 2019-03-06 14:32:24 -0800 | [diff] [blame] | 133 | OptBytes: []byte("谷歌"), |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 134 | 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 Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 152 | 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 Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 167 | 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 | }, { |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 730 | desc: "required fields not set", |
| 731 | input: &pb2.Requireds{}, |
| 732 | want: `{}`, |
| 733 | wantErr: true, |
| 734 | }, { |
| 735 | desc: "required fields partially set", |
| 736 | input: &pb2.Requireds{ |
| 737 | ReqBool: scalar.Bool(false), |
| 738 | ReqSfixed64: scalar.Int64(0), |
| 739 | ReqDouble: scalar.Float64(1.23), |
| 740 | ReqString: scalar.String("hello"), |
| 741 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 742 | }, |
| 743 | want: `{ |
| 744 | "reqBool": false, |
| 745 | "reqSfixed64": "0", |
| 746 | "reqDouble": 1.23, |
| 747 | "reqString": "hello", |
| 748 | "reqEnum": "ONE" |
| 749 | }`, |
| 750 | wantErr: true, |
| 751 | }, { |
| 752 | desc: "required fields not set with AllowPartial", |
| 753 | mo: jsonpb.MarshalOptions{AllowPartial: true}, |
| 754 | input: &pb2.Requireds{ |
| 755 | ReqBool: scalar.Bool(false), |
| 756 | ReqSfixed64: scalar.Int64(0), |
| 757 | ReqDouble: scalar.Float64(1.23), |
| 758 | ReqString: scalar.String("hello"), |
| 759 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 760 | }, |
| 761 | want: `{ |
| 762 | "reqBool": false, |
| 763 | "reqSfixed64": "0", |
| 764 | "reqDouble": 1.23, |
| 765 | "reqString": "hello", |
| 766 | "reqEnum": "ONE" |
| 767 | }`, |
| 768 | }, { |
| 769 | desc: "required fields all set", |
| 770 | input: &pb2.Requireds{ |
| 771 | ReqBool: scalar.Bool(false), |
| 772 | ReqSfixed64: scalar.Int64(0), |
| 773 | ReqDouble: scalar.Float64(1.23), |
| 774 | ReqString: scalar.String("hello"), |
| 775 | ReqEnum: pb2.Enum_ONE.Enum(), |
| 776 | ReqNested: &pb2.Nested{}, |
| 777 | }, |
| 778 | want: `{ |
| 779 | "reqBool": false, |
| 780 | "reqSfixed64": "0", |
| 781 | "reqDouble": 1.23, |
| 782 | "reqString": "hello", |
| 783 | "reqEnum": "ONE", |
| 784 | "reqNested": {} |
| 785 | }`, |
| 786 | }, { |
| 787 | desc: "indirect required field", |
| 788 | input: &pb2.IndirectRequired{ |
| 789 | OptNested: &pb2.NestedWithRequired{}, |
| 790 | }, |
| 791 | want: `{ |
| 792 | "optNested": {} |
| 793 | }`, |
| 794 | wantErr: true, |
| 795 | }, { |
| 796 | desc: "indirect required field with AllowPartial", |
| 797 | mo: jsonpb.MarshalOptions{AllowPartial: true}, |
| 798 | input: &pb2.IndirectRequired{ |
| 799 | OptNested: &pb2.NestedWithRequired{}, |
| 800 | }, |
| 801 | want: `{ |
| 802 | "optNested": {} |
| 803 | }`, |
| 804 | }, { |
| 805 | desc: "indirect required field in empty repeated", |
| 806 | input: &pb2.IndirectRequired{ |
| 807 | RptNested: []*pb2.NestedWithRequired{}, |
| 808 | }, |
| 809 | want: `{}`, |
| 810 | }, { |
| 811 | desc: "indirect required field in repeated", |
| 812 | input: &pb2.IndirectRequired{ |
| 813 | RptNested: []*pb2.NestedWithRequired{ |
| 814 | &pb2.NestedWithRequired{}, |
| 815 | }, |
| 816 | }, |
| 817 | want: `{ |
| 818 | "rptNested": [ |
| 819 | {} |
| 820 | ] |
| 821 | }`, |
| 822 | wantErr: true, |
| 823 | }, { |
| 824 | desc: "indirect required field in repeated with AllowPartial", |
| 825 | mo: jsonpb.MarshalOptions{AllowPartial: true}, |
| 826 | input: &pb2.IndirectRequired{ |
| 827 | RptNested: []*pb2.NestedWithRequired{ |
| 828 | &pb2.NestedWithRequired{}, |
| 829 | }, |
| 830 | }, |
| 831 | want: `{ |
| 832 | "rptNested": [ |
| 833 | {} |
| 834 | ] |
| 835 | }`, |
| 836 | }, { |
| 837 | desc: "indirect required field in empty map", |
| 838 | input: &pb2.IndirectRequired{ |
| 839 | StrToNested: map[string]*pb2.NestedWithRequired{}, |
| 840 | }, |
| 841 | want: "{}", |
| 842 | }, { |
| 843 | desc: "indirect required field in map", |
| 844 | input: &pb2.IndirectRequired{ |
| 845 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 846 | "fail": &pb2.NestedWithRequired{}, |
| 847 | }, |
| 848 | }, |
| 849 | want: `{ |
| 850 | "strToNested": { |
| 851 | "fail": {} |
| 852 | } |
| 853 | }`, |
| 854 | wantErr: true, |
| 855 | }, { |
| 856 | desc: "indirect required field in map with AllowPartial", |
| 857 | mo: jsonpb.MarshalOptions{AllowPartial: true}, |
| 858 | input: &pb2.IndirectRequired{ |
| 859 | StrToNested: map[string]*pb2.NestedWithRequired{ |
| 860 | "fail": &pb2.NestedWithRequired{}, |
| 861 | }, |
| 862 | }, |
| 863 | want: `{ |
| 864 | "strToNested": { |
| 865 | "fail": {} |
| 866 | } |
| 867 | }`, |
| 868 | }, { |
| 869 | desc: "indirect required field in oneof", |
| 870 | input: &pb2.IndirectRequired{ |
| 871 | Union: &pb2.IndirectRequired_OneofNested{ |
| 872 | OneofNested: &pb2.NestedWithRequired{}, |
| 873 | }, |
| 874 | }, |
| 875 | want: `{ |
| 876 | "oneofNested": {} |
| 877 | }`, |
| 878 | wantErr: true, |
| 879 | }, { |
| 880 | desc: "indirect required field in oneof with AllowPartial", |
| 881 | mo: jsonpb.MarshalOptions{AllowPartial: true}, |
| 882 | input: &pb2.IndirectRequired{ |
| 883 | Union: &pb2.IndirectRequired_OneofNested{ |
| 884 | OneofNested: &pb2.NestedWithRequired{}, |
| 885 | }, |
| 886 | }, |
| 887 | want: `{ |
| 888 | "oneofNested": {} |
| 889 | }`, |
| 890 | }, { |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 891 | desc: "unknown fields are ignored", |
| 892 | input: &pb2.Scalars{ |
| 893 | OptString: scalar.String("no unknowns"), |
| 894 | XXX_unrecognized: pack.Message{ |
| 895 | pack.Tag{101, pack.BytesType}, pack.String("hello world"), |
| 896 | }.Marshal(), |
| 897 | }, |
| 898 | want: `{ |
| 899 | "optString": "no unknowns" |
| 900 | }`, |
| 901 | }, { |
| 902 | desc: "json_name", |
| 903 | input: &pb3.JSONNames{ |
| 904 | SString: "json_name", |
| 905 | }, |
| 906 | want: `{ |
| 907 | "foo_bar": "json_name" |
| 908 | }`, |
Herbie Ong | f83d5bb | 2019-03-14 00:01:27 -0700 | [diff] [blame] | 909 | }, { |
| 910 | desc: "extensions of non-repeated fields", |
| 911 | input: func() proto.Message { |
| 912 | m := &pb2.Extensions{ |
| 913 | OptString: scalar.String("non-extension field"), |
| 914 | OptBool: scalar.Bool(true), |
| 915 | OptInt32: scalar.Int32(42), |
| 916 | } |
| 917 | setExtension(m, pb2.E_OptExtBool, true) |
| 918 | setExtension(m, pb2.E_OptExtString, "extension field") |
| 919 | setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN) |
| 920 | setExtension(m, pb2.E_OptExtNested, &pb2.Nested{ |
| 921 | OptString: scalar.String("nested in an extension"), |
| 922 | OptNested: &pb2.Nested{ |
| 923 | OptString: scalar.String("another nested in an extension"), |
| 924 | }, |
| 925 | }) |
| 926 | return m |
| 927 | }(), |
| 928 | want: `{ |
| 929 | "optString": "non-extension field", |
| 930 | "optBool": true, |
| 931 | "optInt32": 42, |
| 932 | "[pb2.opt_ext_bool]": true, |
| 933 | "[pb2.opt_ext_enum]": "TEN", |
| 934 | "[pb2.opt_ext_nested]": { |
| 935 | "optString": "nested in an extension", |
| 936 | "optNested": { |
| 937 | "optString": "another nested in an extension" |
| 938 | } |
| 939 | }, |
| 940 | "[pb2.opt_ext_string]": "extension field" |
| 941 | }`, |
| 942 | }, { |
| 943 | desc: "extension message field set to nil", |
| 944 | input: func() proto.Message { |
| 945 | m := &pb2.Extensions{} |
| 946 | setExtension(m, pb2.E_OptExtNested, nil) |
| 947 | return m |
| 948 | }(), |
| 949 | want: "{}", |
| 950 | }, { |
| 951 | desc: "extensions of repeated fields", |
| 952 | input: func() proto.Message { |
| 953 | m := &pb2.Extensions{} |
| 954 | setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
| 955 | setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47}) |
| 956 | setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{ |
| 957 | &pb2.Nested{OptString: scalar.String("one")}, |
| 958 | &pb2.Nested{OptString: scalar.String("two")}, |
| 959 | &pb2.Nested{OptString: scalar.String("three")}, |
| 960 | }) |
| 961 | return m |
| 962 | }(), |
| 963 | want: `{ |
| 964 | "[pb2.rpt_ext_enum]": [ |
| 965 | "TEN", |
| 966 | 101, |
| 967 | "ONE" |
| 968 | ], |
| 969 | "[pb2.rpt_ext_fixed32]": [ |
| 970 | 42, |
| 971 | 47 |
| 972 | ], |
| 973 | "[pb2.rpt_ext_nested]": [ |
| 974 | { |
| 975 | "optString": "one" |
| 976 | }, |
| 977 | { |
| 978 | "optString": "two" |
| 979 | }, |
| 980 | { |
| 981 | "optString": "three" |
| 982 | } |
| 983 | ] |
| 984 | }`, |
| 985 | }, { |
| 986 | desc: "extensions of non-repeated fields in another message", |
| 987 | input: func() proto.Message { |
| 988 | m := &pb2.Extensions{} |
| 989 | setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true) |
| 990 | setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field") |
| 991 | setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN) |
| 992 | setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{ |
| 993 | OptString: scalar.String("nested in an extension"), |
| 994 | OptNested: &pb2.Nested{ |
| 995 | OptString: scalar.String("another nested in an extension"), |
| 996 | }, |
| 997 | }) |
| 998 | return m |
| 999 | }(), |
| 1000 | want: `{ |
| 1001 | "[pb2.ExtensionsContainer.opt_ext_bool]": true, |
| 1002 | "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN", |
| 1003 | "[pb2.ExtensionsContainer.opt_ext_nested]": { |
| 1004 | "optString": "nested in an extension", |
| 1005 | "optNested": { |
| 1006 | "optString": "another nested in an extension" |
| 1007 | } |
| 1008 | }, |
| 1009 | "[pb2.ExtensionsContainer.opt_ext_string]": "extension field" |
| 1010 | }`, |
| 1011 | }, { |
| 1012 | desc: "extensions of repeated fields in another message", |
| 1013 | input: func() proto.Message { |
| 1014 | m := &pb2.Extensions{ |
| 1015 | OptString: scalar.String("non-extension field"), |
| 1016 | OptBool: scalar.Bool(true), |
| 1017 | OptInt32: scalar.Int32(42), |
| 1018 | } |
| 1019 | setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE}) |
| 1020 | setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"}) |
| 1021 | setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{ |
| 1022 | &pb2.Nested{OptString: scalar.String("one")}, |
| 1023 | &pb2.Nested{OptString: scalar.String("two")}, |
| 1024 | &pb2.Nested{OptString: scalar.String("three")}, |
| 1025 | }) |
| 1026 | return m |
| 1027 | }(), |
| 1028 | want: `{ |
| 1029 | "optString": "non-extension field", |
| 1030 | "optBool": true, |
| 1031 | "optInt32": 42, |
| 1032 | "[pb2.ExtensionsContainer.rpt_ext_enum]": [ |
| 1033 | "TEN", |
| 1034 | 101, |
| 1035 | "ONE" |
| 1036 | ], |
| 1037 | "[pb2.ExtensionsContainer.rpt_ext_nested]": [ |
| 1038 | { |
| 1039 | "optString": "one" |
| 1040 | }, |
| 1041 | { |
| 1042 | "optString": "two" |
| 1043 | }, |
| 1044 | { |
| 1045 | "optString": "three" |
| 1046 | } |
| 1047 | ], |
| 1048 | "[pb2.ExtensionsContainer.rpt_ext_string]": [ |
| 1049 | "hello", |
| 1050 | "world" |
| 1051 | ] |
| 1052 | }`, |
| 1053 | }, { |
| 1054 | desc: "MessageSet", |
| 1055 | input: func() proto.Message { |
| 1056 | m := &pb2.MessageSet{} |
| 1057 | setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{ |
| 1058 | OptString: scalar.String("a messageset extension"), |
| 1059 | }) |
| 1060 | setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{ |
| 1061 | OptString: scalar.String("not a messageset extension"), |
| 1062 | }) |
| 1063 | setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{ |
| 1064 | OptString: scalar.String("just a regular extension"), |
| 1065 | }) |
| 1066 | return m |
| 1067 | }(), |
| 1068 | want: `{ |
| 1069 | "[pb2.MessageSetExtension]": { |
| 1070 | "optString": "a messageset extension" |
| 1071 | }, |
| 1072 | "[pb2.MessageSetExtension.ext_nested]": { |
| 1073 | "optString": "just a regular extension" |
| 1074 | }, |
| 1075 | "[pb2.MessageSetExtension.not_message_set_extension]": { |
| 1076 | "optString": "not a messageset extension" |
| 1077 | } |
| 1078 | }`, |
| 1079 | }, { |
| 1080 | desc: "not real MessageSet 1", |
| 1081 | input: func() proto.Message { |
| 1082 | m := &pb2.FakeMessageSet{} |
| 1083 | setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
| 1084 | OptString: scalar.String("not a messageset extension"), |
| 1085 | }) |
| 1086 | return m |
| 1087 | }(), |
| 1088 | want: `{ |
| 1089 | "[pb2.FakeMessageSetExtension.message_set_extension]": { |
| 1090 | "optString": "not a messageset extension" |
| 1091 | } |
| 1092 | }`, |
| 1093 | }, { |
| 1094 | desc: "not real MessageSet 2", |
| 1095 | input: func() proto.Message { |
| 1096 | m := &pb2.MessageSet{} |
| 1097 | setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{ |
| 1098 | OptString: scalar.String("another not a messageset extension"), |
| 1099 | }) |
| 1100 | return m |
| 1101 | }(), |
| 1102 | want: `{ |
| 1103 | "[pb2.message_set_extension]": { |
| 1104 | "optString": "another not a messageset extension" |
| 1105 | } |
| 1106 | }`, |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1107 | }, { |
| 1108 | desc: "BoolValue empty", |
| 1109 | input: &knownpb.BoolValue{}, |
| 1110 | want: `false`, |
| 1111 | }, { |
| 1112 | desc: "BoolValue", |
| 1113 | input: &knownpb.BoolValue{Value: true}, |
| 1114 | want: `true`, |
| 1115 | }, { |
| 1116 | desc: "Int32Value empty", |
| 1117 | input: &knownpb.Int32Value{}, |
| 1118 | want: `0`, |
| 1119 | }, { |
| 1120 | desc: "Int32Value", |
| 1121 | input: &knownpb.Int32Value{Value: 42}, |
| 1122 | want: `42`, |
| 1123 | }, { |
| 1124 | desc: "Int64Value", |
| 1125 | input: &knownpb.Int64Value{Value: 42}, |
| 1126 | want: `"42"`, |
| 1127 | }, { |
| 1128 | desc: "UInt32Value", |
| 1129 | input: &knownpb.UInt32Value{Value: 42}, |
| 1130 | want: `42`, |
| 1131 | }, { |
| 1132 | desc: "UInt64Value", |
| 1133 | input: &knownpb.UInt64Value{Value: 42}, |
| 1134 | want: `"42"`, |
| 1135 | }, { |
| 1136 | desc: "FloatValue", |
| 1137 | input: &knownpb.FloatValue{Value: 1.02}, |
| 1138 | want: `1.02`, |
| 1139 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1140 | desc: "FloatValue Infinity", |
| 1141 | input: &knownpb.FloatValue{Value: float32(math.Inf(-1))}, |
| 1142 | want: `"-Infinity"`, |
| 1143 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1144 | desc: "DoubleValue", |
| 1145 | input: &knownpb.DoubleValue{Value: 1.02}, |
| 1146 | want: `1.02`, |
| 1147 | }, { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 1148 | desc: "DoubleValue NaN", |
| 1149 | input: &knownpb.DoubleValue{Value: math.NaN()}, |
| 1150 | want: `"NaN"`, |
| 1151 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1152 | desc: "StringValue empty", |
| 1153 | input: &knownpb.StringValue{}, |
| 1154 | want: `""`, |
| 1155 | }, { |
| 1156 | desc: "StringValue", |
| 1157 | input: &knownpb.StringValue{Value: "谷歌"}, |
| 1158 | want: `"谷歌"`, |
| 1159 | }, { |
| 1160 | desc: "StringValue with invalid UTF8 error", |
| 1161 | input: &knownpb.StringValue{Value: "abc\xff"}, |
| 1162 | want: "\"abc\xff\"", |
| 1163 | wantErr: true, |
| 1164 | }, { |
| 1165 | desc: "StringValue field with invalid UTF8 error", |
| 1166 | input: &pb2.KnownTypes{ |
| 1167 | OptString: &knownpb.StringValue{Value: "abc\xff"}, |
| 1168 | }, |
| 1169 | want: "{\n \"optString\": \"abc\xff\"\n}", |
| 1170 | wantErr: true, |
| 1171 | }, { |
| 1172 | desc: "BytesValue", |
| 1173 | input: &knownpb.BytesValue{Value: []byte("hello")}, |
| 1174 | want: `"aGVsbG8="`, |
| 1175 | }, { |
| 1176 | desc: "Empty", |
| 1177 | input: &knownpb.Empty{}, |
| 1178 | want: `{}`, |
| 1179 | }, { |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame^] | 1180 | desc: "NullValue field", |
| 1181 | input: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)}, |
| 1182 | want: `{ |
| 1183 | "optNull": null |
| 1184 | }`, |
| 1185 | }, { |
Herbie Ong | 1c7462c | 2019-03-22 17:56:55 -0700 | [diff] [blame] | 1186 | desc: "Value empty", |
| 1187 | input: &knownpb.Value{}, |
| 1188 | wantErr: true, |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1189 | }, { |
| 1190 | desc: "Value empty field", |
| 1191 | input: &pb2.KnownTypes{ |
| 1192 | OptValue: &knownpb.Value{}, |
| 1193 | }, |
Herbie Ong | 1c7462c | 2019-03-22 17:56:55 -0700 | [diff] [blame] | 1194 | wantErr: true, |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1195 | }, { |
| 1196 | desc: "Value contains NullValue", |
| 1197 | input: &knownpb.Value{Kind: &knownpb.Value_NullValue{}}, |
| 1198 | want: `null`, |
| 1199 | }, { |
| 1200 | desc: "Value contains BoolValue", |
| 1201 | input: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}}, |
| 1202 | want: `false`, |
| 1203 | }, { |
| 1204 | desc: "Value contains NumberValue", |
| 1205 | input: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}}, |
| 1206 | want: `1.02`, |
| 1207 | }, { |
| 1208 | desc: "Value contains StringValue", |
| 1209 | input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}}, |
| 1210 | want: `"hello"`, |
| 1211 | }, { |
| 1212 | desc: "Value contains StringValue with invalid UTF8", |
| 1213 | input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}}, |
| 1214 | want: "\"\xff\"", |
| 1215 | wantErr: true, |
| 1216 | }, { |
| 1217 | desc: "Value contains Struct", |
| 1218 | input: &knownpb.Value{ |
| 1219 | Kind: &knownpb.Value_StructValue{ |
| 1220 | &knownpb.Struct{ |
| 1221 | Fields: map[string]*knownpb.Value{ |
| 1222 | "null": {Kind: &knownpb.Value_NullValue{}}, |
| 1223 | "number": {Kind: &knownpb.Value_NumberValue{}}, |
| 1224 | "string": {Kind: &knownpb.Value_StringValue{}}, |
| 1225 | "struct": {Kind: &knownpb.Value_StructValue{}}, |
| 1226 | "list": {Kind: &knownpb.Value_ListValue{}}, |
| 1227 | "bool": {Kind: &knownpb.Value_BoolValue{}}, |
| 1228 | }, |
| 1229 | }, |
| 1230 | }, |
| 1231 | }, |
| 1232 | want: `{ |
| 1233 | "bool": false, |
| 1234 | "list": [], |
| 1235 | "null": null, |
| 1236 | "number": 0, |
| 1237 | "string": "", |
| 1238 | "struct": {} |
| 1239 | }`, |
| 1240 | }, { |
| 1241 | desc: "Value contains ListValue", |
| 1242 | input: &knownpb.Value{ |
| 1243 | Kind: &knownpb.Value_ListValue{ |
| 1244 | &knownpb.ListValue{ |
| 1245 | Values: []*knownpb.Value{ |
| 1246 | {Kind: &knownpb.Value_BoolValue{}}, |
| 1247 | {Kind: &knownpb.Value_NullValue{}}, |
| 1248 | {Kind: &knownpb.Value_NumberValue{}}, |
| 1249 | {Kind: &knownpb.Value_StringValue{}}, |
| 1250 | {Kind: &knownpb.Value_StructValue{}}, |
| 1251 | {Kind: &knownpb.Value_ListValue{}}, |
| 1252 | }, |
| 1253 | }, |
| 1254 | }, |
| 1255 | }, |
| 1256 | want: `[ |
| 1257 | false, |
| 1258 | null, |
| 1259 | 0, |
| 1260 | "", |
| 1261 | {}, |
| 1262 | [] |
| 1263 | ]`, |
| 1264 | }, { |
| 1265 | desc: "Struct with nil map", |
| 1266 | input: &knownpb.Struct{}, |
| 1267 | want: `{}`, |
| 1268 | }, { |
| 1269 | desc: "Struct with empty map", |
| 1270 | input: &knownpb.Struct{ |
| 1271 | Fields: map[string]*knownpb.Value{}, |
| 1272 | }, |
| 1273 | want: `{}`, |
| 1274 | }, { |
| 1275 | desc: "Struct", |
| 1276 | input: &knownpb.Struct{ |
| 1277 | Fields: map[string]*knownpb.Value{ |
| 1278 | "bool": {Kind: &knownpb.Value_BoolValue{true}}, |
| 1279 | "null": {Kind: &knownpb.Value_NullValue{}}, |
| 1280 | "number": {Kind: &knownpb.Value_NumberValue{3.1415}}, |
| 1281 | "string": {Kind: &knownpb.Value_StringValue{"hello"}}, |
| 1282 | "struct": { |
| 1283 | Kind: &knownpb.Value_StructValue{ |
| 1284 | &knownpb.Struct{ |
| 1285 | Fields: map[string]*knownpb.Value{ |
| 1286 | "string": {Kind: &knownpb.Value_StringValue{"world"}}, |
| 1287 | }, |
| 1288 | }, |
| 1289 | }, |
| 1290 | }, |
| 1291 | "list": { |
| 1292 | Kind: &knownpb.Value_ListValue{ |
| 1293 | &knownpb.ListValue{ |
| 1294 | Values: []*knownpb.Value{ |
| 1295 | {Kind: &knownpb.Value_BoolValue{}}, |
| 1296 | {Kind: &knownpb.Value_NullValue{}}, |
| 1297 | {Kind: &knownpb.Value_NumberValue{}}, |
| 1298 | }, |
| 1299 | }, |
| 1300 | }, |
| 1301 | }, |
| 1302 | }, |
| 1303 | }, |
| 1304 | want: `{ |
| 1305 | "bool": true, |
| 1306 | "list": [ |
| 1307 | false, |
| 1308 | null, |
| 1309 | 0 |
| 1310 | ], |
| 1311 | "null": null, |
| 1312 | "number": 3.1415, |
| 1313 | "string": "hello", |
| 1314 | "struct": { |
| 1315 | "string": "world" |
| 1316 | } |
| 1317 | }`, |
| 1318 | }, { |
| 1319 | desc: "Struct message with invalid UTF8 string", |
| 1320 | input: &knownpb.Struct{ |
| 1321 | Fields: map[string]*knownpb.Value{ |
| 1322 | "string": {Kind: &knownpb.Value_StringValue{"\xff"}}, |
| 1323 | }, |
| 1324 | }, |
| 1325 | want: "{\n \"string\": \"\xff\"\n}", |
| 1326 | wantErr: true, |
| 1327 | }, { |
| 1328 | desc: "ListValue with nil values", |
| 1329 | input: &knownpb.ListValue{}, |
| 1330 | want: `[]`, |
| 1331 | }, { |
| 1332 | desc: "ListValue with empty values", |
| 1333 | input: &knownpb.ListValue{ |
| 1334 | Values: []*knownpb.Value{}, |
| 1335 | }, |
| 1336 | want: `[]`, |
| 1337 | }, { |
| 1338 | desc: "ListValue", |
| 1339 | input: &knownpb.ListValue{ |
| 1340 | Values: []*knownpb.Value{ |
| 1341 | {Kind: &knownpb.Value_BoolValue{true}}, |
| 1342 | {Kind: &knownpb.Value_NullValue{}}, |
| 1343 | {Kind: &knownpb.Value_NumberValue{3.1415}}, |
| 1344 | {Kind: &knownpb.Value_StringValue{"hello"}}, |
| 1345 | { |
| 1346 | Kind: &knownpb.Value_ListValue{ |
| 1347 | &knownpb.ListValue{ |
| 1348 | Values: []*knownpb.Value{ |
| 1349 | {Kind: &knownpb.Value_BoolValue{}}, |
| 1350 | {Kind: &knownpb.Value_NullValue{}}, |
| 1351 | {Kind: &knownpb.Value_NumberValue{}}, |
| 1352 | }, |
| 1353 | }, |
| 1354 | }, |
| 1355 | }, |
| 1356 | { |
| 1357 | Kind: &knownpb.Value_StructValue{ |
| 1358 | &knownpb.Struct{ |
| 1359 | Fields: map[string]*knownpb.Value{ |
| 1360 | "string": {Kind: &knownpb.Value_StringValue{"world"}}, |
| 1361 | }, |
| 1362 | }, |
| 1363 | }, |
| 1364 | }, |
| 1365 | }, |
| 1366 | }, |
| 1367 | want: `[ |
| 1368 | true, |
| 1369 | null, |
| 1370 | 3.1415, |
| 1371 | "hello", |
| 1372 | [ |
| 1373 | false, |
| 1374 | null, |
| 1375 | 0 |
| 1376 | ], |
| 1377 | { |
| 1378 | "string": "world" |
| 1379 | } |
| 1380 | ]`, |
| 1381 | }, { |
| 1382 | desc: "ListValue with invalid UTF8 string", |
| 1383 | input: &knownpb.ListValue{ |
| 1384 | Values: []*knownpb.Value{ |
| 1385 | {Kind: &knownpb.Value_StringValue{"\xff"}}, |
| 1386 | }, |
| 1387 | }, |
| 1388 | want: "[\n \"\xff\"\n]", |
| 1389 | wantErr: true, |
| 1390 | }, { |
| 1391 | desc: "Duration empty", |
| 1392 | input: &knownpb.Duration{}, |
| 1393 | want: `"0s"`, |
| 1394 | }, { |
| 1395 | desc: "Duration with secs", |
| 1396 | input: &knownpb.Duration{Seconds: 3}, |
| 1397 | want: `"3s"`, |
| 1398 | }, { |
| 1399 | desc: "Duration with -secs", |
| 1400 | input: &knownpb.Duration{Seconds: -3}, |
| 1401 | want: `"-3s"`, |
| 1402 | }, { |
| 1403 | desc: "Duration with nanos", |
| 1404 | input: &knownpb.Duration{Nanos: 1e6}, |
| 1405 | want: `"0.001s"`, |
| 1406 | }, { |
| 1407 | desc: "Duration with -nanos", |
| 1408 | input: &knownpb.Duration{Nanos: -1e6}, |
| 1409 | want: `"-0.001s"`, |
| 1410 | }, { |
| 1411 | desc: "Duration with large secs", |
| 1412 | input: &knownpb.Duration{Seconds: 1e10, Nanos: 1}, |
| 1413 | want: `"10000000000.000000001s"`, |
| 1414 | }, { |
| 1415 | desc: "Duration with 6-digit nanos", |
| 1416 | input: &knownpb.Duration{Nanos: 1e4}, |
| 1417 | want: `"0.000010s"`, |
| 1418 | }, { |
| 1419 | desc: "Duration with 3-digit nanos", |
| 1420 | input: &knownpb.Duration{Nanos: 1e6}, |
| 1421 | want: `"0.001s"`, |
| 1422 | }, { |
| 1423 | desc: "Duration with -secs -nanos", |
| 1424 | input: &knownpb.Duration{Seconds: -123, Nanos: -450}, |
| 1425 | want: `"-123.000000450s"`, |
| 1426 | }, { |
| 1427 | desc: "Duration with +secs -nanos", |
| 1428 | input: &knownpb.Duration{Seconds: 1, Nanos: -1}, |
| 1429 | wantErr: true, |
| 1430 | }, { |
| 1431 | desc: "Duration with -secs +nanos", |
| 1432 | input: &knownpb.Duration{Seconds: -1, Nanos: 1}, |
| 1433 | wantErr: true, |
| 1434 | }, { |
| 1435 | desc: "Duration with +secs out of range", |
| 1436 | input: &knownpb.Duration{Seconds: 315576000001}, |
| 1437 | wantErr: true, |
| 1438 | }, { |
| 1439 | desc: "Duration with -secs out of range", |
| 1440 | input: &knownpb.Duration{Seconds: -315576000001}, |
| 1441 | wantErr: true, |
| 1442 | }, { |
| 1443 | desc: "Duration with +nanos out of range", |
| 1444 | input: &knownpb.Duration{Seconds: 0, Nanos: 1e9}, |
| 1445 | wantErr: true, |
| 1446 | }, { |
| 1447 | desc: "Duration with -nanos out of range", |
| 1448 | input: &knownpb.Duration{Seconds: 0, Nanos: -1e9}, |
| 1449 | wantErr: true, |
| 1450 | }, { |
| 1451 | desc: "Timestamp zero", |
| 1452 | input: &knownpb.Timestamp{}, |
| 1453 | want: `"1970-01-01T00:00:00Z"`, |
| 1454 | }, { |
| 1455 | desc: "Timestamp", |
| 1456 | input: &knownpb.Timestamp{Seconds: 1553036601}, |
| 1457 | want: `"2019-03-19T23:03:21Z"`, |
| 1458 | }, { |
| 1459 | desc: "Timestamp with nanos", |
| 1460 | input: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1}, |
| 1461 | want: `"2019-03-19T23:03:21.000000001Z"`, |
| 1462 | }, { |
| 1463 | desc: "Timestamp with 6-digit nanos", |
| 1464 | input: &knownpb.Timestamp{Nanos: 1e3}, |
| 1465 | want: `"1970-01-01T00:00:00.000001Z"`, |
| 1466 | }, { |
| 1467 | desc: "Timestamp with 3-digit nanos", |
| 1468 | input: &knownpb.Timestamp{Nanos: 1e7}, |
| 1469 | want: `"1970-01-01T00:00:00.010Z"`, |
| 1470 | }, { |
| 1471 | desc: "Timestamp with +secs out of range", |
| 1472 | input: &knownpb.Timestamp{Seconds: 253402300800}, |
| 1473 | wantErr: true, |
| 1474 | }, { |
| 1475 | desc: "Timestamp with -secs out of range", |
| 1476 | input: &knownpb.Timestamp{Seconds: -62135596801}, |
| 1477 | wantErr: true, |
| 1478 | }, { |
| 1479 | desc: "Timestamp with -nanos", |
| 1480 | input: &knownpb.Timestamp{Nanos: -1}, |
| 1481 | wantErr: true, |
| 1482 | }, { |
| 1483 | desc: "Timestamp with +nanos out of range", |
| 1484 | input: &knownpb.Timestamp{Nanos: 1e9}, |
| 1485 | wantErr: true, |
| 1486 | }, { |
| 1487 | desc: "FieldMask empty", |
| 1488 | input: &knownpb.FieldMask{}, |
| 1489 | want: `""`, |
| 1490 | }, { |
| 1491 | desc: "FieldMask", |
| 1492 | input: &knownpb.FieldMask{ |
| 1493 | Paths: []string{ |
| 1494 | "foo", |
| 1495 | "foo_bar", |
| 1496 | "foo.bar_qux", |
| 1497 | "_foo", |
| 1498 | }, |
| 1499 | }, |
| 1500 | want: `"foo,fooBar,foo.barQux,Foo"`, |
| 1501 | }, { |
| 1502 | desc: "FieldMask error 1", |
| 1503 | input: &knownpb.FieldMask{ |
| 1504 | Paths: []string{"foo_"}, |
| 1505 | }, |
| 1506 | wantErr: true, |
| 1507 | }, { |
| 1508 | desc: "FieldMask error 2", |
| 1509 | input: &knownpb.FieldMask{ |
| 1510 | Paths: []string{"foo__bar"}, |
| 1511 | }, |
| 1512 | wantErr: true, |
| 1513 | }, { |
| 1514 | desc: "Any empty", |
| 1515 | input: &knownpb.Any{}, |
| 1516 | want: `{}`, |
| 1517 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1518 | desc: "Any with non-custom message", |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1519 | mo: jsonpb.MarshalOptions{ |
| 1520 | Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()), |
| 1521 | }, |
| 1522 | input: func() proto.Message { |
| 1523 | m := &pb2.Nested{ |
| 1524 | OptString: scalar.String("embedded inside Any"), |
| 1525 | OptNested: &pb2.Nested{ |
| 1526 | OptString: scalar.String("inception"), |
| 1527 | }, |
| 1528 | } |
| 1529 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1530 | if err != nil { |
| 1531 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1532 | } |
| 1533 | return &knownpb.Any{ |
| 1534 | TypeUrl: "foo/pb2.Nested", |
| 1535 | Value: b, |
| 1536 | } |
| 1537 | }(), |
| 1538 | want: `{ |
| 1539 | "@type": "foo/pb2.Nested", |
| 1540 | "optString": "embedded inside Any", |
| 1541 | "optNested": { |
| 1542 | "optString": "inception" |
| 1543 | } |
| 1544 | }`, |
| 1545 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1546 | desc: "Any with empty embedded message", |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1547 | mo: jsonpb.MarshalOptions{ |
| 1548 | Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()), |
| 1549 | }, |
| 1550 | input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"}, |
| 1551 | want: `{ |
| 1552 | "@type": "foo/pb2.Nested" |
| 1553 | }`, |
| 1554 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1555 | desc: "Any without registered type", |
| 1556 | mo: jsonpb.MarshalOptions{Resolver: preg.NewTypes()}, |
| 1557 | input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"}, |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1558 | wantErr: true, |
| 1559 | }, { |
| 1560 | desc: "Any with missing required error", |
| 1561 | mo: jsonpb.MarshalOptions{ |
| 1562 | Resolver: preg.NewTypes((&pb2.PartialRequired{}).ProtoReflect().Type()), |
| 1563 | }, |
| 1564 | input: func() proto.Message { |
| 1565 | m := &pb2.PartialRequired{ |
| 1566 | OptString: scalar.String("embedded inside Any"), |
| 1567 | } |
| 1568 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1569 | // TODO: Marshal may fail due to required field not set at some |
| 1570 | // point. Need to ignore required not set error here. |
| 1571 | if err != nil { |
| 1572 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1573 | } |
| 1574 | return &knownpb.Any{ |
| 1575 | TypeUrl: string(m.ProtoReflect().Type().FullName()), |
| 1576 | Value: b, |
| 1577 | } |
| 1578 | }(), |
| 1579 | want: `{ |
| 1580 | "@type": "pb2.PartialRequired", |
| 1581 | "optString": "embedded inside Any" |
| 1582 | }`, |
| 1583 | wantErr: true, |
| 1584 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1585 | desc: "Any with partial required and AllowPartial", |
| 1586 | mo: jsonpb.MarshalOptions{ |
| 1587 | AllowPartial: true, |
| 1588 | Resolver: preg.NewTypes((&pb2.PartialRequired{}).ProtoReflect().Type()), |
| 1589 | }, |
| 1590 | input: func() proto.Message { |
| 1591 | m := &pb2.PartialRequired{ |
| 1592 | OptString: scalar.String("embedded inside Any"), |
| 1593 | } |
| 1594 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1595 | // TODO: Marshal may fail due to required field not set at some |
| 1596 | // point. Need to ignore required not set error here. |
| 1597 | if err != nil { |
| 1598 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1599 | } |
| 1600 | return &knownpb.Any{ |
| 1601 | TypeUrl: string(m.ProtoReflect().Type().FullName()), |
| 1602 | Value: b, |
| 1603 | } |
| 1604 | }(), |
| 1605 | want: `{ |
| 1606 | "@type": "pb2.PartialRequired", |
| 1607 | "optString": "embedded inside Any" |
| 1608 | }`, |
| 1609 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1610 | desc: "Any with invalid UTF8", |
| 1611 | mo: jsonpb.MarshalOptions{ |
| 1612 | Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()), |
| 1613 | }, |
| 1614 | input: func() proto.Message { |
| 1615 | m := &pb2.Nested{ |
| 1616 | OptString: scalar.String("abc\xff"), |
| 1617 | } |
| 1618 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1619 | if err != nil { |
| 1620 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1621 | } |
| 1622 | return &knownpb.Any{ |
| 1623 | TypeUrl: "foo/pb2.Nested", |
| 1624 | Value: b, |
| 1625 | } |
| 1626 | }(), |
| 1627 | want: `{ |
| 1628 | "@type": "foo/pb2.Nested", |
| 1629 | "optString": "` + "abc\xff" + `" |
| 1630 | }`, |
| 1631 | wantErr: true, |
| 1632 | }, { |
| 1633 | desc: "Any with invalid value", |
| 1634 | mo: jsonpb.MarshalOptions{ |
| 1635 | Resolver: preg.NewTypes((&pb2.Nested{}).ProtoReflect().Type()), |
| 1636 | }, |
| 1637 | input: &knownpb.Any{ |
| 1638 | TypeUrl: "foo/pb2.Nested", |
| 1639 | Value: dhex("80"), |
| 1640 | }, |
| 1641 | wantErr: true, |
| 1642 | }, { |
| 1643 | desc: "Any with BoolValue", |
| 1644 | mo: jsonpb.MarshalOptions{ |
| 1645 | Resolver: preg.NewTypes((&knownpb.BoolValue{}).ProtoReflect().Type()), |
| 1646 | }, |
| 1647 | input: func() proto.Message { |
| 1648 | m := &knownpb.BoolValue{Value: true} |
| 1649 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1650 | if err != nil { |
| 1651 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1652 | } |
| 1653 | return &knownpb.Any{ |
| 1654 | TypeUrl: "type.googleapis.com/google.protobuf.BoolValue", |
| 1655 | Value: b, |
| 1656 | } |
| 1657 | }(), |
| 1658 | want: `{ |
| 1659 | "@type": "type.googleapis.com/google.protobuf.BoolValue", |
| 1660 | "value": true |
| 1661 | }`, |
| 1662 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1663 | desc: "Any with Empty", |
| 1664 | mo: jsonpb.MarshalOptions{ |
| 1665 | Resolver: preg.NewTypes((&knownpb.Empty{}).ProtoReflect().Type()), |
| 1666 | }, |
| 1667 | input: func() proto.Message { |
| 1668 | m := &knownpb.Empty{} |
| 1669 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1670 | if err != nil { |
| 1671 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1672 | } |
| 1673 | return &knownpb.Any{ |
| 1674 | TypeUrl: "type.googleapis.com/google.protobuf.Empty", |
| 1675 | Value: b, |
| 1676 | } |
| 1677 | }(), |
| 1678 | want: `{ |
Herbie Ong | 82014a5 | 2019-03-27 15:21:43 -0700 | [diff] [blame] | 1679 | "@type": "type.googleapis.com/google.protobuf.Empty", |
| 1680 | "value": {} |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1681 | }`, |
| 1682 | }, { |
| 1683 | desc: "Any with StringValue containing invalid UTF8", |
| 1684 | mo: jsonpb.MarshalOptions{ |
| 1685 | Resolver: preg.NewTypes((&knownpb.StringValue{}).ProtoReflect().Type()), |
| 1686 | }, |
| 1687 | input: func() proto.Message { |
| 1688 | m := &knownpb.StringValue{Value: "abc\xff"} |
| 1689 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1690 | if err != nil { |
| 1691 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1692 | } |
| 1693 | return &knownpb.Any{ |
| 1694 | TypeUrl: "google.protobuf.StringValue", |
| 1695 | Value: b, |
| 1696 | } |
| 1697 | }(), |
| 1698 | want: `{ |
| 1699 | "@type": "google.protobuf.StringValue", |
| 1700 | "value": "` + "abc\xff" + `" |
| 1701 | }`, |
| 1702 | wantErr: true, |
| 1703 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1704 | desc: "Any with Int64Value", |
| 1705 | mo: jsonpb.MarshalOptions{ |
| 1706 | Resolver: preg.NewTypes((&knownpb.Int64Value{}).ProtoReflect().Type()), |
| 1707 | }, |
| 1708 | input: func() proto.Message { |
| 1709 | m := &knownpb.Int64Value{Value: 42} |
| 1710 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1711 | if err != nil { |
| 1712 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1713 | } |
| 1714 | return &knownpb.Any{ |
| 1715 | TypeUrl: "google.protobuf.Int64Value", |
| 1716 | Value: b, |
| 1717 | } |
| 1718 | }(), |
| 1719 | want: `{ |
| 1720 | "@type": "google.protobuf.Int64Value", |
| 1721 | "value": "42" |
| 1722 | }`, |
| 1723 | }, { |
| 1724 | desc: "Any with Duration", |
| 1725 | mo: jsonpb.MarshalOptions{ |
| 1726 | Resolver: preg.NewTypes((&knownpb.Duration{}).ProtoReflect().Type()), |
| 1727 | }, |
| 1728 | input: func() proto.Message { |
| 1729 | m := &knownpb.Duration{} |
| 1730 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1731 | if err != nil { |
| 1732 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1733 | } |
| 1734 | return &knownpb.Any{ |
| 1735 | TypeUrl: "type.googleapis.com/google.protobuf.Duration", |
| 1736 | Value: b, |
| 1737 | } |
| 1738 | }(), |
| 1739 | want: `{ |
| 1740 | "@type": "type.googleapis.com/google.protobuf.Duration", |
| 1741 | "value": "0s" |
| 1742 | }`, |
| 1743 | }, { |
| 1744 | desc: "Any with empty Value", |
| 1745 | mo: jsonpb.MarshalOptions{ |
| 1746 | Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()), |
| 1747 | }, |
| 1748 | input: func() proto.Message { |
| 1749 | m := &knownpb.Value{} |
| 1750 | b, err := proto.Marshal(m) |
| 1751 | if err != nil { |
| 1752 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1753 | } |
| 1754 | return &knownpb.Any{ |
| 1755 | TypeUrl: "type.googleapis.com/google.protobuf.Value", |
| 1756 | Value: b, |
| 1757 | } |
| 1758 | }(), |
| 1759 | wantErr: true, |
| 1760 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1761 | desc: "Any with Value of StringValue", |
| 1762 | mo: jsonpb.MarshalOptions{ |
| 1763 | Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()), |
| 1764 | }, |
| 1765 | input: func() proto.Message { |
| 1766 | m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abc\xff"}} |
| 1767 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1768 | if err != nil { |
| 1769 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1770 | } |
| 1771 | return &knownpb.Any{ |
| 1772 | TypeUrl: "type.googleapis.com/google.protobuf.Value", |
| 1773 | Value: b, |
| 1774 | } |
| 1775 | }(), |
| 1776 | want: `{ |
| 1777 | "@type": "type.googleapis.com/google.protobuf.Value", |
| 1778 | "value": "` + "abc\xff" + `" |
| 1779 | }`, |
| 1780 | wantErr: true, |
| 1781 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1782 | desc: "Any with Value of NullValue", |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1783 | mo: jsonpb.MarshalOptions{ |
| 1784 | Resolver: preg.NewTypes((&knownpb.Value{}).ProtoReflect().Type()), |
| 1785 | }, |
| 1786 | input: func() proto.Message { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1787 | m := &knownpb.Value{Kind: &knownpb.Value_NullValue{}} |
| 1788 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1789 | if err != nil { |
| 1790 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1791 | } |
| 1792 | return &knownpb.Any{ |
| 1793 | TypeUrl: "type.googleapis.com/google.protobuf.Value", |
| 1794 | Value: b, |
| 1795 | } |
| 1796 | }(), |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1797 | want: `{ |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1798 | "@type": "type.googleapis.com/google.protobuf.Value", |
| 1799 | "value": null |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1800 | }`, |
| 1801 | }, { |
| 1802 | desc: "Any with Struct", |
| 1803 | mo: jsonpb.MarshalOptions{ |
| 1804 | Resolver: preg.NewTypes( |
| 1805 | (&knownpb.Struct{}).ProtoReflect().Type(), |
| 1806 | (&knownpb.Value{}).ProtoReflect().Type(), |
| 1807 | (&knownpb.BoolValue{}).ProtoReflect().Type(), |
| 1808 | knownpb.NullValue_NULL_VALUE.Type(), |
| 1809 | (&knownpb.StringValue{}).ProtoReflect().Type(), |
| 1810 | ), |
| 1811 | }, |
| 1812 | input: func() proto.Message { |
| 1813 | m := &knownpb.Struct{ |
| 1814 | Fields: map[string]*knownpb.Value{ |
| 1815 | "bool": {Kind: &knownpb.Value_BoolValue{true}}, |
| 1816 | "null": {Kind: &knownpb.Value_NullValue{}}, |
| 1817 | "string": {Kind: &knownpb.Value_StringValue{"hello"}}, |
| 1818 | "struct": { |
| 1819 | Kind: &knownpb.Value_StructValue{ |
| 1820 | &knownpb.Struct{ |
| 1821 | Fields: map[string]*knownpb.Value{ |
| 1822 | "string": {Kind: &knownpb.Value_StringValue{"world"}}, |
| 1823 | }, |
| 1824 | }, |
| 1825 | }, |
| 1826 | }, |
| 1827 | }, |
| 1828 | } |
| 1829 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1830 | if err != nil { |
| 1831 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1832 | } |
| 1833 | return &knownpb.Any{ |
| 1834 | TypeUrl: "google.protobuf.Struct", |
| 1835 | Value: b, |
| 1836 | } |
| 1837 | }(), |
| 1838 | want: `{ |
| 1839 | "@type": "google.protobuf.Struct", |
| 1840 | "value": { |
| 1841 | "bool": true, |
| 1842 | "null": null, |
| 1843 | "string": "hello", |
| 1844 | "struct": { |
| 1845 | "string": "world" |
| 1846 | } |
| 1847 | } |
| 1848 | }`, |
| 1849 | }, { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 1850 | desc: "Any with missing type_url", |
| 1851 | mo: jsonpb.MarshalOptions{ |
| 1852 | Resolver: preg.NewTypes((&knownpb.BoolValue{}).ProtoReflect().Type()), |
| 1853 | }, |
| 1854 | input: func() proto.Message { |
| 1855 | m := &knownpb.BoolValue{Value: true} |
| 1856 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 1857 | if err != nil { |
| 1858 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 1859 | } |
| 1860 | return &knownpb.Any{ |
| 1861 | Value: b, |
| 1862 | } |
| 1863 | }(), |
| 1864 | wantErr: true, |
| 1865 | }, { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1866 | desc: "well known types as field values", |
| 1867 | mo: jsonpb.MarshalOptions{ |
| 1868 | Resolver: preg.NewTypes((&knownpb.Empty{}).ProtoReflect().Type()), |
| 1869 | }, |
| 1870 | input: &pb2.KnownTypes{ |
| 1871 | OptBool: &knownpb.BoolValue{Value: false}, |
| 1872 | OptInt32: &knownpb.Int32Value{Value: 42}, |
| 1873 | OptInt64: &knownpb.Int64Value{Value: 42}, |
| 1874 | OptUint32: &knownpb.UInt32Value{Value: 42}, |
| 1875 | OptUint64: &knownpb.UInt64Value{Value: 42}, |
| 1876 | OptFloat: &knownpb.FloatValue{Value: 1.23}, |
| 1877 | OptDouble: &knownpb.DoubleValue{Value: 3.1415}, |
| 1878 | OptString: &knownpb.StringValue{Value: "hello"}, |
| 1879 | OptBytes: &knownpb.BytesValue{Value: []byte("hello")}, |
| 1880 | OptDuration: &knownpb.Duration{Seconds: 123}, |
| 1881 | OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601}, |
| 1882 | OptStruct: &knownpb.Struct{ |
| 1883 | Fields: map[string]*knownpb.Value{ |
| 1884 | "string": {Kind: &knownpb.Value_StringValue{"hello"}}, |
| 1885 | }, |
| 1886 | }, |
| 1887 | OptList: &knownpb.ListValue{ |
| 1888 | Values: []*knownpb.Value{ |
| 1889 | {Kind: &knownpb.Value_NullValue{}}, |
| 1890 | {Kind: &knownpb.Value_StringValue{}}, |
| 1891 | {Kind: &knownpb.Value_StructValue{}}, |
| 1892 | {Kind: &knownpb.Value_ListValue{}}, |
| 1893 | }, |
| 1894 | }, |
Herbie Ong | 1c7462c | 2019-03-22 17:56:55 -0700 | [diff] [blame] | 1895 | OptValue: &knownpb.Value{ |
| 1896 | Kind: &knownpb.Value_StringValue{"world"}, |
| 1897 | }, |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1898 | OptEmpty: &knownpb.Empty{}, |
| 1899 | OptAny: &knownpb.Any{ |
| 1900 | TypeUrl: "google.protobuf.Empty", |
| 1901 | }, |
| 1902 | OptFieldmask: &knownpb.FieldMask{ |
| 1903 | Paths: []string{"foo_bar", "bar_foo"}, |
| 1904 | }, |
| 1905 | }, |
| 1906 | want: `{ |
| 1907 | "optBool": false, |
| 1908 | "optInt32": 42, |
| 1909 | "optInt64": "42", |
| 1910 | "optUint32": 42, |
| 1911 | "optUint64": "42", |
| 1912 | "optFloat": 1.23, |
| 1913 | "optDouble": 3.1415, |
| 1914 | "optString": "hello", |
| 1915 | "optBytes": "aGVsbG8=", |
| 1916 | "optDuration": "123s", |
| 1917 | "optTimestamp": "2019-03-19T23:03:21Z", |
| 1918 | "optStruct": { |
| 1919 | "string": "hello" |
| 1920 | }, |
| 1921 | "optList": [ |
| 1922 | null, |
| 1923 | "", |
| 1924 | {}, |
| 1925 | [] |
| 1926 | ], |
Herbie Ong | 1c7462c | 2019-03-22 17:56:55 -0700 | [diff] [blame] | 1927 | "optValue": "world", |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1928 | "optEmpty": {}, |
| 1929 | "optAny": { |
Herbie Ong | 82014a5 | 2019-03-27 15:21:43 -0700 | [diff] [blame] | 1930 | "@type": "google.protobuf.Empty", |
| 1931 | "value": {} |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1932 | }, |
| 1933 | "optFieldmask": "fooBar,barFoo" |
| 1934 | }`, |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1935 | }} |
| 1936 | |
| 1937 | for _, tt := range tests { |
| 1938 | tt := tt |
| 1939 | t.Run(tt.desc, func(t *testing.T) { |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1940 | // Use 2-space indentation on all MarshalOptions. |
| 1941 | tt.mo.Indent = " " |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1942 | b, err := tt.mo.Marshal(tt.input) |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1943 | if err != nil && !tt.wantErr { |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1944 | t.Errorf("Marshal() returned error: %v\n", err) |
| 1945 | } |
Herbie Ong | 0b0f403 | 2019-03-18 19:06:15 -0700 | [diff] [blame] | 1946 | if err == nil && tt.wantErr { |
| 1947 | t.Errorf("Marshal() got nil error, want error\n") |
| 1948 | } |
Herbie Ong | 7b828bc | 2019-02-08 19:56:24 -0800 | [diff] [blame] | 1949 | got := string(b) |
| 1950 | if got != tt.want { |
| 1951 | t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want) |
| 1952 | if diff := cmp.Diff(tt.want, got, splitLines); diff != "" { |
| 1953 | t.Errorf("Marshal() diff -want +got\n%v\n", diff) |
| 1954 | } |
| 1955 | } |
| 1956 | }) |
| 1957 | } |
| 1958 | } |