Skip to content

Commit 53db8bc

Browse files
nija-atmergify[bot]
authored andcommitted
fix(apigateway): defaultChild on RestApi returns the underlying L1 (#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
1 parent 4ed4d96 commit 53db8bc

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export class RestApi extends Resource implements IRestApi {
230230
cloneFrom: props.cloneFrom ? props.cloneFrom.restApiId : undefined,
231231
parameters: props.parameters
232232
});
233+
this.node.defaultChild = resource;
233234

234235
this.restApiId = resource.ref;
235236

packages/@aws-cdk/aws-apigateway/test/test.restapi.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/a
22
import { App, CfnElement, CfnResource, Stack } from '@aws-cdk/core';
33
import { Test } from 'nodeunit';
44
import apigateway = require('../lib');
5-
import { JsonSchemaType, JsonSchemaVersion } from '../lib';
5+
import { CfnRestApi, JsonSchemaType, JsonSchemaVersion } from '../lib';
66

77
// tslint:disable:max-line-length
88

@@ -100,6 +100,13 @@ export = {
100100
test.done();
101101
},
102102

103+
'defaultChild is set correctly'(test: Test) {
104+
const stack = new Stack();
105+
const api = new apigateway.RestApi(stack, 'my-api');
106+
test.ok(api.node.defaultChild instanceof CfnRestApi);
107+
test.done();
108+
},
109+
103110
'"name" is defaulted to resource physical name'(test: Test) {
104111
// GIVEN
105112
const stack = new Stack();

packages/@aws-cdk/core/lib/construct.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ export class ConstructNode {
198198
}
199199

200200
/**
201-
* Returns the child construct that has the id `Default` or `Resource"`
201+
* Returns the child construct that has the id `Default` or `Resource"`.
202+
* This is usually the construct that provides the bulk of the underlying functionality.
203+
* Useful for modifications of the underlying construct that are not avialable at the higher levels.
204+
*
202205
* @throws if there is more than one child
203206
* @returns a construct or undefined if there is no default child
204207
*/

0 commit comments

Comments
 (0)