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

DisableFetchingTableMetadata leads to key exception if PropertyConverter is used #3303

Closed
Dreamescaper opened this issue Apr 26, 2024 · 3 comments · Fixed by #3304
Closed
Labels
bug This issue is a bug. dynamodb p2 This is a standard priority issue

Comments

@Dreamescaper
Copy link
Contributor

Describe the bug

DisableFetchingTableMetadata does not account for PropertyConverters. See repro.

Expected Behavior

It shouldn't fail trying to save item with a strange message.

Current Behavior

System.InvalidOperationException: 'Key attribute Type must be of type Numeric'

Reproduction Steps

Repro
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;

var config = new AmazonDynamoDBConfig { ServiceURL = $"http://localhost:8000" };
var credentials = new BasicAWSCredentials("access", "secret");
var dynamoDbClient = new AmazonDynamoDBClient(credentials, config);
var context = new DynamoDBContext(dynamoDbClient, new DynamoDBContextConfig
{
    DisableFetchingTableMetadata = true
});

var tables = await dynamoDbClient.ListTablesAsync();
if (!tables.TableNames.Contains("TestTable"))
{
    await dynamoDbClient.CreateTableAsync(new CreateTableRequest
    {
        TableName = "TestTable",
        AttributeDefinitions =
        [
            new AttributeDefinition
            {
                AttributeName = "Type",
                AttributeType = ScalarAttributeType.S
            },
            new AttributeDefinition
            {
                AttributeName = "Id",
                AttributeType = ScalarAttributeType.S
            }
        ],
        KeySchema =
        [
            new KeySchemaElement
            {
                AttributeName = "Type",
                KeyType = KeyType.HASH
            },
            new KeySchemaElement
            {
                AttributeName = "Id",
                KeyType = KeyType.RANGE
            }
        ],
        ProvisionedThroughput = new ProvisionedThroughput
        {
            ReadCapacityUnits = 1,
            WriteCapacityUnits = 1
        }
    });

    Console.WriteLine("Table created");
}

await context.SaveAsync(new TestType
{
    Id = "1",
    Type = TypeEnum.Value2,
    Name = "Test",
});
Console.WriteLine("Item saved.");

var item = await context.LoadAsync<TestType>(TypeEnum.Value2, "1");
Console.WriteLine($"Item loaded: {item}");

[DynamoDBTable("TestTable")]
record class TestType
{
    [DynamoDBHashKey]
    [DynamoDBProperty(Converter = typeof(EnumAsStringConverter<TypeEnum>))]
    public TypeEnum Type { get; set; }

    [DynamoDBRangeKey]
    public string Id { get; set; }

    public string Name { get; set; }
}

enum TypeEnum
{
    Value1,
    Value2
}

public class EnumAsStringConverter<T> : IPropertyConverter where T : struct
{
    public object FromEntry(DynamoDBEntry entry) => Enum.Parse<T>(entry.AsString());
    public DynamoDBEntry ToEntry(object value) => new Primitive(value.ToString());
}

The code abode fails with an exception:
System.InvalidOperationException: 'Key attribute Type must be of type Numeric'

It works fine if I set DisableFetchingTableMetadata to false.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.DynamoDBEvents 3.7.302.19

Targeted .NET Platform

.NET 8

Operating System and version

AmazonLinuix

@Dreamescaper Dreamescaper added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 26, 2024
@ashishdhingra
Copy link
Contributor

Needs review with the team.

@ashishdhingra ashishdhingra added needs-review p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. needs-review labels Apr 26, 2024
@eddiemcs3 eddiemcs3 assigned eddiemcs3 and unassigned eddiemcs3 Apr 26, 2024
@ashishdhingra
Copy link
Contributor

The PR had been shipped AWSSDK.DynamoDBv2 version 3.7.302.26.

Copy link

github-actions bot commented May 3, 2024

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. dynamodb p2 This is a standard priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants