diff --git a/marshal_test.go b/marshal_test.go index c42223fed38e02a215e8b47266c39cd6ef05e1f1..daf5460dd550b4336473d0e5949bd357cbc63ed0 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -6,7 +6,11 @@ import ( "time" "github.com/quickfixgo/quickfix" + "github.com/quickfixgo/quickfix/enum" "github.com/quickfixgo/quickfix/field" + "github.com/quickfixgo/quickfix/fix50sp1/instrument" + "github.com/quickfixgo/quickfix/fix50sp1/marketdatasnapshotfullrefresh" + "github.com/quickfixgo/quickfix/fix50sp1/mdfullgrp" ) func TestMarshal_FIXMsgType(t *testing.T) { @@ -363,3 +367,13 @@ func TestMarshal_HeaderTrailer(t *testing.T) { } } } + +// [GH 109] https://github.com/quickfixgo/quickfix/issues/109 +func TestGH109_MarshalWillNotRecurseIntoTaggedStructFieldsAndCausePanic(t *testing.T) { + // When a message contains a non-component struct field (i.e. NoMDEntries.ExpireTime has type *time.Time) + grp := mdfullgrp.New([]mdfullgrp.NoMDEntries{mdfullgrp.NoMDEntries{MDEntryType: enum.MDEntryType_BID}}) + msg := marketdatasnapshotfullrefresh.New(instrument.Instrument{}, *grp) + + // Then parseStructTag should not cause `panic: strconv.ParseInt: parsing "": invalid syntax` when marshalling + msg.Marshal() +} diff --git a/reflection.go b/reflection.go index 162161d17fe0d742978e29f1fc1e7603724d31be..6597a8b2fcbc0dc0eeba8c6cd7d7eea3ee0c36d7 100644 --- a/reflection.go +++ b/reflection.go @@ -39,18 +39,19 @@ func buildGroupTemplate(elem reflect.Type) GroupTemplate { for i := 0; i < t.NumField(); i++ { sf := t.Field(i) - //recurse if item is a component, optional or not elementType := sf.Type if elementType.Kind() == reflect.Ptr { elementType = elementType.Elem() } - if elementType.Kind() == reflect.Struct { + // recurse if item is a component, optional or not + structTag := sf.Tag.Get("fix") + if structTag == "" && elementType.Kind() == reflect.Struct { walkFunc(elementType) continue } - afixTag, _, _ := parseStructTag(sf.Tag.Get("fix")) + afixTag, _, _ := parseStructTag(structTag) template = append(template, GroupElement(Tag(afixTag))) } } diff --git a/unmarshal_test.go b/unmarshal_test.go index 0484b3fbaa43569a1efe1ba7f3c4dba37ccf74e0..e7b7037facc1570fda4640eb13a9be5ba80fa0a7 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -256,7 +256,7 @@ func TestUnmarshal_HeaderTrailer(t *testing.T) { } // [GH 103] https://github.com/quickfixgo/quickfix/issues/103 -func TestUnmarshal_FailsForRepeatingGroupWithoutCorrectGroupDelimter(t *testing.T) { +func TestGH103_UnmarshalFailsForRepeatingGroupWithoutCorrectGroupDelimter(t *testing.T) { // Given a message containing a repeating group WITHOUT the correct delimiter field (tag 55) rawFix := []byte("8=FIXT.1.19=11735=V34=249=MDC52=20160419-22:58:50.94756=KMD262=req_A263=0264=5146=148=DORZ1722=99267=3269=0269=1269=210=194") fixMsg, err := quickfix.ParseMessage(rawFix)