Skip to content

Commit d6eac54

Browse files
committed
Make optional converter public
1 parent 9b5e630 commit d6eac54

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
**Changed**
1010

11-
- Nothing yet!
11+
- The built-in `OptionalConverterFactory` is now public to allow installing it before other converters which consume all types (e.g., Moshi, Gson, Jackson, etc.).
1212

1313
**Fixed**
1414

retrofit/src/main/java/retrofit2/OptionalConverterFactory.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@
2525
import okhttp3.ResponseBody;
2626
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
2727

28+
/**
29+
* A {@link Converter.Factory} which supports Java's {@link Optional} to wrap null values from
30+
* another converter.
31+
* <p>
32+
* This factory is installed by default on the JVM and Android API 24+. If you are using another
33+
* converter which tries to serialize all types, such as Moshi or Gson, the default installation
34+
* of this factory never gets a chance to run. To work around this, you can explicitly install this
35+
* factory before your serialization library converter.
36+
*/
2837
@IgnoreJRERequirement // Only added when Optional is available (Java 8+ / Android API 24+).
2938
@TargetApi(24)
30-
final class OptionalConverterFactory extends Converter.Factory {
39+
public final class OptionalConverterFactory extends Converter.Factory {
40+
public static OptionalConverterFactory create() {
41+
return new OptionalConverterFactory();
42+
}
43+
44+
OptionalConverterFactory() {}
45+
3146
@Override
3247
public @Nullable Converter<ResponseBody, ?> responseBodyConverter(
3348
Type type, Annotation[] annotations, Retrofit retrofit) {
@@ -43,7 +58,7 @@ final class OptionalConverterFactory extends Converter.Factory {
4358

4459
@IgnoreJRERequirement
4560
static final class OptionalConverter<T> implements Converter<ResponseBody, Optional<T>> {
46-
final Converter<ResponseBody, T> delegate;
61+
private final Converter<ResponseBody, T> delegate;
4762

4863
OptionalConverter(Converter<ResponseBody, T> delegate) {
4964
this.delegate = delegate;

0 commit comments

Comments
 (0)