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

fix(.net): missing dynamic type checking for collection-nested unions #3720

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,29 @@ public void AnonymousObjectIsValid()
Assert.IsType<AnonymousObject>(anonymousObject);
Assert.Equal("A", UseOptions.Consume(anonymousObject));
}

[Fact(DisplayName = Prefix + nameof(NestedUnion))]
public void NestedUnion()
{
var exception1 = Assert.Throws<System.ArgumentException>(() =>
new ClassWithNestedUnion(new object[] { 1337.42 }));
Assert.Equal("Expected argument unionProperty[0] to be one of: System.Collections.Generic.IDictionary<string, object>, object[]; received System.Double (Parameter 'unionProperty')", exception1.Message);

var exception2 = Assert.Throws<System.ArgumentException>(() =>
new ClassWithNestedUnion(new object[]
{ new object[] { new StructA { RequiredString = "present" }, 1337 } }));
Assert.Equal("Expected argument unionProperty[0][1] to be one of: Amazon.JSII.Tests.CalculatorNamespace.IStructA, Amazon.JSII.Tests.CalculatorNamespace.IStructB; received System.Int32 (Parameter 'unionProperty')", exception2.Message);

var exception3 = Assert.Throws<System.ArgumentException>(() =>
new ClassWithNestedUnion(new object[]
{
new Dictionary<string, object>
{
{ "good", new StructA { RequiredString = "present" } },
{ "bad", "Not a StructA or StructB" }
}
}));
Assert.Equal("Expected argument unionProperty[0][\"bad\"] to be one of: Amazon.JSII.Tests.CalculatorNamespace.IStructA, Amazon.JSII.Tests.CalculatorNamespace.IStructB; received System.String (Parameter 'unionProperty')", exception3.Message);
}
}
}
8 changes: 8 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3074,3 +3074,11 @@ export class ClassWithCollectionOfUnions {
export interface StructWithCollectionOfUnionts {
readonly unionProperty: Array<Record<string, StructA | StructB>>;
}

export class ClassWithNestedUnion {
public constructor(
public unionProperty: Array<
Array<StructA | StructB> | Record<string, StructA | StructB>
>,
) {}
}
132 changes: 131 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,136 @@
],
"symbolId": "lib/compliance:ClassWithMutableObjectLiteralProperty"
},
"jsii-calc.ClassWithNestedUnion": {
"assembly": "jsii-calc",
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.ClassWithNestedUnion",
"initializer": {
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 3079
},
"parameters": [
{
"name": "unionProperty",
"type": {
"collection": {
"elementtype": {
"union": {
"types": [
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "jsii-calc.StructA"
},
{
"fqn": "jsii-calc.StructB"
}
]
}
},
"kind": "map"
}
},
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "jsii-calc.StructA"
},
{
"fqn": "jsii-calc.StructB"
}
]
}
},
"kind": "array"
}
}
]
}
},
"kind": "array"
}
}
}
]
},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 3078
},
"name": "ClassWithNestedUnion",
"properties": [
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 3080
},
"name": "unionProperty",
"type": {
"collection": {
"elementtype": {
"union": {
"types": [
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "jsii-calc.StructA"
},
{
"fqn": "jsii-calc.StructB"
}
]
}
},
"kind": "map"
}
},
{
"collection": {
"elementtype": {
"union": {
"types": [
{
"fqn": "jsii-calc.StructA"
},
{
"fqn": "jsii-calc.StructB"
}
]
}
},
"kind": "array"
}
}
]
}
},
"kind": "array"
}
}
}
],
"symbolId": "lib/compliance:ClassWithNestedUnion"
},
"jsii-calc.ClassWithPrivateConstructorAndAutomaticProperties": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -18001,5 +18131,5 @@
}
},
"version": "3.20.120",
"fingerprint": "w4jjSmxpafX7pHdksFL0F5FZxa/zM6MB8oFCANIU05k="
"fingerprint": "LBLJQQycukWu6zWQmp2/IbKS/Sfd+4e2zWrX+1KA+Aw="
}
Loading