| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975 |
- // Copyright 2019 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package jsonpb_test
- import (
- "bytes"
- "encoding/hex"
- "math"
- "strings"
- "testing"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- "google.golang.org/protobuf/encoding/jsonpb"
- "google.golang.org/protobuf/internal/encoding/pack"
- "google.golang.org/protobuf/internal/encoding/wire"
- pimpl "google.golang.org/protobuf/internal/impl"
- "google.golang.org/protobuf/internal/scalar"
- "google.golang.org/protobuf/proto"
- preg "google.golang.org/protobuf/reflect/protoregistry"
- "google.golang.org/protobuf/runtime/protoiface"
- "google.golang.org/protobuf/encoding/testprotos/pb2"
- "google.golang.org/protobuf/encoding/testprotos/pb3"
- knownpb "google.golang.org/protobuf/types/known"
- )
- // splitLines is a cmpopts.Option for comparing strings with line breaks.
- var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
- return strings.Split(s, "\n")
- })
- func pb2Enum(i int32) *pb2.Enum {
- p := new(pb2.Enum)
- *p = pb2.Enum(i)
- return p
- }
- func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
- p := new(pb2.Enums_NestedEnum)
- *p = pb2.Enums_NestedEnum(i)
- return p
- }
- func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
- knownFields := m.ProtoReflect().KnownFields()
- extTypes := knownFields.ExtensionTypes()
- extTypes.Register(xd.Type)
- if val == nil {
- return
- }
- pval := xd.Type.ValueOf(val)
- knownFields.Set(wire.Number(xd.Field), pval)
- }
- // dhex decodes a hex-string and returns the bytes and panics if s is invalid.
- func dhex(s string) []byte {
- b, err := hex.DecodeString(s)
- if err != nil {
- panic(err)
- }
- return b
- }
- func TestMarshal(t *testing.T) {
- tests := []struct {
- desc string
- mo jsonpb.MarshalOptions
- input proto.Message
- want string
- wantErr bool // TODO: Verify error message substring.
- }{{
- desc: "proto2 optional scalars not set",
- input: &pb2.Scalars{},
- want: "{}",
- }, {
- desc: "proto3 scalars not set",
- input: &pb3.Scalars{},
- want: "{}",
- }, {
- desc: "proto2 optional scalars set to zero values",
- input: &pb2.Scalars{
- OptBool: scalar.Bool(false),
- OptInt32: scalar.Int32(0),
- OptInt64: scalar.Int64(0),
- OptUint32: scalar.Uint32(0),
- OptUint64: scalar.Uint64(0),
- OptSint32: scalar.Int32(0),
- OptSint64: scalar.Int64(0),
- OptFixed32: scalar.Uint32(0),
- OptFixed64: scalar.Uint64(0),
- OptSfixed32: scalar.Int32(0),
- OptSfixed64: scalar.Int64(0),
- OptFloat: scalar.Float32(0),
- OptDouble: scalar.Float64(0),
- OptBytes: []byte{},
- OptString: scalar.String(""),
- },
- want: `{
- "optBool": false,
- "optInt32": 0,
- "optInt64": "0",
- "optUint32": 0,
- "optUint64": "0",
- "optSint32": 0,
- "optSint64": "0",
- "optFixed32": 0,
- "optFixed64": "0",
- "optSfixed32": 0,
- "optSfixed64": "0",
- "optFloat": 0,
- "optDouble": 0,
- "optBytes": "",
- "optString": ""
- }`,
- }, {
- desc: "proto2 optional scalars set to some values",
- input: &pb2.Scalars{
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(0xff),
- OptInt64: scalar.Int64(0xdeadbeef),
- OptUint32: scalar.Uint32(47),
- OptUint64: scalar.Uint64(0xdeadbeef),
- OptSint32: scalar.Int32(-1001),
- OptSint64: scalar.Int64(-0xffff),
- OptFixed64: scalar.Uint64(64),
- OptSfixed32: scalar.Int32(-32),
- OptFloat: scalar.Float32(1.02),
- OptDouble: scalar.Float64(1.234),
- OptBytes: []byte("谷歌"),
- OptString: scalar.String("谷歌"),
- },
- want: `{
- "optBool": true,
- "optInt32": 255,
- "optInt64": "3735928559",
- "optUint32": 47,
- "optUint64": "3735928559",
- "optSint32": -1001,
- "optSint64": "-65535",
- "optFixed64": "64",
- "optSfixed32": -32,
- "optFloat": 1.02,
- "optDouble": 1.234,
- "optBytes": "6LC35q2M",
- "optString": "谷歌"
- }`,
- }, {
- desc: "string",
- input: &pb3.Scalars{
- SString: "谷歌",
- },
- want: `{
- "sString": "谷歌"
- }`,
- }, {
- desc: "string with invalid UTF8",
- input: &pb3.Scalars{
- SString: "abc\xff",
- },
- want: "{\n \"sString\": \"abc\xff\"\n}",
- wantErr: true,
- }, {
- desc: "float nan",
- input: &pb3.Scalars{
- SFloat: float32(math.NaN()),
- },
- want: `{
- "sFloat": "NaN"
- }`,
- }, {
- desc: "float positive infinity",
- input: &pb3.Scalars{
- SFloat: float32(math.Inf(1)),
- },
- want: `{
- "sFloat": "Infinity"
- }`,
- }, {
- desc: "float negative infinity",
- input: &pb3.Scalars{
- SFloat: float32(math.Inf(-1)),
- },
- want: `{
- "sFloat": "-Infinity"
- }`,
- }, {
- desc: "double nan",
- input: &pb3.Scalars{
- SDouble: math.NaN(),
- },
- want: `{
- "sDouble": "NaN"
- }`,
- }, {
- desc: "double positive infinity",
- input: &pb3.Scalars{
- SDouble: math.Inf(1),
- },
- want: `{
- "sDouble": "Infinity"
- }`,
- }, {
- desc: "double negative infinity",
- input: &pb3.Scalars{
- SDouble: math.Inf(-1),
- },
- want: `{
- "sDouble": "-Infinity"
- }`,
- }, {
- desc: "proto2 enum not set",
- input: &pb2.Enums{},
- want: "{}",
- }, {
- desc: "proto2 enum set to zero value",
- input: &pb2.Enums{
- OptEnum: pb2Enum(0),
- OptNestedEnum: pb2Enums_NestedEnum(0),
- },
- want: `{
- "optEnum": 0,
- "optNestedEnum": 0
- }`,
- }, {
- desc: "proto2 enum",
- input: &pb2.Enums{
- OptEnum: pb2.Enum_ONE.Enum(),
- OptNestedEnum: pb2.Enums_UNO.Enum(),
- },
- want: `{
- "optEnum": "ONE",
- "optNestedEnum": "UNO"
- }`,
- }, {
- desc: "proto2 enum set to numeric values",
- input: &pb2.Enums{
- OptEnum: pb2Enum(2),
- OptNestedEnum: pb2Enums_NestedEnum(2),
- },
- want: `{
- "optEnum": "TWO",
- "optNestedEnum": "DOS"
- }`,
- }, {
- desc: "proto2 enum set to unnamed numeric values",
- input: &pb2.Enums{
- OptEnum: pb2Enum(101),
- OptNestedEnum: pb2Enums_NestedEnum(-101),
- },
- want: `{
- "optEnum": 101,
- "optNestedEnum": -101
- }`,
- }, {
- desc: "proto3 enum not set",
- input: &pb3.Enums{},
- want: "{}",
- }, {
- desc: "proto3 enum set to zero value",
- input: &pb3.Enums{
- SEnum: pb3.Enum_ZERO,
- SNestedEnum: pb3.Enums_CERO,
- },
- want: "{}",
- }, {
- desc: "proto3 enum",
- input: &pb3.Enums{
- SEnum: pb3.Enum_ONE,
- SNestedEnum: pb3.Enums_UNO,
- },
- want: `{
- "sEnum": "ONE",
- "sNestedEnum": "UNO"
- }`,
- }, {
- desc: "proto3 enum set to numeric values",
- input: &pb3.Enums{
- SEnum: 2,
- SNestedEnum: 2,
- },
- want: `{
- "sEnum": "TWO",
- "sNestedEnum": "DOS"
- }`,
- }, {
- desc: "proto3 enum set to unnamed numeric values",
- input: &pb3.Enums{
- SEnum: -47,
- SNestedEnum: 47,
- },
- want: `{
- "sEnum": -47,
- "sNestedEnum": 47
- }`,
- }, {
- desc: "proto2 nested message not set",
- input: &pb2.Nests{},
- want: "{}",
- }, {
- desc: "proto2 nested message set to empty",
- input: &pb2.Nests{
- OptNested: &pb2.Nested{},
- Optgroup: &pb2.Nests_OptGroup{},
- },
- want: `{
- "optNested": {},
- "optgroup": {}
- }`,
- }, {
- desc: "proto2 nested messages",
- input: &pb2.Nests{
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested message"),
- },
- },
- },
- want: `{
- "optNested": {
- "optString": "nested message",
- "optNested": {
- "optString": "another nested message"
- }
- }
- }`,
- }, {
- desc: "proto2 groups",
- input: &pb2.Nests{
- Optgroup: &pb2.Nests_OptGroup{
- OptString: scalar.String("inside a group"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message inside a group"),
- },
- Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
- OptFixed32: scalar.Uint32(47),
- },
- },
- },
- want: `{
- "optgroup": {
- "optString": "inside a group",
- "optNested": {
- "optString": "nested message inside a group"
- },
- "optnestedgroup": {
- "optFixed32": 47
- }
- }
- }`,
- }, {
- desc: "proto3 nested message not set",
- input: &pb3.Nests{},
- want: "{}",
- }, {
- desc: "proto3 nested message set to empty",
- input: &pb3.Nests{
- SNested: &pb3.Nested{},
- },
- want: `{
- "sNested": {}
- }`,
- }, {
- desc: "proto3 nested message",
- input: &pb3.Nests{
- SNested: &pb3.Nested{
- SString: "nested message",
- SNested: &pb3.Nested{
- SString: "another nested message",
- },
- },
- },
- want: `{
- "sNested": {
- "sString": "nested message",
- "sNested": {
- "sString": "another nested message"
- }
- }
- }`,
- }, {
- desc: "oneof not set",
- input: &pb3.Oneofs{},
- want: "{}",
- }, {
- desc: "oneof set to empty string",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{},
- },
- want: `{
- "oneofString": ""
- }`,
- }, {
- desc: "oneof set to string",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- want: `{
- "oneofString": "hello"
- }`,
- }, {
- desc: "oneof set to enum",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofEnum{
- OneofEnum: pb3.Enum_ZERO,
- },
- },
- want: `{
- "oneofEnum": "ZERO"
- }`,
- }, {
- desc: "oneof set to empty message",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{},
- },
- },
- want: `{
- "oneofNested": {}
- }`,
- }, {
- desc: "oneof set to message",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested message",
- },
- },
- },
- want: `{
- "oneofNested": {
- "sString": "nested message"
- }
- }`,
- }, {
- desc: "repeated fields not set",
- input: &pb2.Repeats{},
- want: "{}",
- }, {
- desc: "repeated fields set to empty slices",
- input: &pb2.Repeats{
- RptBool: []bool{},
- RptInt32: []int32{},
- RptInt64: []int64{},
- RptUint32: []uint32{},
- RptUint64: []uint64{},
- RptFloat: []float32{},
- RptDouble: []float64{},
- RptBytes: [][]byte{},
- },
- want: "{}",
- }, {
- desc: "repeated fields set to some values",
- input: &pb2.Repeats{
- RptBool: []bool{true, false, true, true},
- RptInt32: []int32{1, 6, 0, 0},
- RptInt64: []int64{-64, 47},
- RptUint32: []uint32{0xff, 0xffff},
- RptUint64: []uint64{0xdeadbeef},
- RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
- RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
- RptString: []string{"hello", "世界"},
- RptBytes: [][]byte{
- []byte("hello"),
- []byte("\xe4\xb8\x96\xe7\x95\x8c"),
- },
- },
- want: `{
- "rptBool": [
- true,
- false,
- true,
- true
- ],
- "rptInt32": [
- 1,
- 6,
- 0,
- 0
- ],
- "rptInt64": [
- "-64",
- "47"
- ],
- "rptUint32": [
- 255,
- 65535
- ],
- "rptUint64": [
- "3735928559"
- ],
- "rptFloat": [
- "NaN",
- "Infinity",
- "-Infinity",
- 1.034
- ],
- "rptDouble": [
- "NaN",
- "Infinity",
- "-Infinity",
- 1.23e-308
- ],
- "rptString": [
- "hello",
- "世界"
- ],
- "rptBytes": [
- "aGVsbG8=",
- "5LiW55WM"
- ]
- }`,
- }, {
- desc: "repeated enums",
- input: &pb2.Enums{
- RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
- RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
- },
- want: `{
- "rptEnum": [
- "ONE",
- "TWO",
- "TEN",
- 42
- ],
- "rptNestedEnum": [
- "DOS",
- 47,
- "DIEZ"
- ]
- }`,
- }, {
- desc: "repeated messages set to empty",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{},
- Rptgroup: []*pb2.Nests_RptGroup{},
- },
- want: "{}",
- }, {
- desc: "repeated messages",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{
- {
- OptString: scalar.String("repeat nested one"),
- },
- {
- OptString: scalar.String("repeat nested two"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inside repeat nested two"),
- },
- },
- {},
- },
- },
- want: `{
- "rptNested": [
- {
- "optString": "repeat nested one"
- },
- {
- "optString": "repeat nested two",
- "optNested": {
- "optString": "inside repeat nested two"
- }
- },
- {}
- ]
- }`,
- }, {
- desc: "repeated messages contains nil value",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{nil, {}},
- },
- want: `{
- "rptNested": [
- {},
- {}
- ]
- }`,
- }, {
- desc: "repeated groups",
- input: &pb2.Nests{
- Rptgroup: []*pb2.Nests_RptGroup{
- {
- RptString: []string{"hello", "world"},
- },
- {},
- nil,
- },
- },
- want: `{
- "rptgroup": [
- {
- "rptString": [
- "hello",
- "world"
- ]
- },
- {},
- {}
- ]
- }`,
- }, {
- desc: "map fields not set",
- input: &pb3.Maps{},
- want: "{}",
- }, {
- desc: "map fields set to empty",
- input: &pb3.Maps{
- Int32ToStr: map[int32]string{},
- BoolToUint32: map[bool]uint32{},
- Uint64ToEnum: map[uint64]pb3.Enum{},
- StrToNested: map[string]*pb3.Nested{},
- StrToOneofs: map[string]*pb3.Oneofs{},
- },
- want: "{}",
- }, {
- desc: "map fields 1",
- input: &pb3.Maps{
- BoolToUint32: map[bool]uint32{
- true: 42,
- false: 101,
- },
- },
- want: `{
- "boolToUint32": {
- "false": 101,
- "true": 42
- }
- }`,
- }, {
- desc: "map fields 2",
- input: &pb3.Maps{
- Int32ToStr: map[int32]string{
- -101: "-101",
- 0xff: "0xff",
- 0: "zero",
- },
- },
- want: `{
- "int32ToStr": {
- "-101": "-101",
- "0": "zero",
- "255": "0xff"
- }
- }`,
- }, {
- desc: "map fields 3",
- input: &pb3.Maps{
- Uint64ToEnum: map[uint64]pb3.Enum{
- 1: pb3.Enum_ONE,
- 2: pb3.Enum_TWO,
- 10: pb3.Enum_TEN,
- 47: 47,
- },
- },
- want: `{
- "uint64ToEnum": {
- "1": "ONE",
- "2": "TWO",
- "10": "TEN",
- "47": 47
- }
- }`,
- }, {
- desc: "map fields 4",
- input: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nested": &pb3.Nested{
- SString: "nested in a map",
- },
- },
- },
- want: `{
- "strToNested": {
- "nested": {
- "sString": "nested in a map"
- }
- }
- }`,
- }, {
- desc: "map fields 5",
- input: &pb3.Maps{
- StrToOneofs: map[string]*pb3.Oneofs{
- "string": &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- "nested": &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested oneof in map field value",
- },
- },
- },
- },
- },
- want: `{
- "strToOneofs": {
- "nested": {
- "oneofNested": {
- "sString": "nested oneof in map field value"
- }
- },
- "string": {
- "oneofString": "hello"
- }
- }
- }`,
- }, {
- desc: "map field contains nil value",
- input: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nil": nil,
- },
- },
- want: `{
- "strToNested": {
- "nil": {}
- }
- }`,
- }, {
- desc: "required fields not set",
- input: &pb2.Requireds{},
- want: `{}`,
- wantErr: true,
- }, {
- desc: "required fields partially set",
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE"
- }`,
- wantErr: true,
- }, {
- desc: "required fields not set with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE"
- }`,
- }, {
- desc: "required fields all set",
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- ReqNested: &pb2.Nested{},
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE",
- "reqNested": {}
- }`,
- }, {
- desc: "indirect required field",
- input: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- want: `{
- "optNested": {}
- }`,
- wantErr: true,
- }, {
- desc: "indirect required field with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- want: `{
- "optNested": {}
- }`,
- }, {
- desc: "indirect required field in empty repeated",
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{},
- },
- want: `{}`,
- }, {
- desc: "indirect required field in repeated",
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "rptNested": [
- {}
- ]
- }`,
- wantErr: true,
- }, {
- desc: "indirect required field in repeated with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "rptNested": [
- {}
- ]
- }`,
- }, {
- desc: "indirect required field in empty map",
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{},
- },
- want: "{}",
- }, {
- desc: "indirect required field in map",
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "fail": &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "strToNested": {
- "fail": {}
- }
- }`,
- wantErr: true,
- }, {
- desc: "indirect required field in map with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "fail": &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "strToNested": {
- "fail": {}
- }
- }`,
- }, {
- desc: "indirect required field in oneof",
- input: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "oneofNested": {}
- }`,
- wantErr: true,
- }, {
- desc: "indirect required field in oneof with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "oneofNested": {}
- }`,
- }, {
- desc: "unknown fields are ignored",
- input: &pb2.Scalars{
- OptString: scalar.String("no unknowns"),
- XXX_unrecognized: pack.Message{
- pack.Tag{101, pack.BytesType}, pack.String("hello world"),
- }.Marshal(),
- },
- want: `{
- "optString": "no unknowns"
- }`,
- }, {
- desc: "json_name",
- input: &pb3.JSONNames{
- SString: "json_name",
- },
- want: `{
- "foo_bar": "json_name"
- }`,
- }, {
- desc: "extensions of non-repeated fields",
- input: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_OptExtBool, true)
- setExtension(m, pb2.E_OptExtString, "extension field")
- setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- want: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.opt_ext_bool]": true,
- "[pb2.opt_ext_enum]": "TEN",
- "[pb2.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.opt_ext_string]": "extension field"
- }`,
- }, {
- desc: "extension message field set to nil",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_OptExtNested, nil)
- return m
- }(),
- want: "{}",
- }, {
- desc: "extensions of repeated fields",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
- setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- want: `{
- "[pb2.rpt_ext_enum]": [
- "TEN",
- 101,
- "ONE"
- ],
- "[pb2.rpt_ext_fixed32]": [
- 42,
- 47
- ],
- "[pb2.rpt_ext_nested]": [
- {
- "optString": "one"
- },
- {
- "optString": "two"
- },
- {
- "optString": "three"
- }
- ]
- }`,
- }, {
- desc: "extensions of non-repeated fields in another message",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
- setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- want: `{
- "[pb2.ExtensionsContainer.opt_ext_bool]": true,
- "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
- "[pb2.ExtensionsContainer.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
- }`,
- }, {
- desc: "extensions of repeated fields in another message",
- input: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- want: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.ExtensionsContainer.rpt_ext_enum]": [
- "TEN",
- 101,
- "ONE"
- ],
- "[pb2.ExtensionsContainer.rpt_ext_nested]": [
- {
- "optString": "one"
- },
- {
- "optString": "two"
- },
- {
- "optString": "three"
- }
- ],
- "[pb2.ExtensionsContainer.rpt_ext_string]": [
- "hello",
- "world"
- ]
- }`,
- }, {
- desc: "MessageSet",
- input: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
- OptString: scalar.String("just a regular extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.MessageSetExtension]": {
- "optString": "a messageset extension"
- },
- "[pb2.MessageSetExtension.ext_nested]": {
- "optString": "just a regular extension"
- },
- "[pb2.MessageSetExtension.not_message_set_extension]": {
- "optString": "not a messageset extension"
- }
- }`,
- }, {
- desc: "not real MessageSet 1",
- input: func() proto.Message {
- m := &pb2.FakeMessageSet{}
- setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.FakeMessageSetExtension.message_set_extension]": {
- "optString": "not a messageset extension"
- }
- }`,
- }, {
- desc: "not real MessageSet 2",
- input: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("another not a messageset extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.message_set_extension]": {
- "optString": "another not a messageset extension"
- }
- }`,
- }, {
- desc: "BoolValue empty",
- input: &knownpb.BoolValue{},
- want: `false`,
- }, {
- desc: "BoolValue",
- input: &knownpb.BoolValue{Value: true},
- want: `true`,
- }, {
- desc: "Int32Value empty",
- input: &knownpb.Int32Value{},
- want: `0`,
- }, {
- desc: "Int32Value",
- input: &knownpb.Int32Value{Value: 42},
- want: `42`,
- }, {
- desc: "Int64Value",
- input: &knownpb.Int64Value{Value: 42},
- want: `"42"`,
- }, {
- desc: "UInt32Value",
- input: &knownpb.UInt32Value{Value: 42},
- want: `42`,
- }, {
- desc: "UInt64Value",
- input: &knownpb.UInt64Value{Value: 42},
- want: `"42"`,
- }, {
- desc: "FloatValue",
- input: &knownpb.FloatValue{Value: 1.02},
- want: `1.02`,
- }, {
- desc: "FloatValue Infinity",
- input: &knownpb.FloatValue{Value: float32(math.Inf(-1))},
- want: `"-Infinity"`,
- }, {
- desc: "DoubleValue",
- input: &knownpb.DoubleValue{Value: 1.02},
- want: `1.02`,
- }, {
- desc: "DoubleValue NaN",
- input: &knownpb.DoubleValue{Value: math.NaN()},
- want: `"NaN"`,
- }, {
- desc: "StringValue empty",
- input: &knownpb.StringValue{},
- want: `""`,
- }, {
- desc: "StringValue",
- input: &knownpb.StringValue{Value: "谷歌"},
- want: `"谷歌"`,
- }, {
- desc: "StringValue with invalid UTF8 error",
- input: &knownpb.StringValue{Value: "abc\xff"},
- want: "\"abc\xff\"",
- wantErr: true,
- }, {
- desc: "StringValue field with invalid UTF8 error",
- input: &pb2.KnownTypes{
- OptString: &knownpb.StringValue{Value: "abc\xff"},
- },
- want: "{\n \"optString\": \"abc\xff\"\n}",
- wantErr: true,
- }, {
- desc: "BytesValue",
- input: &knownpb.BytesValue{Value: []byte("hello")},
- want: `"aGVsbG8="`,
- }, {
- desc: "Empty",
- input: &knownpb.Empty{},
- want: `{}`,
- }, {
- desc: "NullValue field",
- input: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
- want: `{
- "optNull": null
- }`,
- }, {
- desc: "Value empty",
- input: &knownpb.Value{},
- wantErr: true,
- }, {
- desc: "Value empty field",
- input: &pb2.KnownTypes{
- OptValue: &knownpb.Value{},
- },
- wantErr: true,
- }, {
- desc: "Value contains NullValue",
- input: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
- want: `null`,
- }, {
- desc: "Value contains BoolValue",
- input: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}},
- want: `false`,
- }, {
- desc: "Value contains NumberValue",
- input: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
- want: `1.02`,
- }, {
- desc: "Value contains StringValue",
- input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}},
- want: `"hello"`,
- }, {
- desc: "Value contains StringValue with invalid UTF8",
- input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
- want: "\"\xff\"",
- wantErr: true,
- }, {
- desc: "Value contains Struct",
- input: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "null": {Kind: &knownpb.Value_NullValue{}},
- "number": {Kind: &knownpb.Value_NumberValue{}},
- "string": {Kind: &knownpb.Value_StringValue{}},
- "struct": {Kind: &knownpb.Value_StructValue{}},
- "list": {Kind: &knownpb.Value_ListValue{}},
- "bool": {Kind: &knownpb.Value_BoolValue{}},
- },
- },
- },
- },
- want: `{
- "bool": false,
- "list": [],
- "null": null,
- "number": 0,
- "string": "",
- "struct": {}
- }`,
- }, {
- desc: "Value contains ListValue",
- input: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- {Kind: &knownpb.Value_StringValue{}},
- {Kind: &knownpb.Value_StructValue{}},
- {Kind: &knownpb.Value_ListValue{}},
- },
- },
- },
- },
- want: `[
- false,
- null,
- 0,
- "",
- {},
- []
- ]`,
- }, {
- desc: "Struct with nil map",
- input: &knownpb.Struct{},
- want: `{}`,
- }, {
- desc: "Struct with empty map",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{},
- },
- want: `{}`,
- }, {
- desc: "Struct",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "bool": {Kind: &knownpb.Value_BoolValue{true}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "number": {Kind: &knownpb.Value_NumberValue{3.1415}},
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- "list": {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- },
- },
- },
- },
- },
- },
- want: `{
- "bool": true,
- "list": [
- false,
- null,
- 0
- ],
- "null": null,
- "number": 3.1415,
- "string": "hello",
- "struct": {
- "string": "world"
- }
- }`,
- }, {
- desc: "Struct message with invalid UTF8 string",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"\xff"}},
- },
- },
- want: "{\n \"string\": \"\xff\"\n}",
- wantErr: true,
- }, {
- desc: "ListValue with nil values",
- input: &knownpb.ListValue{},
- want: `[]`,
- }, {
- desc: "ListValue with empty values",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{},
- },
- want: `[]`,
- }, {
- desc: "ListValue",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{true}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{3.1415}},
- {Kind: &knownpb.Value_StringValue{"hello"}},
- {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- },
- },
- },
- },
- {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- },
- },
- want: `[
- true,
- null,
- 3.1415,
- "hello",
- [
- false,
- null,
- 0
- ],
- {
- "string": "world"
- }
- ]`,
- }, {
- desc: "ListValue with invalid UTF8 string",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"\xff"}},
- },
- },
- want: "[\n \"\xff\"\n]",
- wantErr: true,
- }, {
- desc: "Duration empty",
- input: &knownpb.Duration{},
- want: `"0s"`,
- }, {
- desc: "Duration with secs",
- input: &knownpb.Duration{Seconds: 3},
- want: `"3s"`,
- }, {
- desc: "Duration with -secs",
- input: &knownpb.Duration{Seconds: -3},
- want: `"-3s"`,
- }, {
- desc: "Duration with nanos",
- input: &knownpb.Duration{Nanos: 1e6},
- want: `"0.001s"`,
- }, {
- desc: "Duration with -nanos",
- input: &knownpb.Duration{Nanos: -1e6},
- want: `"-0.001s"`,
- }, {
- desc: "Duration with large secs",
- input: &knownpb.Duration{Seconds: 1e10, Nanos: 1},
- want: `"10000000000.000000001s"`,
- }, {
- desc: "Duration with 6-digit nanos",
- input: &knownpb.Duration{Nanos: 1e4},
- want: `"0.000010s"`,
- }, {
- desc: "Duration with 3-digit nanos",
- input: &knownpb.Duration{Nanos: 1e6},
- want: `"0.001s"`,
- }, {
- desc: "Duration with -secs -nanos",
- input: &knownpb.Duration{Seconds: -123, Nanos: -450},
- want: `"-123.000000450s"`,
- }, {
- desc: "Duration max value",
- input: &knownpb.Duration{Seconds: 315576000000, Nanos: 999999999},
- want: `"315576000000.999999999s"`,
- }, {
- desc: "Duration min value",
- input: &knownpb.Duration{Seconds: -315576000000, Nanos: -999999999},
- want: `"-315576000000.999999999s"`,
- }, {
- desc: "Duration with +secs -nanos",
- input: &knownpb.Duration{Seconds: 1, Nanos: -1},
- wantErr: true,
- }, {
- desc: "Duration with -secs +nanos",
- input: &knownpb.Duration{Seconds: -1, Nanos: 1},
- wantErr: true,
- }, {
- desc: "Duration with +secs out of range",
- input: &knownpb.Duration{Seconds: 315576000001},
- wantErr: true,
- }, {
- desc: "Duration with -secs out of range",
- input: &knownpb.Duration{Seconds: -315576000001},
- wantErr: true,
- }, {
- desc: "Duration with +nanos out of range",
- input: &knownpb.Duration{Seconds: 0, Nanos: 1e9},
- wantErr: true,
- }, {
- desc: "Duration with -nanos out of range",
- input: &knownpb.Duration{Seconds: 0, Nanos: -1e9},
- wantErr: true,
- }, {
- desc: "Timestamp zero",
- input: &knownpb.Timestamp{},
- want: `"1970-01-01T00:00:00Z"`,
- }, {
- desc: "Timestamp",
- input: &knownpb.Timestamp{Seconds: 1553036601},
- want: `"2019-03-19T23:03:21Z"`,
- }, {
- desc: "Timestamp with nanos",
- input: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1},
- want: `"2019-03-19T23:03:21.000000001Z"`,
- }, {
- desc: "Timestamp with 6-digit nanos",
- input: &knownpb.Timestamp{Nanos: 1e3},
- want: `"1970-01-01T00:00:00.000001Z"`,
- }, {
- desc: "Timestamp with 3-digit nanos",
- input: &knownpb.Timestamp{Nanos: 1e7},
- want: `"1970-01-01T00:00:00.010Z"`,
- }, {
- desc: "Timestamp max value",
- input: &knownpb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
- want: `"9999-12-31T23:59:59.999999999Z"`,
- }, {
- desc: "Timestamp min value",
- input: &knownpb.Timestamp{Seconds: -62135596800},
- want: `"0001-01-01T00:00:00Z"`,
- }, {
- desc: "Timestamp with +secs out of range",
- input: &knownpb.Timestamp{Seconds: 253402300800},
- wantErr: true,
- }, {
- desc: "Timestamp with -secs out of range",
- input: &knownpb.Timestamp{Seconds: -62135596801},
- wantErr: true,
- }, {
- desc: "Timestamp with -nanos",
- input: &knownpb.Timestamp{Nanos: -1},
- wantErr: true,
- }, {
- desc: "Timestamp with +nanos out of range",
- input: &knownpb.Timestamp{Nanos: 1e9},
- wantErr: true,
- }, {
- desc: "FieldMask empty",
- input: &knownpb.FieldMask{},
- want: `""`,
- }, {
- desc: "FieldMask",
- input: &knownpb.FieldMask{
- Paths: []string{
- "foo",
- "foo_bar",
- "foo.bar_qux",
- "_foo",
- },
- },
- want: `"foo,fooBar,foo.barQux,Foo"`,
- }, {
- desc: "FieldMask error 1",
- input: &knownpb.FieldMask{
- Paths: []string{"foo_"},
- },
- wantErr: true,
- }, {
- desc: "FieldMask error 2",
- input: &knownpb.FieldMask{
- Paths: []string{"foo__bar"},
- },
- wantErr: true,
- }, {
- desc: "Any empty",
- input: &knownpb.Any{},
- want: `{}`,
- }, {
- desc: "Any with non-custom message",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("embedded inside Any"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inception"),
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- want: `{
- "@type": "foo/pb2.Nested",
- "optString": "embedded inside Any",
- "optNested": {
- "optString": "inception"
- }
- }`,
- }, {
- desc: "Any with empty embedded message",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
- want: `{
- "@type": "foo/pb2.Nested"
- }`,
- }, {
- desc: "Any without registered type",
- mo: jsonpb.MarshalOptions{Resolver: preg.NewTypes()},
- input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
- wantErr: true,
- }, {
- desc: "Any with missing required error",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- input: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- AllowPartial: true,
- Deterministic: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- want: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
- }`,
- wantErr: true,
- }, {
- desc: "Any with partial required and AllowPartial",
- mo: jsonpb.MarshalOptions{
- AllowPartial: true,
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- input: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- AllowPartial: true,
- Deterministic: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- want: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
- }`,
- }, {
- desc: "Any with invalid UTF8",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("abc\xff"),
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- want: `{
- "@type": "foo/pb2.Nested",
- "optString": "` + "abc\xff" + `"
- }`,
- wantErr: true,
- }, {
- desc: "Any with invalid value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: dhex("80"),
- },
- wantErr: true,
- }, {
- desc: "Any with BoolValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.BoolValue{Value: true}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.BoolValue",
- "value": true
- }`,
- }, {
- desc: "Any with Empty",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- input: func() proto.Message {
- m := &knownpb.Empty{}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Empty",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Empty",
- "value": {}
- }`,
- }, {
- desc: "Any with StringValue containing invalid UTF8",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.StringValue{Value: "abcd"}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.StringValue",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- want: `{
- "@type": "google.protobuf.StringValue",
- "value": "` + "abc\xff" + `"
- }`,
- wantErr: true,
- }, {
- desc: "Any with Int64Value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Int64Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Int64Value{Value: 42}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Int64Value",
- Value: b,
- }
- }(),
- want: `{
- "@type": "google.protobuf.Int64Value",
- "value": "42"
- }`,
- }, {
- desc: "Any with Duration",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Duration{})),
- },
- input: func() proto.Message {
- m := &knownpb.Duration{}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Duration",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Duration",
- "value": "0s"
- }`,
- }, {
- desc: "Any with empty Value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{}
- b, err := proto.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with Value of StringValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Value",
- "value": "` + "abc\xff" + `"
- }`,
- wantErr: true,
- }, {
- desc: "Any with Value of NullValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_NullValue{}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Value",
- "value": null
- }`,
- }, {
- desc: "Any with Struct",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(
- pimpl.Export{}.MessageTypeOf(&knownpb.Struct{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.Value{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{}),
- pimpl.Export{}.EnumTypeOf(knownpb.NullValue_NULL_VALUE),
- pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
- ),
- },
- input: func() proto.Message {
- m := &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "bool": {Kind: &knownpb.Value_BoolValue{true}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Struct",
- Value: b,
- }
- }(),
- want: `{
- "@type": "google.protobuf.Struct",
- "value": {
- "bool": true,
- "null": null,
- "string": "hello",
- "struct": {
- "string": "world"
- }
- }
- }`,
- }, {
- desc: "Any with missing type_url",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.BoolValue{Value: true}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "well known types as field values",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- input: &pb2.KnownTypes{
- OptBool: &knownpb.BoolValue{Value: false},
- OptInt32: &knownpb.Int32Value{Value: 42},
- OptInt64: &knownpb.Int64Value{Value: 42},
- OptUint32: &knownpb.UInt32Value{Value: 42},
- OptUint64: &knownpb.UInt64Value{Value: 42},
- OptFloat: &knownpb.FloatValue{Value: 1.23},
- OptDouble: &knownpb.DoubleValue{Value: 3.1415},
- OptString: &knownpb.StringValue{Value: "hello"},
- OptBytes: &knownpb.BytesValue{Value: []byte("hello")},
- OptDuration: &knownpb.Duration{Seconds: 123},
- OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601},
- OptStruct: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- },
- },
- OptList: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_StringValue{}},
- {Kind: &knownpb.Value_StructValue{}},
- {Kind: &knownpb.Value_ListValue{}},
- },
- },
- OptValue: &knownpb.Value{
- Kind: &knownpb.Value_StringValue{"world"},
- },
- OptEmpty: &knownpb.Empty{},
- OptAny: &knownpb.Any{
- TypeUrl: "google.protobuf.Empty",
- },
- OptFieldmask: &knownpb.FieldMask{
- Paths: []string{"foo_bar", "bar_foo"},
- },
- },
- want: `{
- "optBool": false,
- "optInt32": 42,
- "optInt64": "42",
- "optUint32": 42,
- "optUint64": "42",
- "optFloat": 1.23,
- "optDouble": 3.1415,
- "optString": "hello",
- "optBytes": "aGVsbG8=",
- "optDuration": "123s",
- "optTimestamp": "2019-03-19T23:03:21Z",
- "optStruct": {
- "string": "hello"
- },
- "optList": [
- null,
- "",
- {},
- []
- ],
- "optValue": "world",
- "optEmpty": {},
- "optAny": {
- "@type": "google.protobuf.Empty",
- "value": {}
- },
- "optFieldmask": "fooBar,barFoo"
- }`,
- }}
- for _, tt := range tests {
- tt := tt
- t.Run(tt.desc, func(t *testing.T) {
- // Use 2-space indentation on all MarshalOptions.
- tt.mo.Indent = " "
- b, err := tt.mo.Marshal(tt.input)
- if err != nil && !tt.wantErr {
- t.Errorf("Marshal() returned error: %v\n", err)
- }
- if err == nil && tt.wantErr {
- t.Errorf("Marshal() got nil error, want error\n")
- }
- got := string(b)
- if got != tt.want {
- t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
- if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
- t.Errorf("Marshal() diff -want +got\n%v\n", diff)
- }
- }
- })
- }
- }
|