Skip to content

Commit

Permalink
Fix NoSuchMethodError when projects have OkHttp 3 dependency (#342)
Browse files Browse the repository at this point in the history
* Use deprecated methods

* Update README and add comments
  • Loading branch information
jimmyjames committed Apr 5, 2021
1 parent 719aa27 commit 3298cfd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 38 deletions.
35 changes: 0 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<dependencyManagement>
<dependencies>
<!-- Needed to avoid conflict with OkHttp being used in auth0-java and okhttp3 being used by spring -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
</dependencyManagement>
```

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).
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/auth0/net/CustomRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/auth0/net/EmptyBodyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/auth0/net/MultipartRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ public MultipartRequest<T> addHeader(String name, String value) {
}

@Override
@SuppressWarnings("deprecation")
public MultipartRequest<T> 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;
}
Expand Down

0 comments on commit 3298cfd

Please sign in to comment.