-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
handle 'Invalid Swagger Document' and refactor some validation into Swagger Editor constructor #2263
handle 'Invalid Swagger Document' and refactor some validation into Swagger Editor constructor #2263
Conversation
…wagger Editor constructor
… into handle_invalid_swagger_document
@@ -237,6 +237,8 @@ def __init__( | |||
self.template_conditions = template_conditions | |||
self.mode = mode | |||
|
|||
self.swagger_editor = SwaggerEditor(self.definition_body) if self.definition_body else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of the validation would happen in the SwaggerEditor
constructor after these changes, so instantiating it in the ApiGenerator
constructor will make the validation happen early.
Also, this file used to instantiate multiple SwaggerEditors
because it kept modifying self.definition_body
and then would use the new value in each new instance of SwaggerEditor. Now, there's only one SwaggerEditor used and only at the end we use all of the changes made to update self.definition_body
once.
@@ -280,7 +282,7 @@ def _construct_rest_api(self): | |||
rest_api.BodyS3Location = self._construct_body_s3_dict() | |||
elif self.definition_body: | |||
# # Post Process OpenApi Auth Settings | |||
self.definition_body = self._openapi_postprocess(self.definition_body) | |||
self.definition_body = self._openapi_postprocess(self.swagger_editor.swagger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now the only time we update self.definition_body with the result of all of the changes we made with the SwaggerEditor
if not SwaggerEditor.is_valid(self.definition_body): | ||
raise InvalidResourceException( | ||
self.logical_id, | ||
"Unable to add Cors configuration because " | ||
"'DefinitionBody' does not contain a valid Swagger definition.", | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing all of these kind of validations, since they're now done when SwaggerEditor is instantiated
samtranslator/swagger/swagger.py
Outdated
# https://swagger.io/specification/#path-item-object | ||
# According to swagger spec, | ||
# each path item object must be a dict (even it is empty). | ||
# We can do an early path validation on path item objects, | ||
# so we don't need to validate wherever we use them. | ||
for path in self.iter_on_path(): | ||
SwaggerEditor.validate_path_item_is_dict(self.get_path(path), path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this into validate_definition_body
… into handle_invalid_swagger_document
… into handle_invalid_swagger_document
Codecov Report
@@ Coverage Diff @@
## develop #2263 +/- ##
===========================================
+ Coverage 93.58% 94.42% +0.84%
===========================================
Files 90 97 +7
Lines 6124 7073 +949
Branches 1260 1430 +170
===========================================
+ Hits 5731 6679 +948
+ Misses 183 179 -4
- Partials 210 215 +5
Continue to review full report at Codecov.
|
samtranslator/swagger/swagger.py
Outdated
Checks if definition_body is a valid Swagger document | ||
|
||
:param dict definition_body: Data to be validated | ||
:return: True, if definition_body is a valid Swagger document |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor): please also add docstring for the exception raised
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, the docstring shouldn't say it returns True
either. Thanks for catching it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
||
self.paths = self._doc.get("paths") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why was _doc["paths"]
changed to _doc.get("paths")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think at some point I had the self.validate_definition_body(doc)
line after self.paths = self._doc["paths"]
. So if paths
wasn't there, it would throw a runtime error (vs. with .get
it returns None
). However, shouldn't make a difference now that validation happens before (self.validate_definition_body(doc)
line before getting paths)
ae44a68
to
dda9f61
Compare
Issue #, if available:
Description of changes:
Mostly refactoring to make validation a bit less messy.
Also handles
Invalid Swagger Document
ValueError.Description of how you validated changes:
Checklist:
make pr
passesExamples?
Please reach out in the comments, if you want to add an example. Examples will be
added to
sam init
through https://github.com/awslabs/aws-sam-cli-app-templates/By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.