Skip to content

Commit

Permalink
fix: Response.protocolVersion may cause NPE (#2351)
Browse files Browse the repository at this point in the history
* fix: set Response.protocolVersion default value

* refactor: add default protocol version field

* refactor: change to default protocol version value using builder field.
  • Loading branch information
etff authored Mar 16, 2024
1 parent a5a1cd3 commit 3051df1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 4 additions & 3 deletions core/src/main/java/feign/Response.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 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
Expand Down Expand Up @@ -50,13 +50,14 @@ public static Builder builder() {
}

public static final class Builder {
private static final ProtocolVersion DEFAULT_PROTOCOL_VERSION = ProtocolVersion.HTTP_1_1;
int status;
String reason;
Map<String, Collection<String>> headers;
Body body;
Request request;
private RequestTemplate requestTemplate;
private ProtocolVersion protocolVersion = ProtocolVersion.HTTP_1_1;
private ProtocolVersion protocolVersion = DEFAULT_PROTOCOL_VERSION;

Builder() {}

Expand Down Expand Up @@ -136,7 +137,7 @@ public Builder request(Request request) {

/** HTTP protocol version */
public Builder protocolVersion(ProtocolVersion protocolVersion) {
this.protocolVersion = protocolVersion;
this.protocolVersion = (protocolVersion != null) ? protocolVersion : DEFAULT_PROTOCOL_VERSION;
return this;
}

Expand Down
17 changes: 16 additions & 1 deletion core/src/test/java/feign/ResponseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 The Feign Authors
* Copyright 2012-2024 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
Expand Down Expand Up @@ -134,4 +134,19 @@ void statusCodesOfAnyValueAreAllowed() {
assertThat(response.status()).isEqualTo(statusCode);
});
}

@Test
void protocolVersionDefaultsToHttp1_1() {
Response response =
Response.builder()
.status(200)
.request(
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.protocolVersion(null)
.body(new byte[0])
.build();

assertThat(response.protocolVersion()).isEqualTo(Request.ProtocolVersion.HTTP_1_1);
assertThat(response.toString()).startsWith("HTTP/1.1 200");
}
}

0 comments on commit 3051df1

Please sign in to comment.