Skip to content

Commit

Permalink
This is weird, it is also broken??
Browse files Browse the repository at this point in the history
  • Loading branch information
rix0rrr committed Aug 10, 2023
1 parent 0f4a3ff commit 870a143
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ class StepFunctionsExecutionIntegration extends AwsIntegration {
deploymentToken = JSON.stringify({ stateMachineName });
}

if (method.methodResponses.length === 0) {
for (const methodResponse of METHOD_RESPONSES) {
method.addMethodResponse(methodResponse);
}
for (const methodResponse of METHOD_RESPONSES) {
method.addMethodResponse(methodResponse);
}

return {
Expand Down
26 changes: 19 additions & 7 deletions packages/aws-cdk-lib/aws-apigateway/lib/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class Method extends Resource {
*/
public readonly api: IRestApi;

public readonly methodResponses: MethodResponse[];
private readonly methodResponses: MethodResponse[];

constructor(scope: Construct, id: string, props: MethodProps) {
super(scope, id);
Expand Down Expand Up @@ -242,6 +242,22 @@ export class Method extends Resource {
},
});
}

this.node.addValidation({
validate: () => {
const responses = this.methodResponses;

// Validate that all method response codes are unique
const counts: Record<string, number> = {};
for (const resp of responses) {
counts[resp.statusCode] = (counts[resp.statusCode] ?? 0) + 1;
}
const dupes = Object.entries(counts).filter(([_, n]) => n > 1).map(([c, _]) => c);
return dupes.map((code) =>
`${counts[code]} methodResponses for code ${code}: ${this.methodResponses.filter((r) => r.statusCode === code).map((x) => JSON.stringify(x)).join(', ')}`,
);
},
});
}

/**
Expand Down Expand Up @@ -322,12 +338,8 @@ export class Method extends Resource {
let responseModels: {[contentType: string]: string} | undefined;

if (mr.responseModels) {
responseModels = {};
for (const contentType in mr.responseModels) {
if (mr.responseModels.hasOwnProperty(contentType)) {
responseModels[contentType] = mr.responseModels[contentType].modelId;
}
}
responseModels = Object.fromEntries(Object.entries(mr.responseModels)
.map(([contentType, rm]) => [contentType, rm.modelId]));
}

const methodResponseProp = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe('StepFunctionsIntegration', () => {

const integ = apigw.StepFunctionsIntegration.startExecution(stateMachine, integrationOptions);
api.root.addMethod('GET', integ, methodOptions);
api.root.addMethod('POST', integ, methodOptions);
// api.root.addMethod('POST', integ, methodOptions);

//THEN
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::Method', 2);
Expand Down

0 comments on commit 870a143

Please sign in to comment.