Skip to content

Commit

Permalink
fix(dotnet): Fix property set for nested Dictionaries (#736)
Browse files Browse the repository at this point in the history
* fix(dotnet) Fix property set for complex Dictionaries

* Explicit assert type
  • Loading branch information
Hamza Assyad authored and mergify[bot] committed Aug 26, 2019
1 parent 934b5c8 commit 04bab47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public void CollectionTypes()
types.MapProperty = map;
Assert.Equal((double) 123, types.MapProperty["Foo"].Value);
}

[Fact(DisplayName = Prefix + nameof(ComplexCollectionTypes))]
public void ComplexCollectionTypes()
{
// See https://github.com/aws/aws-cdk/issues/2496
AllTypes types = new AllTypes();
// complex map
IDictionary<string, object> map = new Dictionary<string, object>();
map.Add("Foo", new Dictionary<string, object>() { {"Key", 123d}});
types.AnyMapProperty = map;
var dict = (Dictionary<string, object>)types.AnyMapProperty["Foo"];
Assert.Equal(123d, dict["Key"]);
}

[Fact(DisplayName = Prefix + nameof(DynamicTypes))]
public void DynamicTypes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,16 @@ protected override bool TryConvertMap(IReferenceMap referenceMap, TypeReference
foreach (string key in keys)
{
object element = indexer.GetValue(value, new object[] {key});
if (!TryConvert(elementType, referenceMap, element, out object convertedElement))

TypeReference childElementType = InferType(referenceMap, element);

// We should not pass the parent element type as we are in a map
// A map<string, object> could be a map<string, map<string, object> etc
// If we pass the parent referenceMap then it will try to convert it as Any
// So by inferring the child element type we are always converting the correct type.
// See https://github.com/aws/aws-cdk/issues/2496

if (!TryConvert(childElementType, referenceMap, element, out object convertedElement))
{
result = null;
return false;
Expand Down

0 comments on commit 04bab47

Please sign in to comment.