Skip to content

Commit ba3ecbf

Browse files
authored
fix: Response.protocolVersion may cause NPE (#2351)
* fix: set Response.protocolVersion default value * refactor: add default protocol version field * refactor: change to default protocol version value using builder field.
1 parent bacdca7 commit ba3ecbf

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

core/src/main/java/feign/Response.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 The Feign Authors
2+
* Copyright 2012-2024 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -51,13 +51,14 @@ public static Builder builder() {
5151
}
5252

5353
public static final class Builder {
54+
private static final ProtocolVersion DEFAULT_PROTOCOL_VERSION = ProtocolVersion.HTTP_1_1;
5455
int status;
5556
String reason;
5657
Map<String, Collection<String>> headers;
5758
Body body;
5859
Request request;
5960
private RequestTemplate requestTemplate;
60-
private ProtocolVersion protocolVersion = ProtocolVersion.HTTP_1_1;
61+
private ProtocolVersion protocolVersion = DEFAULT_PROTOCOL_VERSION;
6162

6263
Builder() {}
6364

@@ -125,7 +126,7 @@ public Builder request(Request request) {
125126
* HTTP protocol version
126127
*/
127128
public Builder protocolVersion(ProtocolVersion protocolVersion) {
128-
this.protocolVersion = protocolVersion;
129+
this.protocolVersion = (protocolVersion != null) ? protocolVersion : DEFAULT_PROTOCOL_VERSION;
129130
return this;
130131
}
131132

core/src/test/java/feign/ResponseTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 The Feign Authors
2+
* Copyright 2012-2024 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -110,4 +110,17 @@ void statusCodesOfAnyValueAreAllowed() {
110110
assertThat(response.status()).isEqualTo(statusCode);
111111
});
112112
}
113+
114+
@Test
115+
void protocolVersionDefaultsToHttp1_1() {
116+
Response response = Response.builder()
117+
.status(200)
118+
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
119+
.protocolVersion(null)
120+
.body(new byte[0])
121+
.build();
122+
123+
assertThat(response.protocolVersion()).isEqualTo(Request.ProtocolVersion.HTTP_1_1);
124+
assertThat(response.toString()).startsWith("HTTP/1.1 200");
125+
}
113126
}

0 commit comments

Comments
 (0)