Repository navigation
#1210 attempt to fix null body issue #1237
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,7 +177,9 @@ public Map<String, Collection<String>> headers() { | |
| * @return the current character set for the request, may be {@literal null} for binary data. | ||
| */ | ||
| public Charset charset() { | ||
| return body.encoding; | ||
| return (body == null) | ||
| ? Body.EMPTY.encoding | ||
| : body.encoding; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -187,7 +189,9 @@ public Charset charset() { | |
| * @see #charset() | ||
| */ | ||
| public byte[] body() { | ||
| return body.data; | ||
| return (body == null) | ||
| ? Body.EMPTY.data | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any particular reason to not return
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I hate all the That is probably an awesome, compatibility breaking clean up for feign 11.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, data is on object so somebody could call body().length and face NullPointerException. If we provide empty array - we'll avoid such problem
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add a method to /**
* 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, @kdavisk6 |
||
| : body.data; | ||
| } | ||
|
|
||
| public boolean isBinary() { | ||
|
|
@@ -376,6 +380,8 @@ public RequestTemplate requestTemplate() { | |
| @Experimental | ||
| public static class Body { | ||
|
|
||
| public static final Body EMPTY = new Body(new byte[0]); | ||
|
|
||
| private Charset encoding; | ||
| private byte[] data; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Copyright 2012-2020 The Feign Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package feign; | ||
|
|
||
| import feign.Request.HttpMethod; | ||
| import java.util.Collections; | ||
| import org.junit.Test; | ||
| import static feign.assertj.FeignAssertions.assertThat; | ||
|
|
||
| public class RequestTest { | ||
|
|
||
| @Test | ||
| public void testNullBodyShouldBeReplacedByEmptyConstant() { | ||
| Request request = Request.create(HttpMethod.GET, | ||
| "https://github.com/OpenFeign/feign", | ||
| Collections.emptyMap(), | ||
| (Request.Body) null, | ||
| (RequestTemplate) null); | ||
| assertThat(request.body()).isEqualTo(Request.Body.EMPTY.asBytes()); | ||
| } | ||
|
|
||
| } |
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 change will break the documentation, which states that this can return
null