-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: Add ValidateBody/ValidateParameters attribute to setup API model validators #2026
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
Changes from 16 commits
1baaba6
56330e2
218833b
af4b806
4f85273
1c6afda
561a9ee
909c252
5322874
aae5717
fc23711
8f71bcb
37ff9d4
bc3dd9e
91886ff
e50aa42
b3ce527
be312b9
c00c7bf
2ef0d41
895fcd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ class SwaggerEditor(object): | |
_X_APIGW_GATEWAY_RESPONSES = "x-amazon-apigateway-gateway-responses" | ||
_X_APIGW_POLICY = "x-amazon-apigateway-policy" | ||
_X_ANY_METHOD = "x-amazon-apigateway-any-method" | ||
_X_APIGW_REQUEST_VALIDATOR = "x-amazon-apigateway-request-validators" | ||
Rondineli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
_CACHE_KEY_PARAMETERS = "cacheKeyParameters" | ||
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html | ||
_ALL_HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"] | ||
|
@@ -781,6 +782,41 @@ def _set_method_apikey_handling(self, path, method_name, apikey_required): | |
if security != existing_security: | ||
method_definition["security"] = security | ||
|
||
def add_request_validator_to_method(self, path, method_name, validate_body=False, validate_request=False): | ||
""" | ||
Adds request model body parameter for this path/method. | ||
|
||
:param string path: Path name | ||
:param string method_name: Method name | ||
:param bool validate_body: Add validator parameter on the body | ||
:param bool validate_request: Validate request | ||
""" | ||
|
||
normalized_method_name = self._normalize_method_name(method_name) | ||
validator_name = SwaggerEditor.get_validator_name(validate_body, validate_request) | ||
|
||
# Creating validator | ||
request_validator_definition = { | ||
validator_name: {"validateRequestBody": validate_body, "validateRequestParameters": validate_request} | ||
} | ||
if not self._doc.get(self._X_APIGW_REQUEST_VALIDATOR): | ||
self._doc[self._X_APIGW_REQUEST_VALIDATOR] = {} | ||
|
||
if not self._doc[self._X_APIGW_REQUEST_VALIDATOR].get(validator_name): | ||
# Adding only if the validator hasn't been defined already | ||
self._doc[self._X_APIGW_REQUEST_VALIDATOR].update(request_validator_definition) | ||
|
||
# It is possible that the method could have two definitions in a Fn::If block. | ||
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]): | ||
Rondineli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
# If no integration given, then we don't need to process this definition (could be AWS::NoValue) | ||
if not self.method_definition_has_integration(method_definition): | ||
continue | ||
|
||
set_validator_to_method = {"x-amazon-apigateway-request-validator": validator_name} | ||
# Setting validator to the given method | ||
method_definition.update(set_validator_to_method) | ||
|
||
def add_request_model_to_method(self, path, method_name, request_model): | ||
""" | ||
Adds request model body parameter for this path/method. | ||
|
@@ -1265,6 +1301,26 @@ def get_path_without_trailing_slash(path): | |
# convert greedy paths to such as {greedy+}, {proxy+} to "*" | ||
return re.sub(r"{([a-zA-Z0-9._-]+|[a-zA-Z0-9._-]+\+|proxy\+)}", "*", path) | ||
|
||
@staticmethod | ||
def get_validator_name(validate_body, validate_request): | ||
""" | ||
Get a readable path name to use as validator name | ||
|
||
:param boolean validate_body: Boolean if validate body | ||
:param boolean validate_request: Boolean if validate request | ||
:return string: Normalized validator name | ||
""" | ||
if validate_body and validate_request: | ||
return "FULL" | ||
Rondineli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if validate_body and not validate_request: | ||
return "BODY" | ||
|
||
if not validate_body and validate_request: | ||
return "REQUEST" | ||
|
||
return "NO_VALIDATE" | ||
|
||
@staticmethod | ||
def _validate_list_property_is_resolved(property_list): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
Resources: | ||
HtmlFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: / | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateBody: true | ||
ValidateRequest: true | ||
|
||
HtmlFunctionNoValidation: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /no-validation | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateBody: false | ||
ValidateRequest: false | ||
|
||
HtmlFunctionMixinValidation: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /mixin | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateBody: true | ||
ValidateRequest: false | ||
|
||
HtmlFunctionOnlyBodyDefinition: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /only-body-true | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateBody: true | ||
|
||
HtmlFunctionOnlyRequestDefinition: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /only-request-true | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateRequest: true | ||
|
||
HtmlFunctionOnlyRequestDefinitionFalse: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /only-request-false | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateRequest: false | ||
|
||
HtmlFunctionOnlyBodyDefinitionFalse: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /only-body-false | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
ValidateBody: false | ||
|
||
HtmlFunctionNotDefinedValidation: | ||
Rondineli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
Handler: index.gethtml | ||
Runtime: nodejs12.x | ||
Events: | ||
GetHtml: | ||
Type: Api | ||
Properties: | ||
RestApiId: HtmlApi | ||
Path: /not-defined | ||
Method: get | ||
RequestModel: | ||
Model: User | ||
Required: true | ||
|
||
HtmlApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
StageName: Prod | ||
Models: | ||
User: | ||
type: object | ||
properties: | ||
username: | ||
type: string |
Uh oh!
There was an error while loading. Please reload this page.