Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/src/main/java/feign/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

? Body.EMPTY.encoding
: body.encoding;
}

/**
Expand All @@ -187,7 +189,9 @@ public Charset charset() {
* @see #charset()
*/
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

: body.data;
}

public boolean isBinary() {
Expand Down Expand Up @@ -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;

Expand Down
33 changes: 33 additions & 0 deletions core/src/test/java/feign/RequestTest.java
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());
}

}