Skip to content

Commit

Permalink
fix(apigateway): validation for path parts does not allow creation of…
Browse files Browse the repository at this point in the history
… resources beginning with dollar sign (#27619)

This PR adjusts the validation rules for path parts in resources created using the APIGateway library to allow the use of dollar mark in pathParts, as requested in #27083. This enables the creation of resources with paths such as $test(from the reproduction steps in the related issue).

An existing resource unit test ('url for a resource') has been updated to ensure that a dollar mark in a pathPart does not throw an error and properly reflects in the output of RestApi.urlForPath. The integration test integ.restapi has also been updated, wherein the appliances resource now has a path of $appliances:all instead of appliances:all.

Closes #27083.

#### All Submissions:
- [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)
#### Adding new Unconventional Dependencies:
- [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)
#### New Features
- [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
  - [x] Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
  • Loading branch information
lawofcycles authored Oct 26, 2023
1 parent 22a3234 commit a8f5326
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"myapiDeployment92F2CB4993c0f175ba8d5964f5e1cc7bc64fe6e6": {
"myapiDeployment92F2CB49069d16df09c59427069eb74581a0403d": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "beta stage",
Expand All @@ -66,8 +66,8 @@
}
},
"DependsOn": [
"myapiv1appliancesallGETC4DF552D",
"myapiv1appliancesallCF8C6A16",
"myapiv1appliancesallGETB8EB1B77",
"myapiv1appliancesallD279897B",
"myapiv1booksGETC6B996D0",
"myapiv1booksPOST53E2832E",
"myapiv1books1D4BE6C1",
Expand All @@ -86,7 +86,7 @@
"CacheClusterEnabled": true,
"CacheClusterSize": "0.5",
"DeploymentId": {
"Ref": "myapiDeployment92F2CB4993c0f175ba8d5964f5e1cc7bc64fe6e6"
"Ref": "myapiDeployment92F2CB49069d16df09c59427069eb74581a0403d"
},
"Description": "beta stage",
"MethodSettings": [
Expand Down Expand Up @@ -290,19 +290,19 @@
}
}
},
"myapiv1appliancesallCF8C6A16": {
"myapiv1appliancesallD279897B": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Ref": "myapiv113487378"
},
"PathPart": "appliances:all",
"PathPart": "$appliances:all",
"RestApiId": {
"Ref": "myapi4C7BF186"
}
}
},
"myapiv1appliancesallGETC4DF552D": {
"myapiv1appliancesallGETB8EB1B77": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "NONE",
Expand All @@ -311,7 +311,7 @@
"Type": "MOCK"
},
"ResourceId": {
"Ref": "myapiv1appliancesallCF8C6A16"
"Ref": "myapiv1appliancesallD279897B"
},
"RestApiId": {
"Ref": "myapi4C7BF186"
Expand Down Expand Up @@ -682,7 +682,7 @@
}
},
"DependsOn": [
"myapiv1appliancesallGETC4DF552D",
"myapiv1appliancesallGETB8EB1B77",
"myapiv1booksGETC6B996D0",
"myapiv1booksPOST53E2832E",
"myapiv1toysGET7348114D",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Test extends cdk.Stack {
toys.addMethod('POST');
toys.addMethod('PUT');

const appliances = v1.addResource('appliances:all');
const appliances = v1.addResource('$appliances:all');
appliances.addMethod('GET');

const books = v1.addResource('books');
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-apigateway/lib/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ function validateResourcePathPart(part: string) {
}
}

if (!/^[a-zA-Z0-9:\.\_\-]+$/.test(part)) {
throw new Error(`Resource's path part only allow [a-zA-Z0-9:._-], an optional trailing '+'
if (!/^[a-zA-Z0-9:\.\_\-\$]+$/.test(part)) {
throw new Error(`Resource's path part only allow [a-zA-Z0-9:._-$], an optional trailing '+'
and curly braces at the beginning and the end: ${part}`);
}
}
17 changes: 17 additions & 0 deletions packages/aws-cdk-lib/aws-apigateway/test/resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ describe('resource', () => {
const aResource = api.root.addResource('a');
const cResource = aResource.addResource('b').addResource('c');
const colonResource = cResource.addResource('d:e');
const dollarResource = cResource.addResource('$d');

// THEN
expect(stack.resolve(api.urlForPath(aResource.path))).toEqual({
Expand Down Expand Up @@ -247,6 +248,22 @@ describe('resource', () => {
],
],
});
expect(stack.resolve(api.urlForPath(dollarResource.path))).toEqual({
'Fn::Join': [
'',
[
'https://',
{ Ref: 'apiC8550315' },
'.execute-api.',
{ Ref: 'AWS::Region' },
'.',
{ Ref: 'AWS::URLSuffix' },
'/',
{ Ref: 'apiDeploymentStageprod896C8101' },
'/a/b/c/$d',
],
],
});

});

Expand Down

0 comments on commit a8f5326

Please sign in to comment.