Skip to content

(aws-apigateway): enabled property of ApiKeyProps is ignored #18402

@flavioleggio

Description

@flavioleggio

What is the problem?

When a new ApiKey is created the enabled property is ignored and che key is always enabled.

Reproduction Steps

Create a sample app with a stack containing a new ApiKey with enabled set to false

new ApiKey(this, "MyApiKey", {
  enabled: false,
  value: "arandomstringwithmorethantwentycharacters"
});

What did you expect to happen?

A new api key is created and enabled is set to false.

What actually happened?

The ApiKey resource gets synthesized with the Enabled property set to true, something like the following.

"MyApiKey8A246B6F": {
  "Type": "AWS::ApiGateway::ApiKey",
  "Properties": {
    "Enabled": true,
    "Value": "arandomstringwithmorethantwentycharacters"
  },
  "Metadata": {
    "aws:cdk:path": "MyApp/MyApiKey/Resource"
  }
}

CDK CLI Version

2.5.0

Framework Version

1.125.0

Node.js Version

v16.13.0

OS

Linux Mint 20.3

Language

Typescript

Language Version

4.1.3

Other information

I guess this is caused by this portion of the ApiKey constructor code, where the "OR operator" strategy is used to set the default value of enabled when that is undefined.

const resource = new CfnApiKey(this, 'Resource', {
  customerId: props.customerId,
  description: props.description,
  enabled: props.enabled || true, // <--- this will always return true!
  generateDistinctId: props.generateDistinctId,
  name: this.physicalName,
  stageKeys: this.renderStageKeys(props.resources),
  value: props.value,
});

With boolean properties this strategy does not work. Here one would use the nullish coalescing operator like this

props.enabled ?? true

or a more explicit ternary operator like this:

props.enabled !== undefined ? props.enabled : true

This is a really easy task. I managed to make it work with the ternary operator by editing the compiled js file in my node_modules.

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-apigatewayRelated to Amazon API GatewaybugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions