Skip to content

Conversation

@shuperry
Copy link
Contributor

Checklist

  • the tests are passed.
  • documentation is changed or added.

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

LGTM

docs/Routes.md Outdated
fastify.get('/', opts)
```

> Note: if the handler is specified in both the `options` and as the third parameter to the shortcut method then the third parameter takes precedence over the the value supplied in the `options`.
Copy link
Member

Choose a reason for hiding this comment

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

I don't like "magic" fallback. For me it's better throw an error if handler options and the third params are given

@shuperry
Copy link
Contributor Author

@nwoltman @mcollina @allevo Let's take a vote.

  1. handler in shorthand route options take effect if there is no handler function as the third parameter and the second parameter is an object.
  2. throw an error if handler options and the third params are given.

I'll pick the 1st.

@nwoltman
Copy link
Contributor

The answer on what should happen if the handler appears in both places depends on what situation we think the user is in when this happens.

If the user has accidentally put the handler function in both places without realizing it, we should throw an error to let them know they made a mistake.

But if the user is doing this on purpose and expects .get('/path', options, handler) to work whether or not the options object has a handler property, then we should not throw an error.

I'm not sure which situation would be the most common. I also think it would be very rare for the handler to be passed in both places at the same time so I'd be fine with either 1 or 2.

@allevo
Copy link
Member

allevo commented Mar 13, 2018

What I mean is:

Ok:

fastify.get('/', optionsWithoutHandler, handler)

Ok:

fastify.get('/', optionsWithHandler)

Throw:

fastify.get('/', optionsWithHandler, handler)

With this PR, the last snippet is valid.

In that way I'm for 2

@shuperry
Copy link
Contributor Author

OK, I'll update to the 2rd way.

@shuperry
Copy link
Contributor Author

shuperry commented Mar 14, 2018

@nwoltman @allevo @mcollina If I change the logic to the 2rd way, the test onRoute hook should preserve system route configuration will not pass, so guys, shall we just stay at the 1rd way or change this test's logic?

@shuperry
Copy link
Contributor Author

@nwoltman @allevo @mcollina Support for handler in shorthand route options is a new feture, so it's ok to update this test.

@nwoltman
Copy link
Contributor

Funny, there was already a test that says it's okay for the handler to be overwritten. @shuperry Yes that test should be changed if we're going with option 2.

@shuperry
Copy link
Contributor Author

We will need a few other throws, I will push later.

@shuperry
Copy link
Contributor Author

@allevo I updated to option 2, let's discuss.

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

This is missing a test for the new feature. Currently there are only tests for the new error throwing.

@shuperry
Copy link
Contributor Author

@nwoltman I already add test onRoute hook should preserve handler function in options of shorthand route system configuration, which one is missing, can you make it clear?

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

@shuperry There needs to be a test in test/route.test.js that tests that the handler works correctly (the test should use app.inject()).

const fastify = Fastify()
fastify.register((instance, opts, next) => {
instance.addHook('onRoute', function (route) {
t.strictEqual(route.handler.length, 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't test that it's the right handler (it just tests that it is some function with a length of 2). The handler should be created outside of the options object so that the comparison can be:

t.strictEqual(route.handler, handler)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nwoltman I updated the tests, see the code again please.

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@shuperry
Copy link
Contributor Author

@allevo I'm waiting to end this PR.

Copy link
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

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

LGTM

@mcollina mcollina merged commit 613d173 into fastify:master Mar 19, 2018
@mcollina
Copy link
Member

Thanks for the patience @shuperry! Landed now!

@shuperry
Copy link
Contributor Author

@mcollina @nwoltman @allevo @jsumners Yeah, that's great, Thanks you all guys!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants