Skip to content

feat(apigateway): add mode property for SpecRestApi #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

Merged
merged 18 commits into from
Apr 24, 2025

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 (SpecRestApi class) does not currently expose this property.
Therefore, I added the mode property to the SpecRestApiProps interface.

Description of changes

  • Introduced an optional mode property in SpecRestApiProps.
  • 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.spec-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.

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.

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.

LGTM.

It might be good to simplify the PR title, like: feat(apigateway): add `mode` property for SpecRestApi.

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.

Sorry, There was something I had missed...

Comment on lines 1607 to 1624
You can control how API Gateway handles resource updates using the `mode` property. Valid values are:

* `overwrite` - The new API definition replaces the existing one. The existing API identifier remains unchanged.
* `merge` - The new API definition is merged with the existing API.

If you don't specify this property, a default value is chosen:
* For REST APIs created before March 29, 2021, the default is `overwrite`
* For REST APIs created after March 29, 2021, the new API definition takes precedence, but any container types such as endpoint configurations and binary media types are merged with the existing API.

Use the default mode to define top-level RestApi properties in addition to using OpenAPI.
Generally, it's preferred to use API Gateway's OpenAPI extensions to model these properties.

```ts
const api = new apigateway.SpecRestApi(this, 'books-api', {
apiDefinition: apigateway.ApiDefinition.fromAsset('path-to-file.json'),
mode: apigateway.RestApiMode.MERGE
});
```
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Sorry. I noticed that the description should be below the following statement:

**Note:** When starting off with an OpenAPI definition using `SpecRestApi`, it is not possible to configure some
properties that can be configured directly in the OpenAPI specification file. This is to prevent people duplication
of these properties and potential confusion.

And it might be good to change the following sentence to be more natural... (Because the description is related to the above statements, or will be in conflict.)

You can control how API Gateway handles resource updates using the `mode` property. Valid values are:

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 fixed. Added However, .

@tttol tttol changed the title feat(apigateway): support the mode propertyfor how API Gateway resources are updated when using an OpenAPI definition feat(apigateway): add mode propertyto SpecRestApi Apr 22, 2025
@go-to-k
Copy link
Contributor

go-to-k commented Apr 22, 2025

feat(apigateway): add mode propertyto SpecRestApi

😂
propertyto -> property for SpecRestApi

(Or property to SpecRestApiProps is also be good, however I prefer property for SpecRestApi)

@tttol tttol changed the title feat(apigateway): add mode propertyto SpecRestApi feat(apigateway): add mode property to SpecRestApi Apr 22, 2025
@tttol
Copy link
Contributor Author

tttol commented Apr 22, 2025

Umm... I added a space between "property" and "to", but the space is not shown 🤔
image

Adding two spaces worked fine.

@go-to-k
Copy link
Contributor

go-to-k commented Apr 22, 2025

Umm... I added a space between "property" and "to", but the space is not shown 🤔

I think it's because you're typing in double-byte (full-width) characters.

So copy and paste this as is. (And it is changed with property for instead of property to)

feat(apigateway): add `mode` property for `SpecRestApi`

title

@tttol tttol changed the title feat(apigateway): add mode property to SpecRestApi feat(apigateway): add mode property for SpecRestApi Apr 22, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 22, 2025
@shikha372 shikha372 self-assigned this Apr 22, 2025
shikha372
shikha372 previously approved these changes Apr 23, 2025
Copy link
Contributor

@shikha372 shikha372 left a comment

Choose a reason for hiding this comment

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

Thank you @tttol for contributing to this PR, and also @go-to-k for reviewing it thoroughly.

Copy link
Contributor

mergify bot commented Apr 23, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Apr 23, 2025

This pull request has been removed from the queue for the following reason: pull request branch update failed.

The pull request can't be updated.

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio requeue comment.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 23, 2025
Copy link
Contributor

mergify bot commented Apr 23, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot dismissed shikha372’s stale review April 24, 2025 00:10

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Apr 24, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit feadd8c into aws:main Apr 24, 2025
16 of 17 checks passed
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2025
@tttol tttol deleted the add-restapiprops branch April 27, 2025 07:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

4 participants