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

(CDK CLI): cdk migrate produces incorrect types and casing #27473

Closed
rogerchi opened this issue Oct 10, 2023 · 5 comments
Closed

(CDK CLI): cdk migrate produces incorrect types and casing #27473

rogerchi opened this issue Oct 10, 2023 · 5 comments
Labels
bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@rogerchi
Copy link
Contributor

Describe the bug

The generated CfnRestApi construct from a cdk migrate command incorrectly translates some of the property keys.

  • body.paths.{resource}.{verb}.responses.{code} - code should be a string, but translated as number
  • body.paths.{resource}.{verb}.responses.{code}.headers - all headers here have been converted to camelCase instead of remaining kebab-case
  • body.paths.{resource}.xAmazonApigatewayIntegration - should be x-amazon-apigateway-integration
  • body.paths.{resource}.xAmazonApigatewayIntegration.requestTemplates - the templates in here are camelCased (e.g. applicationJson, but should be string keys like application/json
  • body.paths.{resource}.xAmazonApigatewayIntegration.responses.default.responseTemplates - the templates in here are camelCased (e.g. applicationJson, but should be string keys like application/json
  • body.paths.{resource}.xAmazonApigatewayIntegration.responses.default.responseParameters - the properties in here look to refer to a method variable, e.g. method.response.header.AccessControlAllowOrigin: '\'*\'',, but instead should be string keys: 'method.response.header.Access-Control-Allow-Origin': '\'*'\'',

Expected Behavior

Some of these properties should be string keys, not converted to camelCase

Current Behavior

Example output:

    const restApi = new apigateway.CfnRestApi(this, 'RestApi', {
      body: {
        info: {
          version: '1.0',
          title: this.stackName,
        },
        paths: {
          myResource: {
            options: {
              responses: {
                200: {
                  headers: {
                    accessControlAllowOrigin: {
                      type: 'string',
                    },
                    accessControlAllowHeaders: {
                      type: 'string',
                    },
                    accessControlAllowMethods: {
                      type: 'string',
                    },
                  },
                  description: 'Default response for CORS method',
                },
              },
              produces: [
                'application/json',
              ],
              xAmazonApigatewayIntegration: {
                type: 'mock',
                requestTemplates: {
                  applicationJson: '{\n  \"statusCode\" : 200\n}\n',
                },
                responses: {
                  default: {
                    statusCode: '200',
                    responseTemplates: {
                      applicationJson: '{}\n',
                    },
                    responseParameters: {
                      'method.response.header.AccessControlAllowOrigin': '\'*\'',
                      'method.response.header.AccessControlAllowMethods': '\'GET,POST,PUT,OPTIONS\'',
                      'method.response.header.AccessControlAllowHeaders': '\'*\'',
                    },
                  },
                },
              },
              summary: 'CORS support',
              security: [
                {
                  myAuthorizer: [
                  ],
                },
              ],
              consumes: [
                'application/json',
              ],
            },
            get: {
              xAmazonApigatewayIntegration: {
                httpMethod: 'POST',
                type: 'aws_proxy',
                uri: `arn:aws:apigateway:${this.region}:lambda:path/2015-03-31/functions/${myFn.attrArn}/invocations`,
              },
              security: [
                {
                  myAuthorizer: [
                  ],
                },
              ],
              responses: {
              },
            },
          },
        },
        swagger: '2.0',
        securityDefinitions: {
          headerAuthorizer: {
            in: 'header',
            type: 'apiKey',
            name: 'Unused',
            xAmazonApigatewayAuthorizer: {
              type: 'request',
              authorizerResultTtlInSeconds: 3600,
              identitySource: 'method.request.header.Authorization',
              authorizerUri: `arn:aws:apigateway:${this.region}:lambda:path/2015-03-31/functions/${myAuthorizer.attrArn}/invocations`,
            },
            xAmazonApigatewayAuthtype: 'custom',
          },
        },
      },
      name: `${this.stackName}`,
    });

Reproduction Steps

Run cdk migrate against an existing stack with a RestApi

Possible Solution

Set these properties to strings instead of converting them to camelCase

Additional Information/Context

No response

CDK CLI Version

2.100.0

Framework Version

No response

Node.js Version

v18.18.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

@rogerchi rogerchi added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 10, 2023
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Oct 10, 2023
@peterwoodworth
Copy link
Contributor

Thanks for the report,

I'm not really seeing the same behavior, but I am seeing some more obvious issues crop up. Here's the output for my stack file after deploying a RestApi just adding the GET method

import * as cdk from 'aws-cdk-lib';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import * as cdk from 'aws-cdk-lib/aws-cdk';

export interface BucketStackStackProps extends cdk.StackProps {
  /**
   * Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
   * @default '/cdk-bootstrap/hnb659fds/version'
   */
  readonly bootstrapVersion?: string;
}

export class BucketStackStack extends cdk.Stack {
  public readonly restApiEndpoint0551178A;

  public constructor(scope: cdk.App, id: string, props: BucketStackStackProps = {}) {
    super(scope, id, props);

    // Applying default props
    props = {
      ...props,
      bootstrapVersion: new cdk.CfnParameter(this, 'BootstrapVersion', {
        type: 'AWS::SSM::Parameter::Value<String>',
        default: props.bootstrapVersion?.toString() ?? '/cdk-bootstrap/hnb659fds/version',
        description: 'Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]',
      }).valueAsString,
    };

    // Resources
    const cdkMetadata = new cdk.CfnMetadata(this, 'CDKMetadata', {
      analytics: 'v2:deflate64:H4sIAAAAAAAA/02Oyw7CIBBFv8U9jI+FpksfW2NSP8AgjJU+gJTBpmn674Jo4mrOnLm5mQ0UBawWYvBcqoa3+g7TlYRsWFQ34XQlCAcxwlSip73T7PgwPzyha+3YoaFk/7bYUGFyGWLehl7iQXhkZ6SnVemYaU54CeQCfbs/2ZkZqxBqv3ytt7CLP9Zea94HQ7pDKPN8A60Sika/AAAA',
    });
    cdkMetadata.cfnOptions.metadata = {
      aws:cdk:path: 'BucketStack/CDKMetadata/Default',
    };

    const restApi0C43bf4b = new apigateway.CfnRestApi(this, 'RestApi0C43BF4B', {
      name: 'RestApi',
    });
    restApi0C43bf4b.cfnOptions.metadata = {
      aws:cdk:path: 'BucketStack/RestApi/Resource',
    };

    if (restApi0C43bf4b == null) { throw new Error(`A combination of conditions caused 'restApi0C43bf4b' to be undefined. Fixit.`); }
    const restApiGet0f59260b = new apigateway.CfnMethod(this, 'RestApiGET0F59260B', {
      authorizationType: 'NONE',
      httpMethod: 'GET',
      integration: {
        type: 'MOCK',
      },
      resourceId: restApi0C43bf4b.attrRootResourceId,
      restApiId: restApi0C43bf4b.ref,
    });
    restApiGet0f59260b.cfnOptions.metadata = {
      aws:cdk:path: 'BucketStack/RestApi/Default/GET/Resource',
    };

    if (restApi0C43bf4b == null) { throw new Error(`A combination of conditions caused 'restApi0C43bf4b' to be undefined. Fixit.`); }
    if (restApiGet0f59260b == null) { throw new Error(`A combination of conditions caused 'restApiGet0f59260b' to be undefined. Fixit.`); }
    const restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851 = new apigateway.CfnDeployment(this, 'RestApiDeployment180EC503e517d3375a1c04758a7a4b777a070851', {
      description: 'Automatically created by the RestApi construct',
      restApiId: restApi0C43bf4b.ref,
    });
    restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851.cfnOptions.metadata = {
      aws:cdk:path: 'BucketStack/RestApi/Deployment/Resource',
    };
    restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851.addDependency(restApiGet0f59260b);

    if (restApi0C43bf4b == null) { throw new Error(`A combination of conditions caused 'restApi0C43bf4b' to be undefined. Fixit.`); }
    if (restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851 == null) { throw new Error(`A combination of conditions caused 'restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851' to be undefined. Fixit.`); }
    const restApiDeploymentStageprod3855De66 = new apigateway.CfnStage(this, 'RestApiDeploymentStageprod3855DE66', {
      deploymentId: restApiDeployment180Ec503e517d3375a1c04758a7a4b777a070851.ref,
      restApiId: restApi0C43bf4b.ref,
      stageName: 'prod',
    });
    restApiDeploymentStageprod3855De66.cfnOptions.metadata = {
      aws:cdk:path: 'BucketStack/RestApi/DeploymentStage.prod/Resource',
    };

    // Outputs
    this.restApiEndpoint0551178A = [
      'https://',
      restApi0C43bf4b.ref,
      '.execute-api.us-east-1.',
      this.urlSuffix,
      '/',
      restApiDeploymentStageprod3855De66.ref,
      '/',
    ].join('');
    new cdk.CfnOutput(this, 'RestApiEndpoint0551178A', {
      value: this.restApiEndpoint0551178A!.toString(),
    });
  }
}

This is experimental, so naturally there's still a lot of work to be done here 🙂

@peterwoodworth peterwoodworth changed the title (CDK CLI): cdk migrate produces incorrect CfnRestApi body (CDK CLI): cdk migrate produces incorrect types and casing Oct 10, 2023
@peterwoodworth peterwoodworth added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Oct 10, 2023
@rogerchi
Copy link
Contributor Author

We are starting from a SAM template and defining the API mostly through the Function event sources -- maybe the cause of the differences?

@peterwoodworth
Copy link
Contributor

If you could share a sample template to reproduce exactly what you see that'd be great

@TheRealAmazonKendra
Copy link
Contributor

The issue here was how we were handling json. This should now be fixed. @rogerchi please let us know. if this is not the case.

@TheRealAmazonKendra TheRealAmazonKendra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Dec 19, 2023
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants