Skip to content
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

Merged
merged 10 commits into from
Jan 24, 2022

Conversation

torresxb1
Copy link
Contributor

@torresxb1 torresxb1 commented Dec 14, 2021

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:

  • Add/update tests using:
  • make pr passes
  • Update documentation
  • Verify transformed template deploys and application functions as expected

Examples?

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.

@@ -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
Copy link
Contributor Author

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)
Copy link
Contributor Author

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

Comment on lines -613 to -631
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.",
)

Copy link
Contributor Author

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

Comment on lines 56 to 69
# 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)
Copy link
Contributor Author

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

@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2022

Codecov Report

Merging #2263 (dda9f61) into develop (e7a1496) will increase coverage by 0.84%.
The diff coverage is 96.95%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ
samtranslator/model/api/http_api_generator.py 91.21% <75.00%> (+0.02%) ⬆️
samtranslator/model/api/api_generator.py 94.01% <90.62%> (-0.35%) ⬇️
samtranslator/model/apigateway.py 96.98% <96.00%> (-0.18%) ⬇️
samtranslator/__init__.py 100.00% <100.00%> (ø)
samtranslator/feature_toggle/dialup.py 100.00% <100.00%> (ø)
samtranslator/feature_toggle/feature_toggle.py 100.00% <100.00%> (+12.16%) ⬆️
samtranslator/intrinsics/actions.py 98.79% <100.00%> (+0.07%) ⬆️
samtranslator/metrics/method_decorator.py 100.00% <100.00%> (ø)
samtranslator/metrics/metrics.py 100.00% <100.00%> (ø)
samtranslator/model/__init__.py 97.64% <100.00%> (ø)
... and 34 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a5db070...dda9f61. Read the comment docs.

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
Copy link
Contributor

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

Copy link
Contributor Author

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!

Copy link
Contributor Author

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")
Copy link
Contributor

@hoffa hoffa Jan 17, 2022

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")?

Copy link
Contributor Author

@torresxb1 torresxb1 Jan 17, 2022

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)

@torresxb1 torresxb1 force-pushed the handle_invalid_swagger_document branch from ae44a68 to dda9f61 Compare January 17, 2022 19:43
@torresxb1 torresxb1 merged commit 59df2db into aws:develop Jan 24, 2022
@torresxb1 torresxb1 deleted the handle_invalid_swagger_document branch March 14, 2022 20:20
torresxb1 added a commit to torresxb1/serverless-application-model that referenced this pull request Mar 14, 2022
…n into Swagger Editor constructor (aws#2263)"

This reverts commit 59df2db.
torresxb1 added a commit that referenced this pull request Mar 14, 2022
…n into Swagger Editor constructor (#2263)" (#2346)

This reverts commit 59df2db.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants