Skip to content

Commit

Permalink
fix(apigateway): cannot configure stage for SpecRestApi
Browse files Browse the repository at this point in the history
- Expects generic `RestApiBase` instead of `RestApi` at `BasePathMapping` creation
- Fixes aws#10300
  • Loading branch information
alvyn279 committed Oct 7, 2020
1 parent 0297f31 commit 06a5b21
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Resource, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnBasePathMapping } from './apigateway.generated';
import { IDomainName } from './domain-name';
import { IRestApi, RestApi } from './restapi';
import { IRestApi, RestApiBase } from './restapi';
import { Stage } from './stage';

export interface BasePathMappingOptions {
Expand Down Expand Up @@ -55,7 +55,7 @@ export class BasePathMapping extends Resource {

// if restApi is an owned API and it has a deployment stage, map all requests
// to that stage. otherwise, the stage will have to be specified in the URL.
const stage = props.stage ?? (props.restApi instanceof RestApi
const stage = props.stage ?? (props.restApi instanceof RestApiBase
? props.restApi.deploymentStage
: undefined);

Expand Down
60 changes: 60 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,64 @@ export = {
}));
test.done();
},

'base path mapping configures stage for RestApi creation'(test: Test) {
// GIVEN
const stack = new Stack();
new apigw.RestApi(stack, 'restApiWithStage', {
domainName: {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
},
}).root.addMethod('GET');

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', {
'DomainName': {
'Ref': 'restApiWithStageCustomDomainC4749625',
},
'RestApiId': {
'Ref': 'restApiWithStageD4F931D0',
},
'Stage': {
'Ref': 'restApiWithStageDeploymentStageprodC82A6648',
},
}));

test.done();
},

'base path mapping configures stage for SpecRestApi creation'(test: Test) {
// GIVEN
const stack = new Stack();

const definition = {
key1: 'val1',
};

new apigw.SpecRestApi(stack, 'specRestApiWithStage', {
apiDefinition: apigw.ApiDefinition.fromInline(definition),
domainName: {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
},
}).root.addMethod('GET');

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::BasePathMapping', {
'DomainName': {
'Ref': 'specRestApiWithStageCustomDomain8A36A5C9',
},
'RestApiId': {
'Ref': 'specRestApiWithStageC1492575',
},
'Stage': {
'Ref': 'specRestApiWithStageDeploymentStageprod2D3037ED',
},
}));

test.done();
},
};

0 comments on commit 06a5b21

Please sign in to comment.