diff --git a/CHANGELOG.md b/CHANGELOG.md index 8923355388..da5702d3ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ **Changed** - - Nothing yet! + - The built-in `OptionalConverterFactory` is now public to allow installing it before other converters which consume all types (e.g., Moshi, Gson, Jackson, etc.). **Fixed** diff --git a/retrofit/src/main/java/retrofit2/OptionalConverterFactory.java b/retrofit/src/main/java/retrofit2/OptionalConverterFactory.java index 4c628b7c7c..6d50c83e32 100644 --- a/retrofit/src/main/java/retrofit2/OptionalConverterFactory.java +++ b/retrofit/src/main/java/retrofit2/OptionalConverterFactory.java @@ -25,9 +25,24 @@ import okhttp3.ResponseBody; import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; +/** + * A {@link Converter.Factory} which supports Java's {@link Optional} to wrap null values from + * another converter. + *
+ * This factory is installed by default on the JVM and Android API 24+. If you are using another
+ * converter which tries to serialize all types, such as Moshi or Gson, the default installation
+ * of this factory never gets a chance to run. To work around this, you can explicitly install this
+ * factory before your serialization library converter.
+ */
@IgnoreJRERequirement // Only added when Optional is available (Java 8+ / Android API 24+).
@TargetApi(24)
-final class OptionalConverterFactory extends Converter.Factory {
+public final class OptionalConverterFactory extends Converter.Factory {
+ public static OptionalConverterFactory create() {
+ return new OptionalConverterFactory();
+ }
+
+ OptionalConverterFactory() {}
+
@Override
public @Nullable Converter