Closed
Bug 1400999
Opened 7 years ago
Closed 6 years ago
[tc-lib-api] Do not set application/json for empty responses or DELETE methods
Categories
(Taskcluster :: Services, defect)
Taskcluster
Services
Tracking
(Not tracked)
RESOLVED
FIXED
People
(Reporter: dustin, Assigned: arshadkazmi42, Mentored)
References
Details
In particular, it responds with 204's with empty bodies, which tc-client does not like. Better to reply with an empty JSON body. And follow the other best-practices.
Comment 1•7 years ago
|
||
Sounds like a taskcluster-client-web bug
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → INVALID
Reporter | ||
Updated•7 years ago
|
Component: Platform and Services → Platform Libraries
Summary: purge-cache service needs to be updated → [taskcluster-client-web] don't fail on 204 OK
Updated•7 years ago
|
Status: RESOLVED → REOPENED
Resolution: INVALID → ---
Comment 2•7 years ago
|
||
The problem with this isn't that the client libraries don't handle the data correctly, it's that the response says the content-type is appication/json, but returns an empty body which is not parseable as JSON.
Summary: [taskcluster-client-web] don't fail on 204 OK → [tc-lib-api] Do not set application/json for empty responses or DELETE methods
Comment 4•6 years ago
|
||
Probably we should fix this.
Reporter | ||
Comment 5•6 years ago
|
||
The work here is to make sure that every API method that returns nothing either returns an empty JSON document (`{}`) or does not set content-type: application/json. Ideally tc-lib-api can fix this automatically, but it would be OK for it to check this on every request, and we will get tests failing where it's not the case.
Mentor: dustin
Reporter | ||
Comment 6•6 years ago
|
||
This is still quite relevant -- hopefully someone will hack on it soon!
Assignee | ||
Comment 7•6 years ago
|
||
I am interested to take this up.
Is it still available?
Flags: needinfo?(dustin)
Assignee | ||
Comment 9•6 years ago
|
||
Cool. Where can I find the code for this? On Github? Any suggestions on where can I start working on it?
Flags: needinfo?(dustin)
Reporter | ||
Comment 10•6 years ago
|
||
The code is in https://github.com/taskcluster/taskcluster-lib-api. Comment 5 contains some ideas for how to fix this.
Flags: needinfo?(dustin)
Assignee | ||
Comment 11•6 years ago
|
||
Dustin, I went through the code base, but couldn't find `204` status code anywhere in the code.
I tried debugging route_test.js, all the tests are returning `{}` body response except the api which is giving 400 bad requests.
Can you guide, where exactly I need to be looking for this? Or is there a way to reproduce the issue?
Flags: needinfo?(dustin)
Reporter | ||
Comment 12•6 years ago
|
||
Hm, this is the line that originally caused the complaints:
https://github.com/taskcluster/taskcluster-purge-cache/blob/master/src/api.js#L83
res.status(204).send();
The tc-lib-api docs say "Note: use of res.status(4..).json(..) to return error statuses is an anti-pattern. While you may see older code that still follows this pattern, do not repeat it!" (https://github.com/taskcluster/taskcluster-lib-api#request-handlers) and in general I think API methods shouldn't be using res.status(..) at all, preferring res.reply.
I tested this out in the tools site, which was the original complaint. I added a new purge-cache, and watched in the Firefox network inspector to see the response. It's a 204, without a content-type header, and with content-length 0. So, I think this is in fact 100% fine already. So it seems this was accidentally fixed somewhere along the way?
But I think we can make this a little better. Let's allow
res.reply();
to reply with a 204. Then callers won't need to use `res.status(204)` anymore. So, can you add some support for that in tc-lib-api, including a test? Once that's done, we can upgrade the services that currently use `res.status(204)` to use it.
Flags: needinfo?(dustin)
Assignee | ||
Comment 13•6 years ago
|
||
So, for allowing 204 in res.reply, I am thinking of adding a condition here
https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/schema.js#L85
```
// If JSON was valid or validation was skipped then reply with 200 OK
if (res.statusCode === 204) {
res.status(204).json(json)
} else {
res.status(200).json(json);
}
```
Any thoughts on this?
Flags: needinfo?(dustin)
Reporter | ||
Comment 14•6 years ago
|
||
How would statusCode get set to 204? I think the condition to check for is that the argument to `res.reply` is undefined.
Flags: needinfo?(dustin)
Assignee | ||
Comment 15•6 years ago
|
||
You mean adding something like this, I am new to this repo so got alot of doubts :P
```
if (!json) {
return res.status(204).json(json);
}
// If JSON was valid or validation was skipped then reply with 200 OK
res.status(200).json(json);
```
Flags: needinfo?(dustin)
Reporter | ||
Comment 16•6 years ago
|
||
The `!json` part is right, but I think you will want a simple `.send()` after that rather than `.json(json)` (which will probably fail).
Flags: needinfo?(dustin)
Assignee | ||
Comment 17•6 years ago
|
||
I was working on the tests, I don't see any test suite in which this can be done.
Is there any existing suite which can support this test? Or I need to create a new test suite?
Flags: needinfo?(dustin)
Reporter | ||
Comment 18•6 years ago
|
||
https://github.com/taskcluster/taskcluster-lib-api/blob/master/test/validate_test.js is probably a good choice (since in a way this is "validating" a non-JSON response)
Flags: needinfo?(dustin)
Assignee | ||
Comment 19•6 years ago
|
||
Created a PR here
https://github.com/taskcluster/taskcluster-lib-api/pull/145
Flags: needinfo?(dustin)
Reporter | ||
Updated•6 years ago
|
Flags: needinfo?(dustin)
Reporter | ||
Comment 20•6 years ago
|
||
OK, I've released tc-lib-api as version 12.7.0.
Next up is to upgrade tc-lib-api in a few applications and use this new feature. From grepping in my repositories:
p/taskcluster-auth/src/v1.js: return res.status(204).send();
p/taskcluster-auth/src/v1.js: return res.status(204).send();
p/taskcluster-purge-cache/src/api.js: res.status(204).send();
I think we need to do this for taskcluster/taskcluster-purge-cache and taskcluster/taskcluster-auth. Do you want to make up some quick PR's for those two repositories?
Assignee | ||
Comment 21•6 years ago
|
||
Yes. I would love to work on it.
Do we need to add a new issue for it in here? Or we are going to make those changes as part of this bug itself?
Flags: needinfo?(dustin)
Assignee | ||
Comment 23•6 years ago
|
||
Created PR in both the repos here
https://github.com/taskcluster/taskcluster-auth/pull/187
https://github.com/taskcluster/taskcluster-purge-cache/pull/36
Assignee | ||
Comment 24•6 years ago
|
||
Above to PR's are merged.
Does this bugs status needs to be updated?
Flags: needinfo?(dustin)
Reporter | ||
Comment 25•6 years ago
|
||
Yep, I think we're done here. Thanks!
I noticed you have Joi-related repo in your github org. If schemas are of interest to you, there are some issues on
https://github.com/taskcluster/react-schema-viewer
that you might have a look at. Otherwise, please let me know if I can help you find a next step!
Status: REOPENED → RESOLVED
Closed: 7 years ago → 6 years ago
Flags: needinfo?(dustin)
Resolution: --- → FIXED
Assignee | ||
Comment 26•6 years ago
|
||
Thanks Dustin.
I will look into the react-schema-viewer repo issues.
I am interested to work in other repos also.
It will be great, if I can get something for next step.
Updated•6 years ago
|
Component: Platform Libraries → Services
You need to log in
before you can comment on or make changes to this bug.
Description
•