Skip to content
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

fix: Response.protocolVersion may cause NPE #2351

Merged
merged 3 commits into from
Mar 16, 2024
Merged
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
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 @@ -51,13 +51,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 @@ -125,7 +126,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
15 changes: 14 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 @@ -110,4 +110,17 @@ 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");
}
}