Skip to content

Commit

Permalink
fix(apigateway): defaultChild on RestApi returns the underlying L1 (#…
Browse files Browse the repository at this point in the history
…4318)

* fix(apigateway): defaultChild on RestApi returns the underlying L1

Current calls to defaultChild would throw an exception since it
contains children named 'Resource' and 'Default'.
Explicitly setting the defaultChild value to the CfnRestApi construct.

fixes #3234

* Add tests and fixup documentation

* Add period

* PR feedback
  • Loading branch information
nija-at authored and mergify[bot] committed Oct 2, 2019
1 parent 4ed4d96 commit 53db8bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export class RestApi extends Resource implements IRestApi {
cloneFrom: props.cloneFrom ? props.cloneFrom.restApiId : undefined,
parameters: props.parameters
});
this.node.defaultChild = resource;

this.restApiId = resource.ref;

Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/a
import { App, CfnElement, CfnResource, Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import apigateway = require('../lib');
import { JsonSchemaType, JsonSchemaVersion } from '../lib';
import { CfnRestApi, JsonSchemaType, JsonSchemaVersion } from '../lib';

// tslint:disable:max-line-length

Expand Down Expand Up @@ -100,6 +100,13 @@ export = {
test.done();
},

'defaultChild is set correctly'(test: Test) {
const stack = new Stack();
const api = new apigateway.RestApi(stack, 'my-api');
test.ok(api.node.defaultChild instanceof CfnRestApi);
test.done();
},

'"name" is defaulted to resource physical name'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/core/lib/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export class ConstructNode {
}

/**
* Returns the child construct that has the id `Default` or `Resource"`
* Returns the child construct that has the id `Default` or `Resource"`.
* This is usually the construct that provides the bulk of the underlying functionality.
* Useful for modifications of the underlying construct that are not avialable at the higher levels.
*
* @throws if there is more than one child
* @returns a construct or undefined if there is no default child
*/
Expand Down

0 comments on commit 53db8bc

Please sign in to comment.