Skip to content

Commit 04bab47

Browse files
Hamza Assyadmergify[bot]
authored andcommitted
fix(dotnet): Fix property set for nested Dictionaries (#736)
* fix(dotnet) Fix property set for complex Dictionaries * Explicit assert type
1 parent 934b5c8 commit 04bab47

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ public void CollectionTypes()
9191
types.MapProperty = map;
9292
Assert.Equal((double) 123, types.MapProperty["Foo"].Value);
9393
}
94+
95+
[Fact(DisplayName = Prefix + nameof(ComplexCollectionTypes))]
96+
public void ComplexCollectionTypes()
97+
{
98+
// See https://github.com/aws/aws-cdk/issues/2496
99+
AllTypes types = new AllTypes();
100+
// complex map
101+
IDictionary<string, object> map = new Dictionary<string, object>();
102+
map.Add("Foo", new Dictionary<string, object>() { {"Key", 123d}});
103+
types.AnyMapProperty = map;
104+
var dict = (Dictionary<string, object>)types.AnyMapProperty["Foo"];
105+
Assert.Equal(123d, dict["Key"]);
106+
}
94107

95108
[Fact(DisplayName = Prefix + nameof(DynamicTypes))]
96109
public void DynamicTypes()

packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/Converters/FrameworkToJsiiConverter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,16 @@ protected override bool TryConvertMap(IReferenceMap referenceMap, TypeReference
263263
foreach (string key in keys)
264264
{
265265
object element = indexer.GetValue(value, new object[] {key});
266-
if (!TryConvert(elementType, referenceMap, element, out object convertedElement))
266+
267+
TypeReference childElementType = InferType(referenceMap, element);
268+
269+
// We should not pass the parent element type as we are in a map
270+
// A map<string, object> could be a map<string, map<string, object> etc
271+
// If we pass the parent referenceMap then it will try to convert it as Any
272+
// So by inferring the child element type we are always converting the correct type.
273+
// See https://github.com/aws/aws-cdk/issues/2496
274+
275+
if (!TryConvert(childElementType, referenceMap, element, out object convertedElement))
267276
{
268277
result = null;
269278
return false;

0 commit comments

Comments
 (0)