Skip to content

Commit 7cdbcec

Browse files
authored
fix(apig): Move 'selectionPattern to integrationResponses` (#1636)
The property was modeled in the wrong place, rendering it unusable. Moved it to the correct location instead, and added a validation test. Fixes #1608
1 parent 5df22d9 commit 7cdbcec

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

packages/@aws-cdk/aws-apigateway/lib/integration.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ export interface IntegrationOptions {
8686
*/
8787
integrationResponses?: IntegrationResponse[];
8888

89-
/**
90-
* The templates that are used to transform the integration response body.
91-
* Specify templates as key-value pairs (string-to-string mappings), with a
92-
* content type as the key and a template as the value.
93-
*
94-
* @see http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
95-
*/
96-
selectionPattern?: string;
97-
9889
/**
9990
* The type of network connection to the integration endpoint.
10091
* @default ConnectionType.Internet
@@ -243,6 +234,17 @@ export enum ConnectionType {
243234
}
244235

245236
export interface IntegrationResponse {
237+
/**
238+
* Specifies the regular expression (regex) pattern used to choose an integration response based on the response from
239+
* the back end. For example, if the success response returns nothing and the error response returns some string, you
240+
* could use the ``.+`` regex to match error response. However, make sure that the error response does not contain any
241+
* newline (``\n``) character in such cases. If the back end is an AWS Lambda function, the AWS Lambda function error
242+
* header is matched. For all other HTTP and AWS back ends, the HTTP status code is matched.
243+
*
244+
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-integration-settings-integration-response.html
245+
*/
246+
selectionPattern?: string;
247+
246248
/**
247249
* The status code that API Gateway uses to map the integration response to
248250
* a MethodResponse status code.

packages/@aws-cdk/aws-apigateway/test/test.method.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,51 @@ export = {
302302
// THEN
303303
test.throws(() => api.root.addMethod('GET', integration), /cannot set 'vpcLink' where 'connectionType' is INTERNET/);
304304
test.done();
305+
},
306+
307+
'multiple integration responses can be used'(test: Test) { // @see https://github.com/awslabs/aws-cdk/issues/1608
308+
// GIVEN
309+
const stack = new cdk.Stack();
310+
const api = new apigateway.RestApi(stack, 'test-api', { deploy: false });
311+
312+
// WHEN
313+
api.root.addMethod('GET', new apigateway.AwsIntegration({
314+
service: 'foo-service',
315+
action: 'BarAction',
316+
options: {
317+
integrationResponses: [
318+
{
319+
statusCode: '200',
320+
responseTemplates: { 'application/json': JSON.stringify({ success: true }) },
321+
},
322+
{
323+
selectionPattern: 'Invalid',
324+
statusCode: '503',
325+
responseTemplates: { 'application/json': JSON.stringify({ success: false, message: 'Invalid Request' }) },
326+
}
327+
],
328+
}
329+
}));
330+
331+
// THEN
332+
expect(stack).to(haveResource('AWS::ApiGateway::Method', {
333+
Integration: {
334+
IntegrationHttpMethod: 'POST',
335+
IntegrationResponses: [
336+
{
337+
ResponseTemplates: { 'application/json': '{"success":true}' },
338+
StatusCode: '200',
339+
},
340+
{
341+
ResponseTemplates: { 'application/json': '{"success":false,"message":"Invalid Request"}' },
342+
SelectionPattern: 'Invalid',
343+
StatusCode: '503',
344+
}
345+
],
346+
Type: 'AWS',
347+
Uri: { 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':apigateway:', { Ref: 'AWS::Region' }, ':foo-service:action/BarAction']]}
348+
}
349+
}));
350+
test.done();
305351
}
306352
};

0 commit comments

Comments
 (0)