diff --git a/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts b/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts index 6026fe3eaecd3..17fa52ee7005c 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts @@ -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 { @@ -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); diff --git a/packages/@aws-cdk/aws-apigateway/test/test.domains.ts b/packages/@aws-cdk/aws-apigateway/test/test.domains.ts index 175b7c0c11dd2..1e611c4601cd4 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.domains.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.domains.ts @@ -443,4 +443,63 @@ 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(); + }, };