diff --git a/README.md b/README.md index c4246b1c7..354b815ef 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,13 @@ In `pom.xml` in the `dependencies` section: ``` +### OkHttp platform selection + +The HTTP library's Gradle module metadata selects the OkHttp JVM or Android variant for the consuming platform. Maven consumers receive `okhttp-jvm` through the published POM. + +OkHttp 5.5.1's Android variant requires `compileSdk` 37 or later and AndroidX. Use Android Gradle Plugin 9.1.1 or later with JDK 17 or later, and set `android.useAndroidX=true` in `gradle.properties`. The Java 8 and Android minimum runtime versions are unchanged. + +Android consumers should use a repository that serves Gradle module metadata. If only the Maven POM is available, exclude `okhttp-jvm` and add `okhttp-android` explicitly to avoid including both variants. ### Default OkHttp middleware `KiotaClientFactory` applies URL replacement, query parameter name decoding, and User-Agent to the diff --git a/components/http/okHttp/build.gradle b/components/http/okHttp/build.gradle index d137da961..beb4ce83f 100644 --- a/components/http/okHttp/build.gradle +++ b/components/http/okHttp/build.gradle @@ -97,6 +97,14 @@ publishing { version = "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenCentralSnapshotArtifactSuffix}" from components.java pom.withXml { + // Gradle selects an OkHttp variant from module metadata; Maven needs the JVM artifact. + def okHttpDependencies = asNode().dependencies.dependency.findAll { + it.groupId.text() == 'com.squareup.okhttp3' && it.artifactId.text() == 'okhttp' + } + if (okHttpDependencies.size() != 1) { + throw new GradleException('Expected exactly one OkHttp dependency in the Maven POM') + } + okHttpDependencies[0].artifactId[0].setValue('okhttp-jvm') def pomFile = file("${project.buildDir}/generated-pom.xml") writeTo(pomFile) } @@ -110,6 +118,43 @@ publishing { } } +tasks.register('verifyOkHttpPublicationMetadata') { + dependsOn 'generatePomFileForMavenPublication', 'generateMetadataFileForMavenPublication' + doLast { + def okHttpModules = ['okhttp', 'okhttp-jvm', 'okhttp-android'] + def expectedVersion = libs.versions.okhttp.get() + def pomFile = layout.buildDirectory.file('generated-pom.xml').get().asFile + def pom = new groovy.xml.XmlSlurper().parse(pomFile) + def pomDependencies = pom.dependencies.dependency.findAll { + it.groupId.text() == 'com.squareup.okhttp3' && it.artifactId.text() in okHttpModules + } + if (pomDependencies.size() != 1 || + pomDependencies[0].artifactId.text() != 'okhttp-jvm' || + pomDependencies[0].version.text() != expectedVersion || + pomDependencies[0].scope.text() != 'compile' || + !pomFile.getText('UTF-8').contains('do_not_remove: published-with-gradle-metadata')) { + throw new GradleException('Maven POM must expose okhttp-jvm and the Gradle metadata marker') + } + + def moduleFile = layout.buildDirectory.file('publications/maven/module.json').get().asFile + def metadata = new groovy.json.JsonSlurper().parse(moduleFile) + ['apiElements', 'runtimeElements'].each { variantName -> + def variant = metadata.variants.find { it.name == variantName } + def dependencies = variant?.dependencies?.findAll { + it.group == 'com.squareup.okhttp3' && it.module in okHttpModules + } + if (dependencies == null || dependencies.size() != 1 || + dependencies[0].module != 'okhttp' || + dependencies[0].version.requires != expectedVersion) { + throw new GradleException("Gradle ${variantName} must expose the generic OkHttp dependency") + } + } + } +} +tasks.named('check') { + dependsOn 'verifyOkHttpPublicationMetadata' +} + signing { sign publishing.publications.maven } diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index 74d6904ca..2c5c14a9d 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -378,9 +378,6 @@ private void closeResponse(boolean closeResponse, Response response) { if (targetClass == InputStream.class) { closeResponse = false; final ResponseBody body = response.body(); - if (body == null) { - return null; - } final InputStream rawInputStream = body.byteStream(); return (ModelType) rawInputStream; } @@ -599,10 +596,6 @@ private void closeResponse(boolean closeResponse, Response response) { try (final Scope scope = span.makeCurrent()) { final ResponseBody body = response.body(); // closing the response closes the body and stream - // https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/ - if (body == null) { - return null; - } final InputStream rawInputStream = body.byteStream(); final MediaType contentType = body.contentType(); if (contentType == null) { diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java index 896de69d1..8ebe49316 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java @@ -125,7 +125,7 @@ void sendStreamReturnsUsableStream(int statusCode) throws Exception { @ParameterizedTest @ValueSource(ints = {200, 201, 202, 203, 204, 304}) - void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception { + void sendStreamHandlesEmptyResponseByStatus(int statusCode) throws Exception { final var authenticationProviderMock = mock(AuthenticationProvider.class); authenticationProviderMock.authenticateRequest( any(RequestInformation.class), any(Map.class)); @@ -136,7 +136,7 @@ void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception { .message("OK") .protocol(Protocol.HTTP_1_1) .request(new Request.Builder().url("http://localhost").build()) - .body(null) + .body(ResponseBody.create("", null)) .build()); final var requestAdapter = new OkHttpRequestAdapter(authenticationProviderMock, null, null, client); @@ -149,7 +149,14 @@ void sendStreamReturnsNullOnNoContent(int statusCode) throws Exception { }; final var response = requestAdapter.sendPrimitive(requestInformation, null, InputStream.class); - assertNull(response); + if (statusCode == 204 || statusCode == 304) { + assertNull(response); + } else { + assertNotNull(response); + try (var stream = response) { + assertEquals(-1, stream.read()); + } + } } @ParameterizedTest @@ -165,7 +172,7 @@ void sendReturnsNullOnNoContent(int statusCode) throws Exception { .message("OK") .protocol(Protocol.HTTP_1_1) .request(new Request.Builder().url("http://localhost").build()) - .body(null) + .body(ResponseBody.create("", null)) .build()); final var requestAdapter = new OkHttpRequestAdapter(authenticationProviderMock, null, null, client); diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6a39802f4..af7ea046e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -okhttp = "4.12.1" +okhttp = "5.5.1" opentelemetry = "1.68.0" [libraries]