-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dotnet): AnonymousObject fails runtime type checks (#3709)
The `AnonymousObject` class is used when a value is passed from JS to .NET through a union or `any` typed return point, and there is not sufficient runtime type information on the value to decisively identify its dynamic type. The `AnonymousObject` class will be converted by the jsii runtime to any interface type implicitly (even if that is technically not correct), and it must hence be allowed through runtime type-checks for interfaces. Essentially, this is a blind spot of the runtime type checks, and should not cause valid code to break. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
- Loading branch information
1 parent
6b8e75d
commit e7fadc0
Showing
15 changed files
with
1,216 additions
and
185 deletions.
There are no files selected for viewing
This file contains 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
4 changes: 2 additions & 2 deletions
4
packages/@jsii/dotnet-runtime/src/Amazon.JSII.Runtime/Deputy/AnonymousObject.cs
This file contains 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
namespace Amazon.JSII.Runtime.Deputy | ||
{ | ||
internal sealed class AnonymousObject : DeputyBase | ||
public sealed class AnonymousObject : DeputyBase | ||
{ | ||
internal AnonymousObject(ByRefValue byRefValue) : base(byRefValue) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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,47 @@ | ||
export interface IOptionA { | ||
doSomething(): string; | ||
} | ||
|
||
export interface IOptionB { | ||
doSomethingElse(): string; | ||
} | ||
|
||
export class UseOptions { | ||
public static provide(which: 'A' | 'B'): IOptionA | IOptionB { | ||
switch (which) { | ||
case 'A': | ||
return new OptionA(); | ||
case 'B': | ||
return new OptionB(); | ||
default: | ||
throw new Error(`Unexpected option: ${which as any}`); | ||
} | ||
} | ||
|
||
public static privideAsAny(which: 'A' | 'B'): any { | ||
return this.provide(which); | ||
} | ||
|
||
public static consume(option: IOptionA | IOptionB): string { | ||
if (option instanceof OptionA) { | ||
return option.doSomething(); | ||
} else if (option instanceof OptionB) { | ||
return option.doSomethingElse(); | ||
} | ||
throw new Error(`Unexpected option: ${option as any}`); | ||
} | ||
|
||
private constructor() {} | ||
} | ||
|
||
class OptionA implements IOptionA { | ||
public doSomething(): string { | ||
return 'A'; | ||
} | ||
} | ||
|
||
class OptionB implements IOptionB { | ||
public doSomethingElse(): string { | ||
return 'B'; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.