-
Notifications
You must be signed in to change notification settings - Fork 1
fix(dynamo-gen): filter [UnixTimestamp] to DateTime/DateTimeOffset properties #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
tests/Clients/Goa.Clients.Dynamo.Generator.Tests/CodeGeneration/JsonMapperGeneratorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| using Goa.Clients.Dynamo.Generator.CodeGeneration; | ||
| using Goa.Clients.Dynamo.Generator.TypeHandlers; | ||
| using Goa.Clients.Dynamo.Generator.Tests.Helpers; | ||
| using Goa.Clients.Dynamo.Generator.Models; | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace Goa.Clients.Dynamo.Generator.Tests.CodeGeneration; | ||
|
|
||
| public class JsonMapperGeneratorTests | ||
| { | ||
| private readonly TypeHandlerRegistry _typeHandlerRegistry; | ||
| private readonly JsonMapperGenerator _generator; | ||
|
|
||
| public JsonMapperGeneratorTests() | ||
| { | ||
| _typeHandlerRegistry = CreateTypeHandlerRegistry(); | ||
| _generator = new JsonMapperGenerator(_typeHandlerRegistry); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GenerateCode_WithUnixTimestampAndCollectionOfComplexType_ShouldEmitFromUnixTimeConversion() | ||
| { | ||
| // Arrange: A parent type with [UnixTimestamp] DateTimeOffset + List<NestedType> | ||
| // This reproduces the bug where adding a referenced type via collection | ||
| // causes UnixTimestamp handling to break in the generated JSON mapper. | ||
|
|
||
| var nestedProperties = new List<PropertyInfo> | ||
| { | ||
| TestModelBuilders.CreatePropertyInfo("Name", MockSymbolFactory.PrimitiveTypes.String), | ||
| TestModelBuilders.CreatePropertyInfo("Quantity", MockSymbolFactory.PrimitiveTypes.Int32), | ||
| }; | ||
|
|
||
| var nestedType = TestModelBuilders.CreateDynamoTypeInfo( | ||
| "Ingredient", | ||
| "TestNamespace.Ingredient", | ||
| properties: nestedProperties); | ||
|
|
||
| // Create a collection type: List<Ingredient> | ||
| var listMock = MockSymbolFactory.CreateNamedTypeSymbol( | ||
| "List", | ||
| "System.Collections.Generic.List<TestNamespace.Ingredient>", | ||
| "System.Collections.Generic"); | ||
| listMock.Setup(x => x.IsGenericType).Returns(true); | ||
| listMock.Setup(x => x.TypeArguments).Returns(ImmutableArray.Create<Microsoft.CodeAnalysis.ITypeSymbol>(nestedType.Symbol!)); | ||
|
|
||
| var parentProperties = new List<PropertyInfo> | ||
| { | ||
| TestModelBuilders.CreatePropertyInfo("Id", MockSymbolFactory.PrimitiveTypes.String), | ||
| TestModelBuilders.CreatePropertyInfo("Name", MockSymbolFactory.PrimitiveTypes.String), | ||
| TestModelBuilders.CreateUnixTimestampPropertyInfo("UpdatedAt", MockSymbolFactory.PrimitiveTypes.DateTimeOffset), | ||
| TestModelBuilders.CreateCollectionPropertyInfo( | ||
| "Ingredients", | ||
| listMock.Object, | ||
| nestedType.Symbol!, | ||
| isNullable: true), | ||
| }; | ||
|
|
||
| var parentType = TestModelBuilders.CreateDynamoTypeInfo( | ||
| "Recipe", | ||
| "TestNamespace.Recipe", | ||
| properties: parentProperties, | ||
| attributes: new List<AttributeInfo> | ||
| { | ||
| TestModelBuilders.CreateDynamoModelAttribute("RECIPE#<Id>", "METADATA") | ||
| }); | ||
|
|
||
| var types = new List<DynamoTypeInfo> { parentType, nestedType }; | ||
| var context = new GenerationContext(); | ||
|
|
||
| // Act | ||
| var result = _generator.GenerateCode(types, context); | ||
|
|
||
| // Assert: The generated code must contain FromUnixTimeSeconds for the UpdatedAt read path | ||
| await Assert.That(result) | ||
| .Contains("FromUnixTimeSeconds"); | ||
|
|
||
| // Assert: The generated code must contain ToUnixTimeSeconds for the UpdatedAt write path | ||
| await Assert.That(result) | ||
| .Contains("ToUnixTimeSeconds"); | ||
|
|
||
| // Assert: The generated code should NOT directly assign long to DateTimeOffset without conversion | ||
| await Assert.That(result) | ||
| .DoesNotContain("= (DateTimeOffset)"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GenerateCode_WithIgnoredPropertyAndCollectionOfComplexType_ShouldSkipIgnoredProperty() | ||
| { | ||
| // Arrange: A parent type with [Ignore] property + List<NestedType> | ||
| var nestedProperties = new List<PropertyInfo> | ||
| { | ||
| TestModelBuilders.CreatePropertyInfo("Name", MockSymbolFactory.PrimitiveTypes.String), | ||
| }; | ||
|
|
||
| var nestedType = TestModelBuilders.CreateDynamoTypeInfo( | ||
| "Tag", | ||
| "TestNamespace.Tag", | ||
| properties: nestedProperties); | ||
|
|
||
| var listMock = MockSymbolFactory.CreateNamedTypeSymbol( | ||
| "List", | ||
| "System.Collections.Generic.List<TestNamespace.Tag>", | ||
| "System.Collections.Generic"); | ||
| listMock.Setup(x => x.IsGenericType).Returns(true); | ||
| listMock.Setup(x => x.TypeArguments).Returns(ImmutableArray.Create<Microsoft.CodeAnalysis.ITypeSymbol>(nestedType.Symbol!)); | ||
|
|
||
| var ignoredAttr = new IgnoreAttributeInfo { Direction = Models.IgnoreDirection.Always }; | ||
|
|
||
| var parentProperties = new List<PropertyInfo> | ||
| { | ||
| TestModelBuilders.CreatePropertyInfo("Id", MockSymbolFactory.PrimitiveTypes.String), | ||
| TestModelBuilders.CreatePropertyInfo("InternalState", MockSymbolFactory.PrimitiveTypes.String, | ||
| attributes: new List<AttributeInfo> { ignoredAttr }), | ||
| TestModelBuilders.CreateCollectionPropertyInfo( | ||
| "Tags", | ||
| listMock.Object, | ||
| nestedType.Symbol!, | ||
| isNullable: true), | ||
| }; | ||
|
|
||
| var parentType = TestModelBuilders.CreateDynamoTypeInfo( | ||
| "Item", | ||
| "TestNamespace.Item", | ||
| properties: parentProperties, | ||
| attributes: new List<AttributeInfo> | ||
| { | ||
| TestModelBuilders.CreateDynamoModelAttribute("ITEM#<Id>", "METADATA") | ||
| }); | ||
|
|
||
| var types = new List<DynamoTypeInfo> { parentType, nestedType }; | ||
| var context = new GenerationContext(); | ||
|
|
||
| // Act | ||
| var result = _generator.GenerateCode(types, context); | ||
|
|
||
| // Assert: InternalState should NOT appear in the generated write code | ||
| await Assert.That(result) | ||
| .DoesNotContain("\"InternalState\""); | ||
| } | ||
|
|
||
| private static TypeHandlerRegistry CreateTypeHandlerRegistry() | ||
| { | ||
| var registry = new TypeHandlerRegistry(); | ||
| registry.RegisterHandler(new PrimitiveTypeHandler()); | ||
| registry.RegisterHandler(new EnumTypeHandler()); | ||
| registry.RegisterHandler(new DateOnlyTypeHandler()); | ||
| registry.RegisterHandler(new TimeOnlyTypeHandler()); | ||
| registry.RegisterHandler(new DateTimeTypeHandler()); | ||
| registry.RegisterHandler(new UnixTimestampTypeHandler()); | ||
| var collectionHandler = new CollectionTypeHandler(); | ||
| collectionHandler.SetRegistry(registry); | ||
| registry.RegisterHandler(collectionHandler); | ||
| var complexHandler = new ComplexTypeHandler(); | ||
| complexHandler.SetRegistry(registry); | ||
| registry.RegisterHandler(complexHandler); | ||
| return registry; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Im5tu/goa
Length of output: 692
Update
UnixTimestampTypeHandlerto use robust symbol-based type checking.Line 18 currently uses brittle name-only comparisons (
underlyingType.Name == nameof(DateTime) || underlyingType.Name == nameof(DateTimeOffset)). Update it to match the robust type identification inAttributeHandlerRegistry.IsDateTimeType(): useSpecialType.System_DateTimefor DateTime and namespace-qualified name checking for DateTimeOffset.🤖 Prompt for AI Agents