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

DynamoDb marshaler ignores a name tag for embeded structs #2021

Closed
ztison opened this issue Feb 14, 2023 · 1 comment · Fixed by #2022
Closed

DynamoDb marshaler ignores a name tag for embeded structs #2021

ztison opened this issue Feb 14, 2023 · 1 comment · Fixed by #2022
Assignees
Labels
bug This issue is a bug.

Comments

@ztison
Copy link

ztison commented Feb 14, 2023

Describe the bug

When marshaling the Go object with the embedded struct the name tag is ignored, so the fields from the embedded struct are always inlined. Which could cause unexpected behavior when we have the same field name in the object and in the embedded struct.

Expected Behavior

Marshaller should use the name tag and create a nested map attribute. The same way as the JSON marshaller does.

Reproduction Steps

package main

import (
	"log"

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

type MetaObject struct {
	Org string `json:"org"`
}

type DataObject struct {
	MetaObject `json:"metadata"`
	Name       string `json:"name"`
}

func main() {
	encoder := attributevalue.NewEncoder(func(options *attributevalue.EncoderOptions) {
		options.TagKey = "json"
	})

	encoded, _ := encoder.Encode(&DataObject{
		MetaObject: MetaObject{Org: "org-1"},
		Name:       "name-1",
	})

	encodedMap := encoded.(*dynamodbtypes.AttributeValueMemberM)

	if _, ok := encodedMap.Value["metadata"]; !ok {
		log.Fatalf("metadata field is missing!")
	}
}

Possible Solution

Create a new encoder option to disable default inlining behavior.

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue@v1.10.11
github.com/aws/aws-sdk-go-v2/service/dynamodb@v1.18.2
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams@v1.14.2

Compiler and Version used

go version go1.19.5 darwin/arm64

Operating System and version

macOS Venture 13.1

@ztison ztison added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 14, 2023
Airblader added a commit to Airblader/aws-sdk-go-v2 that referenced this issue Feb 14, 2023
When collecting the fields in a struct, we skip over embedded fields
because by default they are just that: embedded.

However, if the embedded field itself carries a named tag, it is
not embedded during serialization, so consider this case.

fixes aws#2021
@aajtodd aajtodd removed the needs-triage This issue or PR still needs to be triaged. label Feb 17, 2023
@aajtodd aajtodd self-assigned this Feb 17, 2023
aajtodd pushed a commit that referenced this issue Feb 20, 2023
When collecting the fields in a struct, we skip over embedded fields
because by default they are just that: embedded.

However, if the embedded field itself carries a named tag, it is
not embedded during serialization, so consider this case.

fixes #2021
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants