diff --git a/packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs b/packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs index c37e0af5bd..d806c005fe 100644 --- a/packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs +++ b/packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs @@ -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 map = new Dictionary(); + map.Add("Foo", new Dictionary() { {"Key", 123d}}); + types.AnyMapProperty = map; + var dict = (Dictionary)types.AnyMapProperty["Foo"]; + Assert.Equal(123d, dict["Key"]); + } [Fact(DisplayName = Prefix + nameof(DynamicTypes))] public void DynamicTypes() diff --git a/packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/Converters/FrameworkToJsiiConverter.cs b/packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/Converters/FrameworkToJsiiConverter.cs index 8d5f2e1e50..2f02b6ec52 100644 --- a/packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/Converters/FrameworkToJsiiConverter.cs +++ b/packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/Converters/FrameworkToJsiiConverter.cs @@ -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 could be a map 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;