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 NoSuchMethodError when projects have OkHttp 3 dependency #342

Merged
merged 2 commits into from
Apr 5, 2021
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
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