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))
|
.isEqualTo(HasNullableBoolean(null))
|
||||||
assertThat(adapter.toJson(HasNullableBoolean(null))).isEqualTo("""{"boolean":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
|
// 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 generatedAdapter = moshi.adapter(UsesGeneratedAdapter::class.java)
|
||||||
val reflectionAdapter = moshi.adapter(UsesReflectionAdapter::class.java)
|
val reflectionAdapter = moshi.adapter(UsesReflectionAdapter::class.java)
|
||||||
|
|
||||||
assertThat(generatedAdapter.javaClass.name)
|
assertThat(generatedAdapter.toString())
|
||||||
.contains("KotlinJsonAdapterTest_UsesGeneratedAdapterJsonAdapter")
|
.isEqualTo("GeneratedJsonAdapter(KotlinJsonAdapterTest.UsesGeneratedAdapter).nullSafe()")
|
||||||
assertThat(reflectionAdapter.javaClass.name)
|
assertThat(reflectionAdapter.toString())
|
||||||
.doesNotContain("KotlinJsonAdapterTest_UsesReflectionAdapterJsonAdapter")
|
.isEqualTo("KotlinJsonAdapter(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest" +
|
||||||
|
".UsesReflectionAdapter).nullSafe()")
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
|
@ -913,4 +914,11 @@ class KotlinJsonAdapterTest {
|
||||||
.isEqualTo(HasNullableBoolean(null))
|
.isEqualTo(HasNullableBoolean(null))
|
||||||
assertThat(adapter.toJson(HasNullableBoolean(null))).isEqualTo("""{"boolean":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);
|
JsonClass jsonClass = rawType.getAnnotation(JsonClass.class);
|
||||||
if (jsonClass != null && jsonClass.generateAdapter()) {
|
if (jsonClass != null && jsonClass.generateAdapter()) {
|
||||||
return generatedAdapter(moshi, type, rawType);
|
return generatedAdapter(moshi, type, rawType).nullSafe();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rawType.isEnum()) {
|
if (rawType.isEnum()) {
|
||||||
|
|
Loading…
Reference in a new issue