Skip to content

Commit

Permalink
fix(apigateway): proxy method options are not duplicated to root (aws…
Browse files Browse the repository at this point in the history
…#4192)

* fix(apigateway): proxy method options are not duplicated to root

when adding methods to a proxy resource which is mounted on the api root,
the integration configuration and method options were not used, which means
that a method was added, but only with default values.

* fix type of ProxyResourceOptions

* add allowed-breaking-changes
  • Loading branch information
Elad Ben-Israel authored and mergify[bot] committed Sep 22, 2019
1 parent 960d71f commit 0d235fe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
4 changes: 4 additions & 0 deletions allowed-breaking-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ changed-type:@aws-cdk/aws-dynamodb.Table.tableStreamArn
incompatible-argument:@aws-cdk/aws-apigateway.LambdaRestApi.addModel
incompatible-argument:@aws-cdk/aws-apigateway.Model.<initializer>
incompatible-argument:@aws-cdk/aws-apigateway.RestApi.addModel
incompatible-argument:@aws-cdk/aws-apigateway.ProxyResource.addProxy
incompatible-argument:@aws-cdk/aws-apigateway.Resource.addProxy
incompatible-argument:@aws-cdk/aws-apigateway.ResourceBase.addProxy
incompatible-argument:@aws-cdk/aws-apigateway.IResource.addProxy
22 changes: 12 additions & 10 deletions packages/@aws-cdk/aws-apigateway/lib/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface IResource extends IResourceBase {
* Adds a greedy proxy resource ("{proxy+}") and an ANY method to this route.
* @param options Default integration and method options.
*/
addProxy(options?: ResourceOptions): ProxyResource;
addProxy(options?: ProxyResourceOptions): ProxyResource;

/**
* Defines a new method for this resource.
Expand Down Expand Up @@ -140,7 +140,7 @@ export abstract class ResourceBase extends ResourceConstruct implements IResourc
return new Method(this, httpMethod, { resource: this, httpMethod, integration, options });
}

public addProxy(options?: ResourceOptions): ProxyResource {
public addProxy(options?: ProxyResourceOptions): ProxyResource {
return new ProxyResource(this, '{proxy+}', { parent: this, ...options });
}

Expand Down Expand Up @@ -235,13 +235,7 @@ export class Resource extends ResourceBase {
}
}

export interface ProxyResourceProps extends ResourceOptions {
/**
* The parent resource of this resource. You can either pass another
* `Resource` object or a `RestApi` object here.
*/
readonly parent: IResource;

export interface ProxyResourceOptions extends ResourceOptions {
/**
* Adds an "ANY" method to this resource. If set to `false`, you will have to explicitly
* add methods to this resource after it's created.
Expand All @@ -251,6 +245,14 @@ export interface ProxyResourceProps extends ResourceOptions {
readonly anyMethod?: boolean;
}

export interface ProxyResourceProps extends ProxyResourceOptions {
/**
* The parent resource of this resource. You can either pass another
* `Resource` object or a `RestApi` object here.
*/
readonly parent: IResource;
}

/**
* Defines a {proxy+} greedy resource and an ANY method on a route.
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html
Expand Down Expand Up @@ -280,7 +282,7 @@ export class ProxyResource extends Resource {
// In case this proxy is mounted under the root, also add this method to
// the root so that empty paths are proxied as well.
if (this.parentResource && this.parentResource.path === '/') {
this.parentResource.addMethod(httpMethod);
this.parentResource.addMethod(httpMethod, integration, options);
}
return super.addMethod(httpMethod, integration, options);
}
Expand Down
42 changes: 42 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,48 @@ export = {
test.done();
},

'if proxy is added to root, proxy methods are automatically duplicated (with integration and options)'(test: Test) {
// GIVEN
const stack = new Stack();
const api = new apigw.RestApi(stack, 'api');
const proxy = api.root.addProxy({
anyMethod: false
});
const deleteInteg = new apigw.MockIntegration({
requestParameters: {
foo: 'bar'
}
});

// WHEN
proxy.addMethod('DELETE', deleteInteg, {
operationName: 'DeleteMe'
});

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::Method', {
HttpMethod: 'DELETE',
ResourceId: { Ref: 'apiproxy4EA44110' },
Integration: {
RequestParameters: { foo: "bar" },
Type: 'MOCK'
},
OperationName: 'DeleteMe'
}));

expect(stack).to(haveResource('AWS::ApiGateway::Method', {
HttpMethod: 'DELETE',
ResourceId: { "Fn::GetAtt": [ "apiC8550315", "RootResourceId" ] },
Integration: {
RequestParameters: { foo: "bar" },
Type: 'MOCK'
},
OperationName: 'DeleteMe'
}));

test.done();
},

'getResource': {

'root resource': {
Expand Down

0 comments on commit 0d235fe

Please sign in to comment.