Skip to content

Commit 7c555d4

Browse files
author
Rondineli
committed
Set another strategy to set the validators without affect existent clients with model required
1 parent 344a153 commit 7c555d4

21 files changed

+1703
-263
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,13 @@ def _add_swagger_integration(self, api, function, intrinsics_resolver):
802802
# If not type None but any other type it should explicitly invalidate the Spec
803803
# Those fields should be only a boolean
804804
if not isinstance(validate_body, bool) or not isinstance(validate_request, bool):
805-
raise TypeError("ValidateBody and ValidateRequest must be a boolean type, strings or intrinsics are not supported")
805+
raise InvalidEventException(
806+
self.relative_id,
807+
"Unable to set Validator to RequestModel [{model}] on API method [{method}] for path [{path}] "
808+
"ValidateBody and ValidateRequest must be a boolean type, strings or intrinsics are not supported.".format(
809+
model=method_model, method=self.Method, path=self.Path
810+
),
811+
)
806812

807813
editor.add_request_validator_to_method(
808814
path=self.Path,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Resources:
2+
HtmlFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/member_portal.zip
6+
Handler: index.gethtml
7+
Runtime: nodejs12.x
8+
Events:
9+
GetHtml:
10+
Type: Api
11+
Properties:
12+
RestApiId: HtmlApi
13+
Path: /
14+
Method: get
15+
RequestModel:
16+
Model: User
17+
Required: true
18+
ValidateBody: true
19+
ValidateRequest: true
20+
21+
HtmlFunctionNoValidation:
22+
Type: AWS::Serverless::Function
23+
Properties:
24+
CodeUri: s3://sam-demo-bucket/member_portal.zip
25+
Handler: index.gethtml
26+
Runtime: nodejs12.x
27+
Events:
28+
GetHtml:
29+
Type: Api
30+
Properties:
31+
RestApiId: HtmlApi
32+
Path: /no-validation
33+
Method: get
34+
RequestModel:
35+
Model: User
36+
Required: true
37+
ValidateBody: false
38+
ValidateRequest: false
39+
40+
HtmlFunctionMixinValidation:
41+
Type: AWS::Serverless::Function
42+
Properties:
43+
CodeUri: s3://sam-demo-bucket/member_portal.zip
44+
Handler: index.gethtml
45+
Runtime: nodejs12.x
46+
Events:
47+
GetHtml:
48+
Type: Api
49+
Properties:
50+
RestApiId: HtmlApi
51+
Path: /mixin
52+
Method: get
53+
RequestModel:
54+
Model: User
55+
Required: true
56+
ValidateBody: true
57+
ValidateRequest: false
58+
59+
HtmlFunctionNotDefinedValidation:
60+
Type: AWS::Serverless::Function
61+
Properties:
62+
CodeUri: s3://sam-demo-bucket/member_portal.zip
63+
Handler: index.gethtml
64+
Runtime: nodejs12.x
65+
Events:
66+
GetHtml:
67+
Type: Api
68+
Properties:
69+
RestApiId: HtmlApi
70+
Path: /not-defined
71+
Method: get
72+
RequestModel:
73+
Model: User
74+
Required: true
75+
76+
HtmlApi:
77+
Type: AWS::Serverless::Api
78+
Properties:
79+
StageName: Prod
80+
Models:
81+
User:
82+
type: object
83+
properties:
84+
username:
85+
type: string
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Parameters:
2+
validateRequestBody:
3+
Type: String
4+
validateRequestParameters:
5+
Type: String
6+
7+
Resources:
8+
RequestValidator:
9+
Type: AWS::ApiGateway::RequestValidator
10+
Properties:
11+
Name: "validator"
12+
RestApiId: !Ref HtmlApi
13+
ValidateRequestBody: true
14+
ValidateRequestParameters: true
15+
16+
HtmlFunction:
17+
Type: AWS::Serverless::Function
18+
Properties:
19+
CodeUri: s3://sam-demo-bucket/member_portal.zip
20+
Handler: index.gethtml
21+
Runtime: nodejs12.x
22+
Events:
23+
GetHtml:
24+
Type: Api
25+
Properties:
26+
RestApiId: HtmlApi
27+
Path: /
28+
Method: get
29+
RequestModel:
30+
Model: User
31+
Required: true
32+
ValidateBody: !Ref validateRequestBody
33+
ValidateRequest: !Ref validateRequestParameters
34+
35+
HtmlApi:
36+
Type: AWS::Serverless::Api
37+
Properties:
38+
StageName: Prod
39+
Models:
40+
User:
41+
type: object
42+
properties:
43+
username:
44+
type: string
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Resources:
2+
RequestValidator:
3+
Type: AWS::ApiGateway::RequestValidator
4+
Properties:
5+
Name: "validator"
6+
RestApiId: !Ref HtmlApi
7+
ValidateRequestBody: true
8+
ValidateRequestParameters: true
9+
10+
HtmlFunction:
11+
Type: AWS::Serverless::Function
12+
Properties:
13+
CodeUri: s3://sam-demo-bucket/member_portal.zip
14+
Handler: index.gethtml
15+
Runtime: nodejs12.x
16+
Events:
17+
GetHtml:
18+
Type: Api
19+
Properties:
20+
RestApiId: HtmlApi
21+
Path: /
22+
Method: get
23+
RequestModel:
24+
Model: User
25+
Required: true
26+
ValidateBody: "true"
27+
ValidateRequest: "true"
28+
29+
HtmlApi:
30+
Type: AWS::Serverless::Api
31+
Properties:
32+
StageName: Prod
33+
Models:
34+
User:
35+
type: object
36+
properties:
37+
username:
38+
type: string

tests/translator/output/api_request_model.json

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
{
22
"Resources": {
3-
"HtmlApiDeploymentf6373e349a": {
4-
"Type": "AWS::ApiGateway::Deployment",
5-
"Properties": {
6-
"RestApiId": {
7-
"Ref": "HtmlApi"
8-
},
9-
"Description": "RestApi deployment id: f6373e349a348b4eb4f1edd7d12cf37c287dd6c0",
10-
"StageName": "Stage"
11-
}
12-
},
133
"HtmlFunctionRole": {
144
"Type": "AWS::IAM::Role",
155
"Properties": {
@@ -44,7 +34,7 @@
4434
"Type": "AWS::ApiGateway::Stage",
4535
"Properties": {
4636
"DeploymentId": {
47-
"Ref": "HtmlApiDeploymentf6373e349a"
37+
"Ref": "HtmlApiDeploymentdd6198efe6"
4838
},
4939
"RestApiId": {
5040
"Ref": "HtmlApi"
@@ -91,7 +81,6 @@
9181
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HtmlFunction.Arn}/invocations"
9282
}
9383
},
94-
"x-amazon-apigateway-request-validator": "root-get-validator",
9584
"responses": {},
9685
"parameters": [
9786
{
@@ -107,12 +96,6 @@
10796
}
10897
},
10998
"swagger": "2.0",
110-
"x-amazon-apigateway-request-validators": {
111-
"root-get-validator": {
112-
"validateRequestParameters": true,
113-
"validateRequestBody": true
114-
}
115-
},
11699
"definitions": {
117100
"user": {
118101
"type": "object",
@@ -126,6 +109,16 @@
126109
}
127110
}
128111
},
112+
"HtmlApiDeploymentdd6198efe6": {
113+
"Type": "AWS::ApiGateway::Deployment",
114+
"Properties": {
115+
"RestApiId": {
116+
"Ref": "HtmlApi"
117+
},
118+
"Description": "RestApi deployment id: dd6198efe68d8db17c8bf6b680a8a6763e4f36b5",
119+
"StageName": "Stage"
120+
}
121+
},
129122
"HtmlFunction": {
130123
"Type": "AWS::Lambda::Function",
131124
"Properties": {

tests/translator/output/api_request_model_openapi_3.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"Type": "AWS::ApiGateway::Stage",
3535
"Properties": {
3636
"DeploymentId": {
37-
"Ref": "HtmlApiDeployment7bcc9b1f76"
37+
"Ref": "HtmlApiDeployment59eeb787ee"
3838
},
3939
"RestApiId": {
4040
"Ref": "HtmlApi"
@@ -92,7 +92,6 @@
9292
},
9393
"credentials": "arn:aws:iam::*:user/*"
9494
},
95-
"x-amazon-apigateway-request-validator": "iam-get-validator",
9695
"security": [
9796
{
9897
"AWS_IAM": []
@@ -120,7 +119,6 @@
120119
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HtmlFunction.Arn}/invocations"
121120
}
122121
},
123-
"x-amazon-apigateway-request-validator": "root-get-validator",
124122
"responses": {}
125123
}
126124
}
@@ -145,27 +143,17 @@
145143
}
146144
}
147145
}
148-
},
149-
"x-amazon-apigateway-request-validators": {
150-
"iam-get-validator": {
151-
"validateRequestParameters": true,
152-
"validateRequestBody": true
153-
},
154-
"root-get-validator": {
155-
"validateRequestParameters": true,
156-
"validateRequestBody": true
157-
}
158146
}
159147
}
160148
}
161149
},
162-
"HtmlApiDeployment7bcc9b1f76": {
150+
"HtmlApiDeployment59eeb787ee": {
163151
"Type": "AWS::ApiGateway::Deployment",
164152
"Properties": {
165153
"RestApiId": {
166154
"Ref": "HtmlApi"
167155
},
168-
"Description": "RestApi deployment id: 7bcc9b1f76a65d06d9a112661419d20d44564dac"
156+
"Description": "RestApi deployment id: 59eeb787ee1561329a07e10162ac3718998e9f91"
169157
}
170158
},
171159
"HtmlFunctionIamPermissionProd": {

0 commit comments

Comments
 (0)