Merge pull request #655 from square/eric.2018-08-17.nullSafe-generated-adapters
Make generated adapters null-safe.
This commit is contained in:
commit
484fdfe4b6
3 changed files with 20 additions and 5 deletions
|
@ -1075,6 +1075,13 @@ class GeneratedAdaptersTest {
|
|||
.isEqualTo(HasNullableBoolean(null))
|
||||
assertThat(adapter.toJson(HasNullableBoolean(null))).isEqualTo("""{"boolean":null}""")
|
||||
}
|
||||
|
||||
@Test fun adaptersAreNullSafe() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val adapter = moshi.adapter(HasNonNullConstructorParameter::class.java)
|
||||
assertThat(adapter.fromJson("null")).isNull()
|
||||
assertThat(adapter.toJson(null)).isEqualTo("null")
|
||||
}
|
||||
}
|
||||
|
||||
// Has to be outside to avoid Types seeing an owning class
|
||||
|
|
|
@ -860,10 +860,11 @@ class KotlinJsonAdapterTest {
|
|||
val generatedAdapter = moshi.adapter(UsesGeneratedAdapter::class.java)
|
||||
val reflectionAdapter = moshi.adapter(UsesReflectionAdapter::class.java)
|
||||
|
||||
assertThat(generatedAdapter.javaClass.name)
|
||||
.contains("KotlinJsonAdapterTest_UsesGeneratedAdapterJsonAdapter")
|
||||
assertThat(reflectionAdapter.javaClass.name)
|
||||
.doesNotContain("KotlinJsonAdapterTest_UsesReflectionAdapterJsonAdapter")
|
||||
assertThat(generatedAdapter.toString())
|
||||
.isEqualTo("GeneratedJsonAdapter(KotlinJsonAdapterTest.UsesGeneratedAdapter).nullSafe()")
|
||||
assertThat(reflectionAdapter.toString())
|
||||
.isEqualTo("KotlinJsonAdapter(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest" +
|
||||
".UsesReflectionAdapter).nullSafe()")
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
|
@ -913,4 +914,11 @@ class KotlinJsonAdapterTest {
|
|||
.isEqualTo(HasNullableBoolean(null))
|
||||
assertThat(adapter.toJson(HasNullableBoolean(null))).isEqualTo("""{"boolean":null}""")
|
||||
}
|
||||
|
||||
@Test fun adaptersAreNullSafe() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val adapter = moshi.adapter(HasNonNullConstructorParameter::class.java)
|
||||
assertThat(adapter.fromJson("null")).isNull()
|
||||
assertThat(adapter.toJson(null)).isEqualTo("null")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ final class StandardJsonAdapters {
|
|||
|
||||
JsonClass jsonClass = rawType.getAnnotation(JsonClass.class);
|
||||
if (jsonClass != null && jsonClass.generateAdapter()) {
|
||||
return generatedAdapter(moshi, type, rawType);
|
||||
return generatedAdapter(moshi, type, rawType).nullSafe();
|
||||
}
|
||||
|
||||
if (rawType.isEnum()) {
|
||||
|
|
Loading…
Reference in a new issue