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

Saving nested items with class hierarchies #2985

Closed
atjohns opened this issue Jun 28, 2023 · 4 comments
Closed

Saving nested items with class hierarchies #2985

atjohns opened this issue Jun 28, 2023 · 4 comments
Labels
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued

Comments

@atjohns
Copy link

atjohns commented Jun 28, 2023

Describe the bug

This is a similar issue to a very old discussion posted here on class hierarchies in Dynamo, but this one is explicitly about saving items. I am trying to use DynamoDB to save a model that mimics the following structure


    public class A
    {
        public string Name { get; set; }

        public int MyPropA { get; set; } = 1;
    }

    public class B : A
    {
        public int MyPropB { get; set; } = 2;
    }

    [DynamoDBTable("Test")]
    public class ModelA
    {
        [DynamoDBHashKey]
        public Guid id { get; set; }

        public List<A> MyClasses { get; set; }
    }

If we instantiate a new instance of this model like so...


            var a1 = new A { Name = "A1" };
            var b1 = new B { Name = "B1" };

            Guid id = Guid.NewGuid();

            var model = new ModelA
            {
                id = id,
                MyClasses = new List<A> { a1, b1 }
            };

We end up with a list that contains both Class B & Class A types that are being saved into Dynamo incorrectly.

Expected Behavior

Expectation would be for both Model A & Model B to save with the properties that are pushed by the code and be able to recognize the Model B inheritance

Current Behavior

When this model is saved to Dynamo it only saves the Class A properties. Here's a screenshot showing what the object looks like when the Save call is made vs what it looks like within Dynamo

image

Reproduction Steps

See above

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.DynamoDBv2 3.7.104

Targeted .NET Platform

.NET 7

Operating System and version

Windows 10

@atjohns atjohns added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 28, 2023
@ashishdhingra
Copy link
Contributor

Reproducible using below code:

using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.Runtime;

using (AmazonDynamoDBClient client = new AmazonDynamoDBClient())
{
    DynamoDBContext context = new DynamoDBContext(client);
    var a1 = new A { Name = "A1" };
    var b1 = new B { Name = "B1" };

    Guid id = Guid.NewGuid();

    var model = new ModelA
    {
        id = id,
        MyClasses = new List<A> { a1, b1 }
    };

    await context.SaveAsync(model);
}



public class A
{
    public string Name { get; set; }

    public int MyPropA { get; set; } = 1;
}

public class B : A
{
    public int MyPropB { get; set; } = 2;
}

[DynamoDBTable("Test")]
public class ModelA
{
    [DynamoDBHashKey]
    public Guid id { get; set; }

    public List<A> MyClasses { get; set; }
}

Unsure if this is by design due to limitation mentioned in #245 (comment). Needs review with the team.

@ashishdhingra ashishdhingra added needs-review and removed needs-triage This issue or PR still needs to be triaged. labels Jun 28, 2023
@bykka
Copy link

bykka commented Jul 5, 2023

Looks like it a common issue for dynamoDB sdk of different languages.
At least java version discuss the same problem
aws/aws-sdk-java-v2#2861
I guess would be nice to have the similar implementation for simpler code migration

@ashishdhingra ashishdhingra added feature-request A feature should be added or improved. p2 This is a standard priority issue queued and removed bug This issue is a bug. needs-review labels Jul 28, 2023
@dscpinheiro
Copy link
Contributor

This has been released in the latest preview of V4: https://www.nuget.org/packages/AWSSDK.DynamoDBv2/4.0.0-preview.9

Thanks again @irina-herciu for the contribution!

Copy link

github-actions bot commented Mar 4, 2025

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
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

4 participants