Skip to content

Commit

Permalink
Merge pull request #386 from andreaTP/no-close-is
Browse files Browse the repository at this point in the history
Don't close an InputStream for the user
  • Loading branch information
baywet authored Jun 9, 2023
2 parents 2dab4d4 + a72587a commit 2a1d766
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.4.4] - 2023-06-09

### Added

- Fix a bug where the OkHttp client would close InputStream responses before they reach the user code

## [0.4.3] - 2023-04-17

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ public <ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull fina
return CompletableFuture.completedFuture(null);
} else {
if(targetClass == InputStream.class) {
closeResponse = false;
final ResponseBody body = response.body();
if(body == null) {
closeResponse = false;
return CompletableFuture.completedFuture(null);
}
final InputStream rawInputStream = body.byteStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.*;
import io.opentelemetry.api.GlobalOpenTelemetry;
import okio.Okio;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -13,6 +14,9 @@
import com.microsoft.kiota.serialization.ParseNodeFactory;
import com.microsoft.kiota.HttpMethod;
import com.microsoft.kiota.RequestInformation;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.io.InputStream;
Expand Down Expand Up @@ -62,20 +66,30 @@ public Request test() throws Exception {
public void SendStreamReturnsUsableStream(int statusCode) throws Exception {
final var authenticationProviderMock = mock(AuthenticationProvider.class);
when(authenticationProviderMock.authenticateRequest(any(RequestInformation.class), any(Map.class))).thenReturn(CompletableFuture.completedFuture(null));
final var text = "my-demo-text";
final var bufferedSource = Okio.buffer(Okio.source(new ByteArrayInputStream(text.getBytes("UTF-8"))));
final var client = getMockClient(new Response.Builder()
.code(statusCode)
.message("OK")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost").build())
.body(ResponseBody.create("".getBytes("UTF-8"), MediaType.parse("application/json")))
.body(ResponseBody.create(bufferedSource, MediaType.parse("application/binary"), text.getBytes("UTF-8").length))
.build());
final var requestAdapter = new OkHttpRequestAdapter(authenticationProviderMock, null, null, client);
final var requestInformation = new RequestInformation() {{
setUri(new URI("https://localhost"));
httpMethod = HttpMethod.GET;
}};
final var response = requestAdapter.sendPrimitiveAsync(requestInformation, InputStream.class, null).get();
assertNotNull(response);
InputStream response = null;
try {
response = requestAdapter.sendPrimitiveAsync(requestInformation, InputStream.class, null).get();
assertNotNull(response);
assertEquals(text, new String(response.readAllBytes(), StandardCharsets.UTF_8));
} finally {
if (response != null) {
response.close();
}
}
}
@ParameterizedTest
@ValueSource(ints = {200, 201, 202, 203, 204})
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ org.gradle.caching=true
mavenGroupId = com.microsoft.kiota
mavenMajorVersion = 0
mavenMinorVersion = 4
mavenPatchVersion = 3
mavenPatchVersion = 4
mavenArtifactSuffix =

#These values are used to run functional tests
Expand Down

0 comments on commit 2a1d766

Please sign in to comment.