From 3298cfd46d54fc555232a0621e500ee9e0577d20 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Mon, 5 Apr 2021 11:12:26 -0500 Subject: [PATCH] Fix `NoSuchMethodError` when projects have OkHttp 3 dependency (#342) * Use deprecated methods * Update README and add comments --- README.md | 35 ------------------- .../java/com/auth0/net/CustomRequest.java | 5 ++- .../java/com/auth0/net/EmptyBodyRequest.java | 5 ++- .../java/com/auth0/net/MultipartRequest.java | 5 ++- 4 files changed, 12 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index e3c35b12..1303aaf7 100644 --- a/README.md +++ b/README.md @@ -31,41 +31,6 @@ implementation 'com.auth0:auth0:1.28.0' The Auth0 Authentication API and User's Management API are available for Android in the `auth0.android` library. Check https://github.com/auth0/auth0.android for more information. -### Using with Spring Dependency Management - -This library use OkHttp version 4 as the networking client used to make requests to the Auth0 Authentication and Management APIs. If you are using Spring's depdendency management, you may encounter `java.lang.NoSuchMethodError` errors when making requests, related to Spring's dependency management using OkHttp 3. To resolve this issue, you can override Spring's dependency on OkHttp: - -Maven ([more information](https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/)): -```xml - - - - - com.squareup.okhttp3 - okhttp - 4.9.0 - - - -``` - -Gradle ([more information](https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/)): -```java -plugins { - ... - // spring dependency plugin will define okhttp3, which will have issues with okhttp4 in auth0-java - id "io.spring.dependency-management" version "1.0.10.RELEASE" -} - -dependencyManagement { - imports { - mavenBom "com.squareup.okhttp3:okhttp-bom:4.9.0" - } -} -``` - -More information can be found in this [Github issue](https://github.com/auth0/auth0-java/issues/324). - ## Auth API The implementation is based on the [Authentication API Docs](https://auth0.com/docs/api/authentication). diff --git a/src/main/java/com/auth0/net/CustomRequest.java b/src/main/java/com/auth0/net/CustomRequest.java index 4e61ba4b..45ad2af5 100644 --- a/src/main/java/com/auth0/net/CustomRequest.java +++ b/src/main/java/com/auth0/net/CustomRequest.java @@ -42,12 +42,15 @@ public CustomRequest(OkHttpClient client, String url, String method, TypeReferen } @Override + @SuppressWarnings("deprecation") protected RequestBody createRequestBody() throws IOException { if (body == null && parameters.isEmpty()) { return null; } byte[] jsonBody = mapper.writeValueAsBytes(body != null ? body : parameters); - return RequestBody.create(jsonBody, MediaType.parse(CONTENT_TYPE_APPLICATION_JSON)); + // Use OkHttp v3 signature to ensure binary compatibility between v3 and v4 + // https://github.com/auth0/auth0-java/issues/324 + return RequestBody.create(MediaType.parse(CONTENT_TYPE_APPLICATION_JSON), jsonBody); } @Override diff --git a/src/main/java/com/auth0/net/EmptyBodyRequest.java b/src/main/java/com/auth0/net/EmptyBodyRequest.java index 8a199ebc..508414c8 100644 --- a/src/main/java/com/auth0/net/EmptyBodyRequest.java +++ b/src/main/java/com/auth0/net/EmptyBodyRequest.java @@ -18,8 +18,11 @@ public EmptyBodyRequest(OkHttpClient client, String url, String method, TypeRefe } @Override + @SuppressWarnings("deprecation") protected RequestBody createRequestBody() { - return RequestBody.create(new byte[0], null); + // Use OkHttp v3 signature to ensure binary compatibility between v3 and v4 + // https://github.com/auth0/auth0-java/issues/324 + return RequestBody.create(null, new byte[0]); } @Override diff --git a/src/main/java/com/auth0/net/MultipartRequest.java b/src/main/java/com/auth0/net/MultipartRequest.java index d4e91313..e296a538 100644 --- a/src/main/java/com/auth0/net/MultipartRequest.java +++ b/src/main/java/com/auth0/net/MultipartRequest.java @@ -72,14 +72,17 @@ public MultipartRequest addHeader(String name, String value) { } @Override + @SuppressWarnings("deprecation") public MultipartRequest addPart(String name, File file, String mediaType) { assertNotNull(name, "name"); assertNotNull(name, "file"); if (!file.exists()) { throw new IllegalArgumentException("Failed to add part because the file specified cannot be found."); } + // Use OkHttp v3 signature to ensure binary compatibility between v3 and v4 + // https://github.com/auth0/auth0-java/issues/324 bodyBuilder.addFormDataPart(name, file.getName(), - RequestBody.create(file, MediaType.parse(mediaType))); + RequestBody.create(MediaType.parse(mediaType), file)); partsCount++; return this; }