-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow JSON formatter to return a blank body #618
Conversation
Seems like a blank body technically also is not a JSON :) Nevertheless it's more preferable for situation where |
Yeah, I considered also making the |
I think this semantically incorrect, whatever we think people expect. Today Grape will serialize and deserialize the values correctly. If you change it above you won't be able to anymore. 2.0.0-p353 :021 > MultiJson.load('null')
=> nil
2.0.0-p353 :023 > MultiJson.load('""')
=> "" Wit this change you would be returning an empty result, which isn't valid JSON, and you're not able to load it back.
Changing content-type on the fly would be an even further stretch I think :) |
To clarify what I am saying: today Grape is capable of returning both an empty string and a null. I am not saying the client should rely on that, but it's definitely by design and would be an API change. |
@@ -10,6 +10,7 @@ Next Release | |||
|
|||
* [#614](https://github.com/intridea/grape/pull/614): Params with `nil` value are now refused by `RegexpValidator` - [@dm1try](https://github.com/dm1try). | |||
* [#494](https://github.com/intridea/grape/issues/494): Fixed performance issue with requests carrying a large payload - [@dblock](https://github.com/dblock). | |||
* [#618](https://github.com/intridea/grape/pull/618): When body is `nil` or `''`, make the response a blank body even for JSON - [@mbleigh](https://github.com/mbleigh) |
There was a problem hiding this 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 period at the end :)
@dblock do you think it would be better to have an endpoint shortcut for a blank response? I considered using The major times this comes up for me are with |
Did you check whether raising an My 0.02c is that you shouldn't be returning an empty body for a DELETE, you should be returning the original object that has been deleted, but that's a preference. I would definitely vote for something that lets you return an empty body very explicitly. Maybe just return an instance of Grape::Response::EmptyBody or something like that? |
What do you think of a macro e.g. |
I think that would work. |
Bump. |
Any progress on this? It would be really nice to have a Grape equivalent for Rails' |
I went with @body = nil
status 204 |
I think most users expect that
body ''
orreturn nil
will end up with a blank response, but the current JSON formatter put it as""
instead. This fixes that.Made it a pull because I want to make sure no one is depending on it returning a blank string. Technically JSON is only valid if it's an Object or Array, but there may be a use case I'm not thinking of.