Skip to content

feat(apigateway): support the mode propertyfor how API Gateway resources are updated when using an OpenAPI definition #34198

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

tttol
Copy link
Contributor

@tttol tttol commented Apr 18, 2025

Issue # (if applicable)

N/A

Reason for this change

AWS::ApiGateway::RestApi has a mode property, but the L2 construct for API Gateway (RestApi class) does not currently expose this property.
Therefore, I added the mode property to the RestApiProps interface.

Description of changes

  • Introduced an optional mode property in RestApiProps.
  • Created an enum class RestApiMode to define the possible values for mode. This enum includes two values: merge and overwrite.
  • Added unit and integration tests.
  • Updated the README.md for aws-apigateway.

Describe any new or updated permissions being added

No new IAM permissions are introduced.

Description of how you validated changes

  • packages/aws-cdk-lib/aws-apigateway/test/restapi.test.ts
  • packages/@aws-cdk-testing/framework-integ/test/aws-apigateway/test/integ.restapi.ts

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

@github-actions github-actions bot added the repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK label Apr 18, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team April 18, 2025 07:27
@github-actions github-actions bot added the p2 label Apr 18, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Apr 18, 2025
@tttol
Copy link
Contributor Author

tttol commented Apr 20, 2025

I can't resolve CodeCov error...
https://github.com/aws/aws-cdk/actions/runs/14564289548/job/40851338578?pr=34198

Run actions/github-script@v7
Error: Error message: Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable
    at OidcClient.<anonymous> (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:585:23)
    at Generator.next (<anonymous>)
    at /home/runner/work/_actions/actions/github-script/v7/dist/index.js:522:71
    at new Promise (<anonymous>)
    at __webpack_modules__.8041.__awaiter (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:518:12)
    at OidcClient.getIDToken (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:571:16)
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:421:46)
    at Generator.next (<anonymous>)
    at /home/runner/work/_actions/actions/github-script/v7/dist/index.js:133:71
    at new Promise (<anonymous>)
Error: Unhandled error: Error: Error message: Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable

Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Left some minor comments.

Comment on lines 279 to 282
/**
* This property applies only when you use OpenAPI to define your REST API.
* The Mode determines how API Gateway handles resource updates.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this property (mode) be in SpecRestApiProps?

Comment on lines 1693 to 1696

----

This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary? (The same statement is above.)

Copy link
Contributor Author

@tttol tttol Apr 21, 2025

Choose a reason for hiding this comment

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

Oops, It was a simple mistake.

export interface RestApiProps extends RestApiOptions {
export interface RestApiProps extends RestApiBaseProps, ResourceOptions {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an appropriate change. However, it may not be the right thing to do with this PR that adds the mode property. As a type, it should be safe since it is compatible with the original type, but we might want to avoid the change just in case in this PR.

* Specifies how API Gateway handles resource updates when importing an OpenAPI definition.
* This property applies only when you use OpenAPI to define your REST API.
*/
export enum RestApiMode {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to bring it below export enum EndpointType.

Comment on lines 919 to 956
test('mode property is set correctly', () => {
// WHEN
const apiWithOverwrite = new apigw.RestApi(stack, 'api-overwrite', {
mode: apigw.RestApiMode.OVERWRITE,
});
apiWithOverwrite.root.addMethod('GET');

const apiWithMerge = new apigw.RestApi(stack, 'api-merge', {
mode: apigw.RestApiMode.MERGE,
});
apiWithMerge.root.addMethod('GET');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api-overwrite',
Mode: 'overwrite',
});

Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api-merge',
Mode: 'merge',
});
});

test('mode property is optional', () => {
// WHEN
const api = new apigw.RestApi(stack, 'api');
api.root.addMethod('GET');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api',
});
// Mode should not be present in the template when not specified
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Mode: Match.absent(),
});
});
Copy link
Contributor

@go-to-k go-to-k Apr 21, 2025

Choose a reason for hiding this comment

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

We can change like this:

Suggested change
test('mode property is set correctly', () => {
// WHEN
const apiWithOverwrite = new apigw.RestApi(stack, 'api-overwrite', {
mode: apigw.RestApiMode.OVERWRITE,
});
apiWithOverwrite.root.addMethod('GET');
const apiWithMerge = new apigw.RestApi(stack, 'api-merge', {
mode: apigw.RestApiMode.MERGE,
});
apiWithMerge.root.addMethod('GET');
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api-overwrite',
Mode: 'overwrite',
});
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api-merge',
Mode: 'merge',
});
});
test('mode property is optional', () => {
// WHEN
const api = new apigw.RestApi(stack, 'api');
api.root.addMethod('GET');
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api',
});
// Mode should not be present in the template when not specified
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Mode: Match.absent(),
});
});
test.each([
[apigw.RestApiMode.OVERWRITE, 'overwrite'],
[apigw.RestApiMode.MERGE, 'merge'],
[undefined, undefined],
])('mode property is set (%s)', (mode, expectedMode) => {
// WHEN
const api = new apigw.RestApi(stack, 'api', {
mode,
});
api.root.addMethod('GET');
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
Name: 'api',
Mode: expectedMode ?? Match.absent(),
});
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion!

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Apr 21, 2025
@go-to-k
Copy link
Contributor

go-to-k commented Apr 21, 2025

I can't resolve CodeCov error...

The PR has been merged, but still seems to have that problem. (I've seen that problem in other PRs as well.)

In this PR, it's good to not worry about it for now.

@@ -66,6 +70,24 @@ book.addMethod('GET');
book.addMethod('DELETE');
```

When using OpenAPI to define your REST API, you can control how API Gateway handles resource updates using the `mode` property. Valid values are:
Copy link
Contributor

Choose a reason for hiding this comment

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

If this property is applied when using OpenAPI only, it would be good to bring it in the ### OpenAPI Definition, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, moved this contents to ## OpenAPI Definition section 🙋🏻‍♂️

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

(This review is outdated)

@aws-cdk-automation aws-cdk-automation dismissed their stale review April 21, 2025 21:35

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 086bd6a
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. I'll approve after applying the last comments.

Comment on lines +304 to +305
* This property applies only when you use OpenAPI to define your REST API.
* The Mode determines how API Gateway handles resource updates.
Copy link
Contributor

Choose a reason for hiding this comment

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

Moving the property to SpecRestApiProps would make this explanation simpler.

Suggested change
* This property applies only when you use OpenAPI to define your REST API.
* The Mode determines how API Gateway handles resource updates.
* The Mode that determines how API Gateway handles resource updates.

Comment on lines +1086 to +1087
* Specifies how API Gateway handles resource updates when importing an OpenAPI definition.
* This property applies only when you use OpenAPI to define your REST API.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Specifies how API Gateway handles resource updates when importing an OpenAPI definition.
* This property applies only when you use OpenAPI to define your REST API.
* The Mode that determines how API Gateway handles resource updates when importing an OpenAPI definition.

@@ -275,6 +275,7 @@ export interface RestApiProps extends RestApiOptions {
* @default - Metering is disabled.
*/
readonly apiKeySourceType?: ApiKeySourceType;

Copy link
Contributor

Choose a reason for hiding this comment

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

nit.

Suggested change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2 repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants