Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds values of unamed array to additional data #96

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1.0] - 2023-07-11

### Added

- Adds `ArraysAdditionalData` in additional data to hold serialized array or arrays.

## [1.0.3] - 2023-06-28

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

import (
i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91 "github.com/microsoft/kiota-abstractions-go/serialization"
ie8677ce2c7e1b4c22e9c3827ecd078d41185424dd9eeb92b7d971ed2d49a392e "github.com/microsoft/kiota-abstractions-go/store"
)

// Json
type Json struct {
// Stores model information.
backingStore ie8677ce2c7e1b4c22e9c3827ecd078d41185424dd9eeb92b7d971ed2d49a392e.BackingStore
}

// NewJson instantiates a new Json and sets the default values.
func NewJson() *Json {
m := &Json{}
m.backingStore = ie8677ce2c7e1b4c22e9c3827ecd078d41185424dd9eeb92b7d971ed2d49a392e.BackingStoreFactoryInstance()
m.SetAdditionalData(make(map[string]any))
return m
}

// CreateJsonFromDiscriminatorValue creates a new instance of the appropriate class based on discriminator value
func CreateJsonFromDiscriminatorValue(parseNode i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.ParseNode) (i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable, error) {
return NewJson(), nil
}

// GetAdditionalData gets the additionalData property value. Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.
func (m *Json) GetAdditionalData() map[string]any {
val, _ := m.backingStore.Get("additionalData")
return val.(map[string]any)
}

// GetBackingStore gets the backingStore property value. Stores model information.
func (m *Json) GetBackingStore() ie8677ce2c7e1b4c22e9c3827ecd078d41185424dd9eeb92b7d971ed2d49a392e.BackingStore {
return m.backingStore
}

// GetFieldDeserializers the deserialization information for the current model
func (m *Json) GetFieldDeserializers() map[string]func(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.ParseNode) error {
return make(map[string]func(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.ParseNode) error)
}

// Serialize serializes information the current object
func (m *Json) Serialize(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.SerializationWriter) error {
return nil
}

// SetAdditionalData sets the additionalData property value. Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.
func (m *Json) SetAdditionalData(value map[string]any) {
err := m.GetBackingStore().Set("additionalData", value)
if err != nil {
panic(err)
}
}

// SetBackingStore sets the backingStore property value. Stores model information.
func (m *Json) SetBackingStore(value ie8677ce2c7e1b4c22e9c3827ecd078d41185424dd9eeb92b7d971ed2d49a392e.BackingStore) {
m.backingStore = value
}
55 changes: 55 additions & 0 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)

const ArraysAdditionalData = "ArraysAdditionalData"

// JsonParseNode is a ParseNode implementation for JSON.
type JsonParseNode struct {
value interface{}
Expand Down Expand Up @@ -229,10 +231,63 @@ func (n *JsonParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Pars
}
}
}
if !ok {
// try cast to JsonParseNode array and read each value
nodes, okArray := n.value.([]*JsonParseNode)
itemAsHolder, isHolder := result.(absser.AdditionalDataHolder)
if okArray && isHolder {
var itemAdditionalData map[string]interface{}
itemAdditionalData = itemAsHolder.GetAdditionalData()
if itemAdditionalData == nil {
itemAdditionalData = make(map[string]interface{})
itemAsHolder.SetAdditionalData(itemAdditionalData)
}

arrayValues := make([]interface{}, len(nodes))
for i, v := range nodes {
if v != nil {
err := v.SetOnBeforeAssignFieldValues(n.GetOnBeforeAssignFieldValues())
if err != nil {
return nil, err
}
err = v.SetOnAfterAssignFieldValues(n.GetOnAfterAssignFieldValues())
if err != nil {
return nil, err
}
}

if v != nil && isHolder {
rawValue, err := v.GetRawValue()
if err != nil {
return nil, err
}
arrayValues[i] = rawValue
}
}
addArrayToAdditionalDataHolder(itemAdditionalData, arrayValues)
}
}
abstractions.InvokeParsableAction(n.GetOnAfterAssignFieldValues(), result)
return result, nil
}

func addArrayToAdditionalDataHolder(itemAdditionalData map[string]interface{}, values []interface{}) {
val, ok := itemAdditionalData[ArraysAdditionalData]
// If the key exists
if !ok {
// create an array and add the values to the array
itemAdditionalData[ArraysAdditionalData] = values
} else {
// If the key exists, but the value is not a slice
if _, ok := val.([][]interface{}); !ok {
itemAdditionalData[ArraysAdditionalData] = values
} else {
// If the key exists and the value is a slice
itemAdditionalData[ArraysAdditionalData] = append(val.([]interface{}), values...)
}
}
}

// GetCollectionOfObjectValues returns the collection of Parsable values from the node.
func (n *JsonParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory) ([]absser.Parsable, error) {
if n == nil || n.value == nil {
Expand Down
29 changes: 29 additions & 0 deletions json_parse_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,35 @@ func TestThrowErrorOfPrimitiveType(t *testing.T) {
assert.Equal(t, "targetType wrong.UUID is not supported", err.Error())
}

func TestSerializationOfUnamedMaps(t *testing.T) {
source := `{
"values": [
["Name", "Amount"],
["Tony" , 100],
["John" , 200],
["Mary" , 300]
]
}`
sourceArray := []byte(source)
parseNode, err := NewJsonParseNode(sourceArray)
if err != nil {
t.Errorf("Error creating parse node: %s", err.Error())
}

someProp, err := parseNode.GetChildNode("values")
value, err := someProp.GetObjectValue(internal.CreateJsonFromDiscriminatorValue)
jsonValue := value.(*internal.Json)
arrayValues := jsonValue.GetAdditionalData()[ArraysAdditionalData].([]interface{})
assert.Equal(t, "Name", *arrayValues[0].([]interface{})[0].(*string))
assert.Equal(t, "Amount", *arrayValues[0].([]interface{})[1].(*string))

assert.Equal(t, "Tony", *arrayValues[1].([]interface{})[0].(*string))
assert.Equal(t, float64(100), *arrayValues[1].([]interface{})[1].(*float64))

assert.Equal(t, "John", *arrayValues[2].([]interface{})[0].(*string))
assert.Equal(t, float64(200), *arrayValues[2].([]interface{})[1].(*float64))
}

const FunctionalTestSource = "{" +
"\"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#users('vincent%40biret365.onmicrosoft.com')/messages\"," +
"\"@odata.nextLink\": \"https://graph.microsoft.com/v1.0/users/[email protected]/messages?$skip=10\"," +
Expand Down