-
Notifications
You must be signed in to change notification settings - Fork 246
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
(java) anyof union incorrect $jsii.interfaces in JsiiObjectRef #3726
Comments
Ah yes... In this case, the value has multiple serialization options (one per struct in the union), and struct serialization happens by-reference, and always succeeds... This is where the interface list assignment is made, and this actually causes a bad assignment to be made here... |
Adds an `UnsafeCast.unsafeCast` method to the Jsii runtime for Java that allows unsafely casting an instance to a managed interface of the user's choice. This can be useful when dealing with type unions composed of interfaces or structs, as there is otherwise no way to convert the instance without jumping through hoops. Fixes #3726 (sort of)
If I was to fix the "incorrect fqn assignment", you'd still be stuck. The only "real" solution I can offer at this stage is a utility to user-land cast instances to a different interface type... so I made a PR to this effect. |
I'll mention here that we have an open RFC about the type unions problem => aws/aws-cdk-rfcs#193 |
Thanks a lot, I'll use it for now. |
|
Describe the bug
Initially added to the bug under
aws/aws-cdk
, but this seems to be the right place.For
anyOf
union types like this one:The Java generated code looks like this:
Where Object is a
JsiiObject
that can only be converted to the first type in the unionAwsCloudFormationStackProperties
otherwise it throws an exception. To reproduce this issue:Which throws:
When I looked under the hood, the
Kernel.get
method first takes theJsiiObject
and converts it into aJsiiObjectRef
which it then tries to expand and throws the exception. The returnedJsiiObjectRef
is the problem as the token interfaces$jsii.interfaces
is pointing toAwsCloudFormationStackProperties
instead of the expectedAssetManifestProperties
.If anyone is interested in a workaround, I wrote a utillity class residing in the aws package space and simply override the
JsiiObjectRef
token interface to match the correct class like so:The source code is here
Expected Behavior
Expected to be able to use
anyof
union from JavaCurrent Behavior
Throws exception, can only convert to first type in the union
Reproduction Steps
manifest.json:
Possible Solution
No response
Additional Information/Context
No response
SDK version used
jsii-runtime 1.65.0 cdk-cloud-assembly 1.167.0
Environment details (OS name and version, etc.)
macos 12.2.1
The text was updated successfully, but these errors were encountered: