Skip to content

Commit

Permalink
Fix/null serialization collection (#92)
Browse files Browse the repository at this point in the history
* Safely serialize nulls in collections

* Update changelog
  • Loading branch information
rkodev committed Jun 14, 2023
1 parent e5fc62c commit 7f47527
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.2] - 2023-06-14

- Safely serialize null values in collections of Objects, Enums or primitives.

### Changed

## [1.0.1] - 2023-05-25

- Fixes bug where slices backing data from `GetSerializedContent` could be overwritten before they were used but after `JsonSerializationWriter.Close()` was called.
Expand Down
4 changes: 4 additions & 0 deletions internal/test_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type TestEntityable interface {
SetCreatedDateTime(value *time.Time)
}

func TestEntityDiscriminator(absser.ParseNode) (absser.Parsable, error) {
return NewTestEntity(), nil
}

func NewTestEntity() *TestEntity {
return &TestEntity{
additionalData: make(map[string]interface{}),
Expand Down
36 changes: 24 additions & 12 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,15 @@ func (n *JsonParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory)
}
result := make([]absser.Parsable, len(nodes))
for i, v := range nodes {
val, err := (*v).GetObjectValue(ctor)
if err != nil {
return nil, err
if v != nil {
val, err := (*v).GetObjectValue(ctor)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
result[i] = val
}
return result, nil
}
Expand All @@ -270,11 +274,15 @@ func (n *JsonParseNode) GetCollectionOfPrimitiveValues(targetType string) ([]int
}
result := make([]interface{}, len(nodes))
for i, v := range nodes {
val, err := v.getPrimitiveValue(targetType)
if err != nil {
return nil, err
if v != nil {
val, err := v.getPrimitiveValue(targetType)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
result[i] = val
}
return result, nil
}
Expand Down Expand Up @@ -327,11 +335,15 @@ func (n *JsonParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]
}
result := make([]interface{}, len(nodes))
for i, v := range nodes {
val, err := v.GetEnumValue(parser)
if err != nil {
return nil, err
if v != nil {
val, err := v.GetEnumValue(parser)
if err != nil {
return nil, err
}
result[i] = val
} else {
result[i] = nil
}
result[i] = val
}
return result, nil
}
Expand Down
43 changes: 41 additions & 2 deletions json_parse_node_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package jsonserialization

import (
"github.com/microsoft/kiota-serialization-json-go/internal"
"github.com/stretchr/testify/require"
testing "testing"
"testing"

absser "github.com/microsoft/kiota-abstractions-go/serialization"
assert "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)

func TestTree(t *testing.T) {
Expand Down Expand Up @@ -120,6 +121,44 @@ func TestNestedGetRawValue(t *testing.T) {
assert.Equal(t, expected, value)
}

func TestNilValuesInCollections(t *testing.T) {
source := `{
"id": "2",
"status": 200,
"item": null,
"phones": [1,2, null,3],
"testEntities": [
{
"id": "acbb4e46-0aa9-11ee-be56-0242ac120002",
"officeLocation": "Nairobi"
},
null,
{
"id": "acbb4e46-0aa9-11ee-be56-0242ac120002",
"officeLocation": "Nairobi"
}
]
}`
sourceArray := []byte(source)
parseNode, err := NewJsonParseNode(sourceArray)
if err != nil {
t.Errorf("Error creating parse node: %s", err.Error())
}
someProp, err := parseNode.GetChildNode("testEntities")
require.NoError(t, err)
value, err := someProp.GetCollectionOfObjectValues(internal.TestEntityDiscriminator)
require.NoError(t, err)
assert.Equal(t, "Nairobi", *(value[0].(*internal.TestEntity)).GetOfficeLocation())
assert.Nil(t, value[1])

phoneProp, err := parseNode.GetChildNode("phones")
require.NoError(t, err)
phonesValue, err := phoneProp.GetCollectionOfPrimitiveValues("int32")
require.NoError(t, err)
assert.Equal(t, int32(1), *(phonesValue[0].(*int32)))
assert.Equal(t, nil, phonesValue[2])
}

func ref[T interface{}](t T) *T {
return &t
}
Expand Down

0 comments on commit 7f47527

Please sign in to comment.