Skip to content

Websocket event schema#8218

Merged
medikoo merged 14 commits into
serverless:masterfrom
rzaldana:master
Sep 18, 2020
Merged

Websocket event schema#8218
medikoo merged 14 commits into
serverless:masterfrom
rzaldana:master

Conversation

@rzaldana

@rzaldana rzaldana commented Sep 10, 2020

Copy link
Copy Markdown
Contributor

Closes: #8019

Hi this is my proposal for the websocket event schema. This is my first PR so sorry if I missed something basic!

Also, I'm not sure how to do testing for the schema. I did my own testing but I'm not sure how to do it within SLS. Maybe someone could provide some guidance (e.g. which files I should be working with to do the testing)? Thank you!

@codecov-commenter

codecov-commenter commented Sep 10, 2020

Copy link
Copy Markdown

Codecov Report

Merging #8218 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8218      +/-   ##
==========================================
- Coverage   88.09%   88.08%   -0.02%     
==========================================
  Files         248      248              
  Lines        9377     9369       -8     
==========================================
- Hits         8261     8253       -8     
  Misses       1116     1116              
Impacted Files Coverage Δ
lib/configSchema.js 100.00% <ø> (ø)
...ins/aws/package/compile/events/websockets/index.js 100.00% <ø> (ø)
.../package/compile/events/websockets/lib/validate.js 97.22% <ø> (-0.51%) ⬇️
lib/plugins/aws/provider/awsProvider.js 93.11% <ø> (ø)

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 29d5fe7...c149d29. Read the comment docs.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks @zaldanaraul ! That looks very promising.

Please see my remarks, there's nothing significant, I think it's close to what we're after.

Also, I'm not sure how to do testing for the schema.

We've agreed to not write tests that confirm schema errors (simply to avoid maintanance cost, and validation schema engine is tested extensively on its own).

By tests we only confirm that valid configuration works, and all tests that are configured through runServerless (see https://github.com/serverless/serverless/tree/master/test#unit-tests) are configured to crash whenever configuration error is approached in tested case.

Ideally such tests should also cover all supported configuration cases.

Unfortunately websockets, aside of integration tests, are not tested well via service configurations

Ideally would be to have its unit tests refactored, so they're solely based on runServerless, but that's definitely not in scope of this PR (anyway PR that does that is defintely welcome)

Otherwise you may just test it manually, by configuring simple service, and testing configuration via sls print

Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/provider/awsProvider.js Outdated
Comment thread lib/plugins/aws/provider/awsProvider.js
@rzaldana
rzaldana requested a review from medikoo September 15, 2020 14:36

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zaldanaraul great thanks for update! It looks very promising. Please see my remarks.

Also it'll be good to remove inline validation we have in event implementation (see e.g. errors removed here: https://github.com/serverless/serverless/pull/8230/files )

Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/package/compile/events/websockets/index.js Outdated
Comment thread lib/plugins/aws/package/compile/events/websockets/index.js
Comment thread lib/plugins/aws/provider/awsProvider.js
@rzaldana

rzaldana commented Sep 16, 2020

Copy link
Copy Markdown
Contributor Author

@medikoo

Also it'll be good to remove inline validation we have in event implementation (see e.g. errors removed here: https://github.com/serverless/serverless/pull/8230/files )

For websockets, validation seems to be implemented through the validate function defined here:

https://github.com/serverless/serverless/blob/master/lib/plugins/aws/package/compile/events/websockets/lib/validate.js

Although it seems to check for things that the schema also checks for, the errors that it throws do seem clearer than the ones from the schema at this point. Also, because this function is simply included but not called in the AWSCompileWebsockets, I'm not really sure when/how it's called so I think simply removing it would break the code:

const validate = require('./lib/validate');

class AwsCompileWebsockets {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
Object.assign(
this,
validate,

Please advise on how to proceed here :(

@rzaldana
rzaldana requested a review from medikoo September 16, 2020 18:21
@medikoo

medikoo commented Sep 18, 2020

Copy link
Copy Markdown
Contributor

Although it seems to check for things that the schema also checks for, the errors that it throws do seem clearer than the ones from the schema at this point.

Which errors exactly? In general we follow the rule to not duplicate schema validation with inline validation that validates very same things. Also schema errors are configured to be well descriptive

I think simply removing it would break the code

I don't think that we should remove that functions. In most cases validate also normalizes input, and that should stay untouched. See e.g. in this PR, how inline validation was striped out: https://github.com/serverless/serverless/pull/8201/files

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zaldanaraul that looks really good! All that we miss, is what we've discussed in other comments (so restricting routeResponseSelectionExpression to $default, and stripping inline validation which covers what would be covered now by config schema validation)

@rzaldana

Copy link
Copy Markdown
Contributor Author

Which errors exactly? In general we follow the rule to not duplicate schema validation with inline validation that validates very same things. Also schema errors are configured to be well descriptive

I'll stick to this rule then. In that case I removed three inline validations from the validate.js file (all of which the schema already validates):

  • error if event doesn't have a route property
  • error if both http and websocket properties are present in event
  • error if authorizer has neither name nor arn property

Also changed the routeResponseSelectionExpression validation to use const. Please take a look!

@rzaldana
rzaldana requested a review from medikoo September 18, 2020 14:59

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks great @zaldanaraul ! Thank you

@medikoo
medikoo merged commit e1ca63c into serverless:master Sep 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Config schema: Define AWS "websocket" function event properties

3 participants