aws / aws/aws-cdk

aws-cdk-lib:apigateway_v1:CfnMethod: Using a Method's Integrations (or rather its IntegrationProperty) requires CDK code reading.

Open
#24,832 5 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-apigateway bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

Parsing existing cloudformation to a CDK construct. It contains APIGateways, Resources and Methods. The goal is to get their Integration information. The return type of CfnMethod.getIntegration() is "class software.amazon.jsii.JsiiObject", but the compiler sees it as "Object". I tried to first cast it to Integration, thinking I would be later able to check if the type of it really is http,aws or lambda integration.
But the first step failed:
`class software.amazon.jsii.JsiiObject cannot be cast to class software.amazon.awscdk.services.apigateway.Integration `

### Expected Behavior

It should work according to docs:
software.amazon.awscdk.services.apigateway
Class HttpIntegration
java.lang.Object
software.amazon.jsii.JsiiObject
software.amazon.awscdk.services.apigateway.Integration
software.amazon.awscdk.services.apigateway.HttpIntegration

### Current Behavior

I'd expect to be able to cast it to the proper class.

### Reproduction Steps

But we played on using an existing CfnMethod worker:
```
JsiiObject jsiiObject = (JsiiObject) worker.getIntegration();
jsiiObject.$jsii$toJson()
```
outcome:
`{"$jsii.byref":"Object@10049","$jsii.interfaces":["aws-cdk-lib.aws_apigateway.CfnMethod.IntegrationProperty"]}`
How come? So why isn't this method called "getIntegrationProperty()"? Maybe if I cast to it?
`IntegrationProperty intProp = (IntegrationProperty) worker.getIntegration();`
outcome:
`class software.amazon.jsii.JsiiObject cannot be cast to class software.amazon.awscdk.services.apigateway.CfnMethod$IntegrationProperty`
????????
After 1 more hour of fumbling around another guy found this:
`IntegrationProperty integrationProp = UnsafeCast.unsafeCast(jsiiObject, IntegrationProperty.class);`
Worked like a charm. I can access all the methods I need, like:
```
System.out.println(integrationProp.getClass());
System.out.println(integrationProp.getType());
```
outcome:
```
class software.amazon.awscdk.services.apigateway.CfnMethod$IntegrationProperty$Jsii$Proxy
AWS_PROXY
```
But is this really the expected developer experience? I cannot really give you code to test it, as it requires an existing cloudformation. I tried it with 3 different stacks and the behavior is the same.

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.69 and 2.70 is the same

### Framework Version

_No response_

### Node.js Version

Node.js v18.13.0

### OS

Windows 11 Pro: 10.0.22621 Build 22621

### Language

Java

### Language Version

Java (8)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by examining the generated Java bindings for CfnMethod.getIntegration and IntegrationProperty, then reproduce the behavior with the existing CloudFormation-based stack described in the issue. Done means the integration can be accessed through the documented Java API without UnsafeCast, with regression coverage for the returned type.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, java, typescript
Domain
api, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.