Skip to content

#1210 attempt to fix null body issue - #1237

Closed
finnetrolle wants to merge 2 commits into
OpenFeign:masterfrom
finnetrolle:#1210
Closed

finnetrolle wants to merge 2 commits into
OpenFeign:masterfrom
finnetrolle:#1210

Conversation

@finnetrolle

Copy link
Copy Markdown
Contributor

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.

Comment thread core/src/main/java/feign/Request.java Outdated
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@finnetrolle finnetrolle Jun 9, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@nikolas-charalambidis nikolas-charalambidis Aug 10, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@velo velo added the breaking change Change that proposes a non-backward compatible breaking change label Jun 9, 2020
@velo

velo commented Jun 9, 2020

Copy link
Copy Markdown
Member

What is the issue you are trying to solve here?

@finnetrolle

Copy link
Copy Markdown
Contributor Author

Hi velo, This is the issue #1210

@finnetrolle

Copy link
Copy Markdown
Contributor Author

Changes added. Kindly review updated PR

public byte[] body() {
return body.data;
return (body == null)
? Body.EMPTY.data

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any particular reason to not return null?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could add an isEmpty function instead to make our intentions clearer. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For Body? Honestly I didn't get the idea clearly. Could you provide a bit more info please?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you, @kdavisk6
Got it now

@finnetrolle
finnetrolle requested a review from velo June 11, 2020 13:33
@kdavisk6 kdavisk6 added the feedback provided Feedback has been provided to the author label Jun 17, 2020
@finnetrolle
finnetrolle requested a review from kdavisk6 June 20, 2020 01:03
*/
public Charset charset() {
return body.encoding;
return (body == null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change will break the documentation, which states that this can return null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Change that proposes a non-backward compatible breaking change feedback provided Feedback has been provided to the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants