Typescript union types are not correctly resolved in Java
See original GitHub issueIn Typescript CloudFormationStackArtifact#getAssets
returns AssetMetadataEntry[]
, where AssetMetadataEntry = FileAssetMetadataEntry | ContainerImageAssetMetadataEntry
.
In Java this method returns a List<Object>
, which is, actually, a list of JSII objects. The JSII objects are supposed to reference FileAssetMetadataEntry
and ContainerImageAssetMetadataEntry
objects from JSII runtime, however the reference type is always FileAssetMetadataEntry
:
final Object asset = stackArtifact.getAssets().get(0);
// the reference interface will always be FileAssetMetadataEntry even though it's actually ContainerImageAssetMetadataEntry
JsiiEngine.getInstance().nativeToObjRef(asset).getInterfaces()
The issue is also actual for software.amazon.awscdk.cloudassembly.schema.MissingContext#getProps
. It is supposed to return ContextQueryProperties
which is a union type in Typescript, however, the JSII object reference is always of type AmiContextQuery
(looks like it always takes the first type in the union type definition as both FileAssetMetadataEntry
and AmiContextQuery
are defined first.
Reproduction Steps
- Define a container image asset in your stack
- Synthesize the templates
- Try to access
repositoryName
of the asset:
Object asset = stackArtifact.getAssets().get(0);
JsiiEngine jsiiEngine = JsiiEngine.getInstance();
JsiiObjectRef objectRef = jsiiEngine.nativeToObjRef(asset);
JsiiClient client = jsiiEngine.getClient();
String packaging = client.getPropertyValue(objectRef, "packaging").asText();
assert packaging.equals("container-image");
String repositoryName = client.getPropertyValue(objectRef, "repositoryName").asText(); //causes an exception
Error Log
Error: Type Object or interface(s) @aws-cdk/cloud-assembly-schema.FileAssetMetadataEntry doesn't have a property 'repositoryName'
at Kernel._typeInfoForProperty (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:8205:19)
at Kernel.get (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7642:25)
at KernelHost.processRequest (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7388:28)
at KernelHost.run (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7328:14)
at Immediate._onImmediate (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7331:37)
at processImmediate (internal/timers.js:456:21)
Environment
- CLI Version : 1.41.0
- Framework Version: 1.41.0
- OS : macOs Catalina
- Language : Java
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Thank you! Just in case, I’ve created a repo reproducing the issue: https://github.com/RoKish/cdk-union-type-bug.
I’ll mention here that we have an open RFC about the type unions problem => https://github.com/aws/aws-cdk-rfcs/issues/193