@@ -194,6 +194,61 @@ func TestWithOmitsEmptyAnnotationOnRelation_MixedData(t *testing.T) {
194194 }
195195}
196196
197+ func TestWithOmitsEmptyAnnotationOnAttribute (t * testing.T ) {
198+ type Phone struct {
199+ Number string `json:"number"`
200+ }
201+
202+ type Address struct {
203+ City string `json:"city"`
204+ Street string `json:"street"`
205+ }
206+
207+ type Tags map [string ]int
208+
209+ type Author struct {
210+ ID int `jsonapi:"primary,authors"`
211+ Name string `jsonapi:"attr,title"`
212+ Phones []* Phone `jsonapi:"attr,phones,omitempty"`
213+ Address * Address `jsonapi:"attr,address,omitempty"`
214+ Tags Tags `jsonapi:"attr,tags,omitempty"`
215+ }
216+
217+ author := & Author {
218+ ID : 999 ,
219+ Name : "Igor" ,
220+ Phones : nil , // should be omitted
221+ Address : nil , // should be omitted
222+ Tags : Tags {"dogs" : 1 , "cats" : 2 }, // should not be omitted
223+ }
224+
225+ out := bytes .NewBuffer (nil )
226+ if err := MarshalPayload (out , author ); err != nil {
227+ t .Fatal (err )
228+ }
229+
230+ var jsonData map [string ]interface {}
231+ if err := json .Unmarshal (out .Bytes (), & jsonData ); err != nil {
232+ t .Fatal (err )
233+ }
234+
235+ // Verify that there is no field "phones" in attributes
236+ payload := jsonData ["data" ].(map [string ]interface {})
237+ attributes := payload ["attributes" ].(map [string ]interface {})
238+ if _ , ok := attributes ["title" ]; ! ok {
239+ t .Fatal ("Was expecting the data.attributes.title to have NOT been omitted" )
240+ }
241+ if _ , ok := attributes ["phones" ]; ok {
242+ t .Fatal ("Was expecting the data.attributes.phones to have been omitted" )
243+ }
244+ if _ , ok := attributes ["address" ]; ok {
245+ t .Fatal ("Was expecting the data.attributes.phones to have been omitted" )
246+ }
247+ if _ , ok := attributes ["tags" ]; ! ok {
248+ t .Fatal ("Was expecting the data.attributes.tags to have NOT been omitted" )
249+ }
250+ }
251+
197252func TestMarshalIDPtr (t * testing.T ) {
198253 id , make , model := "123e4567-e89b-12d3-a456-426655440000" , "Ford" , "Mustang"
199254 car := & Car {
0 commit comments