aws / aws/aws-cdk

RestApi: CDK is duplicating methods that added to a proxy by adding them to the root resource

Open
#25,849 7 comments 0 reactions 0 assignees View on GitHub
bug effort/medium p3 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

Having RestApi

```
const restApi = new RestApi(this, 'rest-api', {
restApiName: `rest-api`,
deploy: true,
deployOptions: { ... },
cloudWatchRole: true,
endpointConfiguration: {
types: [EndpointType.REGIONAL],
},
});
```

When you add proxy resource to it like so and then you add a method(example shows OPTIONS but it happens with other methods too) to the created proxy

```
const proxy = restApi.root.addProxy({
anyMethod: false,
});

proxy.addMethod('OPTIONS');
```

Cloud formation template that is created has this method created twice, one attached to a proxy and one more attached to the root of the RestApi. That when deployed creates 2 methods instead of 1, as per template.

```
"publicgatewaypublicgatewayapiproxyOPTIONS130DF52D": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"HttpMethod": "OPTIONS",
"ResourceId": {
"Ref": "publicgatewaypublicgatewayapiproxy91EF2EEC"
},
"RestApiId": {
"Ref": "publicgatewaypublicgatewayapi060C6F16"
},
"AuthorizationType": "NONE",
"Integration": {
"Type": "MOCK"
}
},
"Metadata": {
"aws:cdk:path": "dev-eu-west-1-main-adopt-routing/public-gateway/public-gateway-api/Default/{proxy+}/OPTIONS/Resource"
}
},
"publicgatewaypublicgatewayapiOPTIONS68011C19": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"HttpMethod": "OPTIONS",
"ResourceId": {
"Fn::GetAtt": [
"publicgatewaypublicgatewayapi060C6F16",
"RootResourceId"
]
},
"RestApiId": {
"Ref": "publicgatewaypublicgatewayapi060C6F16"
},
"AuthorizationType": "NONE",
"Integration": {
"Type": "MOCK"
}
},
"Metadata": {
"aws:cdk:path": "dev-eu-west-1-main-adopt-routing/public-gateway/public-gateway-api/Default/OPTIONS/Resource"
}
},
```

When you would add some other path resource before proxy i.e.

```
const res = restApi.root.addResource('test');

const proxy = res.addProxy({
anyMethod: false,
});

proxy.addMethod('OPTIONS');
```

It would create only one method as expected.

Same issue occurs when you import existing resource i.e.

```
const res = Resource.fromResourceAttributes(this, 'resource', {
restApi: restApiResource,
resourceId: importedResourceId,
path: '/',
});
```

It could point at root or even further down the path. And if you add proxy directly to it and some method

```
const proxy = res.root.addProxy({
anyMethod: false,
});

proxy.addMethod('OPTIONS');
```

It is going to duplicate that method also to the root of the restApi.
And again if you add some additional path before adding proxy it will only create one method, as expected.

### Expected Behavior

Cloud formation template should only have one method added, to the proxy resource and not to the root of the RestApi.

### Current Behavior

Cloud formation template that is created has this method created twice, one attached to a proxy and one more attached to the root of the RestApi.

### Reproduction Steps

Added to the description of the bug

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.82.0 (build 3a8648a)

### Framework Version

_No response_

### Node.js Version

v14.19.0

### OS

macOS 12.6 (M1 Pro)

### Language

Typescript

### Language Version

Typescript (4.9.5)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue using the RestApi/root and Resource.fromResourceAttributes examples in the report, then synthesize the CloudFormation template. Compare the generated AWS::ApiGateway::Method resources for a proxy directly under the root versus one under an added path. Done means each explicitly added method appears only on its proxy resource, not also on the API root.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, backend
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.