Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): Token resolution should occur within the prepare context #3906

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct, Lazy, RemovalPolicy, Resource, Stack } from '@aws-cdk/core';
import { Construct, DefaultTokenResolver, Lazy, RemovalPolicy, Resource, Stack, StringConcat, Tokenization } from '@aws-cdk/core';
import crypto = require('crypto');
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
import { IRestApi } from './restapi';
Expand Down Expand Up @@ -128,12 +128,23 @@ class LatestDeploymentResource extends CfnDeployment {
if (this.hashComponents.length > 0) {
const md5 = crypto.createHash('md5');
this.hashComponents
.map(c => stack.resolve(c))
.map(c => {
try {
// TODO: Remove the code in the try block with next major version release of CDK.
// It's here to be backwards compatible, i.e., prevent LogicalIds to change.
return stack.resolve(c);
} catch (e) {
return Tokenization.resolve(c, {
scope: this,
resolver: new DefaultTokenResolver(new StringConcat()),
preparing: true,
});
}
})
.forEach(c => md5.update(JSON.stringify(c)));

this.overrideLogicalId(this.originalLogicalId + md5.digest("hex"));
}

super.prepare();
}
}
10 changes: 8 additions & 2 deletions packages/@aws-cdk/core/lib/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ export class Tokenization {
*/
public static resolve(obj: any, options: ResolveOptions): any {
return resolve(obj, {
...options,
preparing: false
scope: options.scope,
resolver: options.resolver,
preparing: options.preparing || false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| is problematic for boolean fields (albeit here it will work). Prefer options.preparing !== undefined ? options.preparing : false.

});
}

Expand Down Expand Up @@ -150,6 +151,11 @@ export interface ResolveOptions {
* The resolver to apply to any resolvable tokens found
*/
readonly resolver: ITokenResolver;

/**
* Whether the resolution is being executed during the prepare phase or not.
*/
readonly preparing?: boolean;
}

/**
Expand Down