Skip to content

Commit

Permalink
DEV-19660: prevent marshalling of nil ExtensionData (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
gm0stache authored Mar 29, 2023
1 parent 8cc7e75 commit 86a2948
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 26 deletions.
3 changes: 0 additions & 3 deletions bo/bilanzierung.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ func (bila Bilanzierung) GetDefaultJsonTags() []string {
}

func (bila *Bilanzierung) UnmarshalJSON(bytes []byte) (err error) {
if bila.ExtensionData == nil {
bila.ExtensionData = map[string]any{}
}
return unmappeddatamarshaller.UnmarshallWithUnmappedData(bila, &bila.ExtensionData, bytes)
}

Expand Down
3 changes: 0 additions & 3 deletions bo/marktlokation.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ type Marktlokation struct {
type marktlokationForUnmarshal Marktlokation

func (malo *Marktlokation) UnmarshalJSON(bytes []byte) (err error) {
if malo.ExtensionData == nil {
malo.ExtensionData = map[string]any{}
}
return unmappeddatamarshaller.UnmarshallWithUnmappedData(malo, &malo.ExtensionData, bytes)
}

Expand Down
3 changes: 0 additions & 3 deletions bo/messlokation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ func (melo Messlokation) GetDefaultJsonTags() []string {
}

func (melo *Messlokation) UnmarshalJSON(bytes []byte) (err error) {
if melo.ExtensionData == nil {
melo.ExtensionData = map[string]any{}
}
return unmappeddatamarshaller.UnmarshallWithUnmappedData(melo, &melo.ExtensionData, bytes)
}

Expand Down
12 changes: 4 additions & 8 deletions bo/messlokation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,12 @@ func (s *Suite) Test_Messlokation_Deserialization() {
err = json.Unmarshal(serializedMelo, &deserializedMelo)
then.AssertThat(s.T(), err, is.Nil())

// compare maps by value not by reference
isUnmappedDataEqual := melo.ExtensionData.CompareTo(deserializedMelo.ExtensionData)
then.AssertThat(s.T(), isUnmappedDataEqual, is.True())

// ignore reference inequality for 'Melo.UnmappedData.ExtensionData' field
deserializedMelo.ExtensionData = melo.ExtensionData
then.AssertThat(s.T(), deserializedMelo, is.EqualTo(melo))
areEqual, err := internal.CompareAsJson(melo, deserializedMelo)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), areEqual, is.True())
}

// Test_Failed_MesslokationValidation verifies that the validators of Messlokation work
// Test_Failed_MesslokationValidation verify that the validators of Messlokation work
func (s *Suite) Test_Failed_MesslokationValidation() {
validate := validator.New()
validate.RegisterStructValidation(bo.XorStructLevelMesslokationValidation, bo.Messlokation{})
Expand Down
5 changes: 4 additions & 1 deletion bo/zaehler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func (s *Suite) Test_Zaehler_Deserialization() {
var deserializedMeter bo.Zaehler
err = json.Unmarshal(serializedMeter, &deserializedMeter)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), deserializedMeter, is.EqualTo(meter))

areEqual, err := internal.CompareAsJson(meter, deserializedMeter)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), areEqual, is.True())
}

// TestFailedZaehlerValidation verifies that the validators of a Zaehler work
Expand Down
3 changes: 0 additions & 3 deletions com/zaehlwerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ type Zaehlwerk struct {
}

func (zw *Zaehlwerk) UnmarshalJSON(bytes []byte) (err error) {
if zw.ExtensionData == nil {
zw.ExtensionData = map[string]any{}
}
return unmappeddatamarshaller.UnmarshallWithUnmappedData(zw, &zw.ExtensionData, bytes)
}

Expand Down
4 changes: 3 additions & 1 deletion com/zaehlwerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ func (s *Suite) Test_Zaehlwerk_Deserialization() {
jsonString := string(serializedZaehlwerk)
then.AssertThat(s.T(), strings.Contains(jsonString, "KWH"), is.True()) // stringified enum
then.AssertThat(s.T(), strings.Contains(jsonString, "AUSSP"), is.True()) // stringified enum
then.AssertThat(s.T(), strings.Contains(jsonString, "wandlerfaktor\":1.2"), is.True()) // no quotes around die decimal
then.AssertThat(s.T(), strings.Contains(jsonString, "wandlerfaktor\":1.2"), is.True()) // no quotes around the decimal
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), serializedZaehlwerk, is.Not(is.Nil()))
var deserializedZaehlwerk com.Zaehlwerk
err = json.Unmarshal(serializedZaehlwerk, &deserializedZaehlwerk)
then.AssertThat(s.T(), err, is.Nil())
then.AssertThat(s.T(), deserializedZaehlwerk.ExtensionData.CompareTo(zaehlwerk.ExtensionData), is.True())
zaehlwerk.ExtensionData = deserializedZaehlwerk.ExtensionData
then.AssertThat(s.T(), deserializedZaehlwerk, is.EqualTo(zaehlwerk))
}

Expand Down
19 changes: 19 additions & 0 deletions internal/testing_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package internal

import (
"bytes"
"encoding/json"
)

func CompareAsJson[T any, T2 any](a T, a2 T2) (areEqual bool, err error) {
bytesFromA, err := json.Marshal(a)
if err != nil {
return
}
bytesFromA2, err := json.Marshal(a2)
if err != nil {
return
}

return bytes.Equal(bytesFromA, bytesFromA2), nil
}
4 changes: 4 additions & 0 deletions internal/unmappeddatamarshaller/unmappeddatamarshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func HandleUnmappedDataPropertyMarshalling(b []byte) (bytes []byte, err error) {
// UnmarshallWithUnmappedData will unmarshal a given type by mapping all strong-typed fields to the 'targetStruct'. All
// other fields will be preserved in the 'unmappedDataInTargetStruct' dictionary.
func UnmarshallWithUnmappedData[T any](targetStruct *T, unmappedDataInTargetStruct *ExtensionData, bytes []byte) (err error) {
if *unmappedDataInTargetStruct == nil {
*unmappedDataInTargetStruct = ExtensionData{}
}

var unmarshalledFields map[string]any
err = json.Unmarshal(bytes, &unmarshalledFields)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ func (s SomeStruct) MarshalJSON() (b []byte, e error) {
}

func (s *SomeStruct) UnmarshalJSON(bytes []byte) (err error) {
if s.ExtensionData == nil {
s.ExtensionData = map[string]any{}
}

return UnmarshallWithUnmappedData(s, &s.ExtensionData, bytes)
}

Expand Down

0 comments on commit 86a2948

Please sign in to comment.