Skip to content

dynamodb: StringSet and StringSet decoders call unmarshaller incorrectly #2895

Closed
@AnatolyRugalev

Description

@AnatolyRugalev

Acknowledgements

Describe the bug

When unmarshaling StringSet or NumberSet values via attributevalue.Unmarshal, unmarshaller interface receives the whole set instead of individual slice items.

Unmarshaler is called here: https://github.com/aws/aws-sdk-go-v2/blob/main/feature/dynamodb/attributevalue/decode.go#L807

	for i := 0; i < v.Cap() && i < len(ss); i++ {
		if !isArray {
			v.SetLen(i + 1)
		}
		u, elem := indirect[Unmarshaler](v.Index(i), indirectOptions{})
		if u != nil {
            // This should pass `&types.AttributeValueMemberS{Value: s[i]}`
			return u.UnmarshalDynamoDBAttributeValue(&types.AttributeValueMemberSS{Value: ss})
		}
		if err := d.decodeString(ss[i], elem, tag{}); err != nil {
			return err
		}
	}

This issue is present in both dynamodb/attributevalue and dynamodbstreams/attributevalue

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Unmarshaler should receive individual set items

Current Behavior

Unmarshaler receives the whole set

Reproduction Steps

package main

import (
	"errors"
	"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

type myType string

type doc struct {
	Items []myType `dynamodbav:"items,stringset"`
}

func (m *myType) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error {
	switch v := av.(type) {
	case *types.AttributeValueMemberS:
		*m = myType(v.Value)
		return nil
	default:
		return errors.New("unexpected type")
	}
}

func main() {
	d := doc{
		Items: []myType{"a", "b"},
	}

	av, err := attributevalue.Marshal(d)
	if err != nil {
		panic(err)
	}

	var d2 doc
	if err := attributevalue.Unmarshal(av, &d2); err != nil {
		panic(err)
	}
}

Possible Solution

-			return u.UnmarshalDynamoDBAttributeValue(&types.AttributeValueMemberSS{Value: ss})
+ 		return u.UnmarshalDynamoDBAttributeValue(&types.AttributeValueMemberS{Value: ss[i]})

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

    github.com/aws/aws-sdk-go-v2 v1.27.1
	github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.0
	github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.7.22
	github.com/aws/aws-sdk-go-v2/service/dynamodb v1.32.7

Compiler and Version used

go version go1.23.2 darwin/arm64

Operating System and version

MacOS

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.feature/dynamodb/attributevaluePertains to dynamodb attributevalue marshaler HLL (feature/dynamodb/attributevalue).p2This is a standard priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions