Repository navigation
#1210 attempt to fix null body issue - #1237
finnetrolle wants to merge 2 commits into
Conversation
| this.url = checkNotNull(url, "url"); | ||
| this.headers = checkNotNull(headers, "headers of %s %s", method, url); | ||
| this.body = body; | ||
| this.body = (body != null) ? body : Body.EMPTY; |
There was a problem hiding this comment.
That is going to be a problem...
https://github.com/OpenFeign/feign/blob/master/httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java#L135
There are clients out there that change behavior when body is null
There was a problem hiding this comment.
Hmmm this is strange. Tests run without any issues. Ok, I have another suggestion. We can just return empty byte array when call Request::body and keep Body null. But I think this is not a good solution.
Another solution implemented.
There was a problem hiding this comment.
The reason the tests run without any issues is because the case is probably not covered with them. The erroneous behavior would appear in runtime. I didn't check it out - the point is that you cannot rely solely on the unit tests.
|
What is the issue you are trying to solve here? |
|
Hi velo, This is the issue #1210 |
|
Changes added. Kindly review updated PR |
| public byte[] body() { | ||
| return body.data; | ||
| return (body == null) | ||
| ? Body.EMPTY.data |
There was a problem hiding this comment.
any particular reason to not return null?
There was a problem hiding this comment.
FWIW, I hate all the nulls and mutable code we have laying around (specially on classes that mix mutable an immutable code)
That is probably an awesome, compatibility breaking clean up for feign 11.
There was a problem hiding this comment.
any particular reason to not return
null?
Well, data is on object so somebody could call body().length and face NullPointerException. If we provide empty array - we'll avoid such problem
There was a problem hiding this comment.
We could add an isEmpty function instead to make our intentions clearer. Thoughts?
There was a problem hiding this comment.
For Body? Honestly I didn't get the idea clearly. Could you provide a bit more info please?
There was a problem hiding this comment.
We can add a method to Request that indicates if the request is "Empty" or not, instead of relying on null checks. For this particular issue we can leave the current implementation alone, but add an additional method, such as hasBody to deal with the state check.
/**
* The Request body. Can be {@code null}
*
* @returns the Request body as a byte array.
*/
public byte[] body() {
return body != null ? body.data : null;
}
/**
* Returns if this Request has a Body defined.
*
* @returns {@code true} if this request has a body defined, {@code false} otherwise.
*/
public boolean hasBody() {
return body != null ? body.data != null : false;
}This type of change accomplishes two things. First, it doesn't break compatibility for older clients. Second it creates a pattern that encourages users to check first before use. In a breaking change, we could update the body method to return an Optional to be even more explicit.
| */ | ||
| public Charset charset() { | ||
| return body.encoding; | ||
| return (body == null) |
There was a problem hiding this comment.
This change will break the documentation, which states that this can return null
Hi all,
This is an attempt to fix an issue. I think if we have null-body for request it is better to provide null back if client ask Request#Body. But in fact we're sending back not Body but body's data. In that case I don't think null is ok for byte array. So I decided to create constant EMPTY body and when we have null body to create Request we can just put that constant instead null.