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

[WIP] feat(openapi-v2): add decorator for head op #862

Closed
wants to merge 1 commit into from

Conversation

b-admike
Copy link
Contributor

@b-admike b-admike commented Jan 15, 2018

Add sugar function for head operation.

Connect to #691

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Related API Documentation was updated
  • Affected artifact templates in packages/cli were updated (N/A?)
  • Affected example projects in packages/example-* were updated (N/A?)

@b-admike b-admike self-assigned this Jan 15, 2018
@b-admike b-admike force-pushed the feat/head-decorator branch from eabbeb2 to c46ca1b Compare January 15, 2018 13:14
@b-admike b-admike changed the title Feat/head decorator feat(openapi-v2): add decorator for head op Jan 15, 2018
@b-admike
Copy link
Contributor Author

@slnode test please

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

Nice 👍

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

lgtm 👍

@b-admike b-admike force-pushed the feat/head-decorator branch from df3cff6 to d2dfd67 Compare January 15, 2018 14:40
@b-admike
Copy link
Contributor Author

@slnode test please

greet() {
return 'Hello world!';
}
greet() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

@b-admike I am not quite familiar with these test cases, but I feel the spec for a function that has a return arg is different than the one that doesn't have a return...so I am not sure how the same openrationSpec passes for both?

Maybe the code with your change is correct. It depends on does operationSpec contains a response object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jannyHou I think these tests are for asserting that the correct operation object is mapped to its corresponding controller method. Yeah I think the response object in operationSpec is the same throughout the tests.


function givenAnOperationSpec() {
return anOperationSpec()
.withStringResponse()
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm...I think if this helper function calls withStringResponse then we probably could not omit the return in those controller functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed this with Janny, but for posterity, we do not have any assertions in the tests for the response object or the actual response, and thus having a return statement with a string for the controller methods doesn't have any effect.

@b-admike b-admike force-pushed the feat/head-decorator branch from d2dfd67 to 353e448 Compare January 15, 2018 15:17
@raymondfeng
Copy link
Contributor

@b-admike Can you explain why you removed all return values for greet?

@b-admike
Copy link
Contributor Author

@raymondfeng it's because we weren't using them in the tests and it increased our code coverage. See my discussion with Janny above.

@raymondfeng
Copy link
Contributor

Tests are examples for some developers to learn and follow. I would rather not sacrifice them for artificial coverage numbers. It's better to use /* istanbul ignore next */ to instruct the tool. See https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md.

@b-admike b-admike mentioned this pull request Jan 15, 2018
6 tasks
@b-admike
Copy link
Contributor Author

b-admike commented Jan 15, 2018

I mainly got rid of them because I thought they didn't serve any purpose. The coverage increase was just a bonus :). But I don't mind reverting those changes and using the ignore comments.

Copy link
Contributor

@shimks shimks left a comment

Choose a reason for hiding this comment

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

+1 on Raymond's comment, otherwise LGTM

@raymondfeng
Copy link
Contributor

@b-admike It does serve some purposes to show the usage of our decorators and when they apply. For example, GET usually expects to see a response from the return value. Developers might be confused to see greet does nothing and returns nothing.

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

Please revert the changes on greet and use /* istanbul ignore next*/ to ignore such sections if desired. I'm fine to let the coverage tool complain for these cases.

@virkt25
Copy link
Contributor

virkt25 commented Jan 16, 2018

Great addition!! Just thinking about this from a developer perspective though ... declaring an additional @Head() with a function seems like extra work. Can this decorator be nested on a function doing the @get() call but make the function just return the headers. Or perhaps we should consider a way to mark a GET route as HEAD enabled ... or we just enable it by default for all GET requests and the decorator allows header values to be set?

I'm ok with the changes. Asking this question more for the purposes of discussion before any changes are made. Reference for purpose of HEAD request: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD

@b-admike
Copy link
Contributor Author

@virkt25 Those are good points. I'm up for finding alternatives to this approach! Not sure how to implement what you've suggested though.

@b-admike b-admike force-pushed the feat/head-decorator branch from 4990415 to 93c1e92 Compare January 17, 2018 13:08
@b-admike b-admike force-pushed the feat/head-decorator branch from 93c1e92 to 2cc856a Compare January 17, 2018 13:10
@raymondfeng
Copy link
Contributor

@virkt25 Good point.

@b-admike Maybe it's not so useful to have @head any more and @get will imply. I think HEAD can be handled by the router so that requests will be routed to GET but remove the response body.

@virkt25
Copy link
Contributor

virkt25 commented Jan 17, 2018

I think it might be better from a performance standpoint to not execute the GET function at all rather than to remove the response body. Unnecessary delay in response to HEAD + additional hits against DB.

But we should be able to set the content-type header (json / xml / whatever) for the expected response. And for files (if static handler will be using the same router) ... we should be able to return the Content-Length header and type header appropriately as well.

@raymondfeng
Copy link
Contributor

Without invoking the get, head won’t have the same list of response headers.

@virkt25
Copy link
Contributor

virkt25 commented Jan 19, 2018

I was just wondering if we may be able to avoid that overhead by letting users optionally specify the headers they want to pass along for a HEAD request. But if there's no other way then I'm fine with calling GET handler and dropping the body. Seems to be what Express does:

The app.get() function is automatically called for the HTTP HEAD method in addition to the GET method if app.head() was not called for the path before app.get().

@b-admike b-admike changed the title feat(openapi-v2): add decorator for head op [WIP] feat(openapi-v2): add decorator for head op Feb 1, 2018
@raymondfeng
Copy link
Contributor

openapi-v2 is now removed.

@bajtos bajtos deleted the feat/head-decorator branch June 5, 2018 07:49
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.

6 participants