aws / aws/jsii

Typescript union types are not correctly resolved in Java

Open
#3,935 7 comments 0 reactions 0 assignees View on GitHub
bug closed-for-staleness effort/large jsii language/java p1
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

In Typescript `CloudFormationStackArtifact#getAssets` returns `AssetMetadataEntry[]`, where `AssetMetadataEntry = FileAssetMetadataEntry | ContainerImageAssetMetadataEntry`.
In Java this method returns a `List`, 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
1. Define a container image asset in your stack
2. Synthesize the templates
3. 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

Contributor guide

Open the contributing guide

Research direction

Trace CloudFormationStackArtifact#getAssets and MissingContext#getProps through JsiiEngine.nativeToObjRef and the Java runtime's type-resolution path. Reproduce the container-image case, then verify that the object reference identifies the correct union member and that repositoryName can be read without an exception; apply the same check to ContextQueryProperties.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.