-
Notifications
You must be signed in to change notification settings - Fork 244
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
Unable to use Role.import_ in Java CDK #320
Comments
The issue appears to be that A minimal repro is: package software.amazon;
import software.amazon.awscdk.App;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.services.iam.ImportedRoleProps;
import software.amazon.awscdk.services.iam.Role;
public final class Jsii320
{
public static void main(final String[] args)
{
final App app = new App();
final Stack stack = new Stack(app, "TestStack");
Role.import_(
stack,
"ImportedRole",
ImportedRoleProps.builder()
.withRoleArn("arn:aws:iam:::role/ImportedRole")
.build()
);
app.run();
}
} |
The Java jsii runtime should be constructing an interace proxy for |
And it should be a compliance test for jsii. |
I reckon this is the tracking issue for that... The issue here is really that the This could be solved either by having the kernel return an ObjID with the correct type name, or by having the Java runtime ignore the type fragment of the FQN (merely treating it as a hint) and just generating an object proxy if the declared type is an interface. |
Fine, but the codegen has the declared type so it could use that as well. |
Agreed. I'm leaning towards a fix that makes the Java runtime not trust the type part of the ObjID so much, because that's a much easier fix, and also because that'd possibly make some use-cases quite convoluted (same object returned under various different interfaces it implements... ouch). |
If we go and trust the declared type completely, there are some edge cases that we can no longer downcast the object even if that would be legal (if jsii would return an exported subtype, or if we passed a subtype from java -> jsii -> java). We should either:
|
This test covers the behavior of the JSII runtimes when a method is declared to return an interface type, and returns an instance of a private (un-exported) type that implements the interface while extending an exported type. This has been seen to cause issues in the Java runtime, for example, as the JSII kernel will return an ObjID with a type fragment that refers to the exported super-class, and not the interface type.
Adding a compliance test around this issue revealed that .NET/C# is also affected by this problem. |
PR #321 fixes this issue, 🎉 |
When an un-exported type that extends an exported type is returned with an interface as the declared type, the JSII kernel used to return a ref with the FQN of the exported supertype, instead of correctly wrapping the instance in a proxy of the interface type as it should have. --- Adds a test that covers the behavior of the JSII runtimes when a method is declared to return an interface type, and returns an instance of a private (un-exported) type that implements the interface while extending an exported type. This has been seen to cause issues in the Java runtime, for example, as the JSII kernel will return an ObjID with a type fragment that refers to the exported super-class, and not the interface type. --- Fixes #302
I'm attempting to call
Role.import_(...)
in my Java CDK application, but I'm getting aClassCastException
from the CDK library. Apparently the result that's being returned from the Jsii runtime is aConstruct
which cannot be cast to anIRole
:The underlying TypeScript (github) declares that it returns
IRole
and explicitly returns anImportedRole
, which implementsIRole
, but also extendsConstruct
(github). While walking through the Java code in the debugger I see that the result from Jsii'stoString()
shows "ImportedRole", but it's type isConstruct
.The text was updated successfully, but these errors were encountered: