Skip to content

Commit

Permalink
Fix Nullability warning in JsonAdapters
Browse files Browse the repository at this point in the history
Updating generated methods `toJson` to accept a non null `JsonWriter`.
Previously the IDE was raising an issue due to different method signature.

Fixes #74
  • Loading branch information
cortinico committed Dec 26, 2019
1 parent ee4b22d commit 64ce597
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 43 deletions.
2 changes: 1 addition & 1 deletion plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal const val X_OPERATION_ID = "x-operation-id"
internal const val X_UNSAFE_OPERATION = "x-unsafe-operation"

// Headers Names
internal const val HEADER_X_OPERATION_ID = "X-Operation-Id"
internal const val HEADER_X_OPERATION_ID = "X-Operation-ID"
internal const val HEADER_CONTENT_TYPE = "Content-Type"

abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
Expand Down
12 changes: 6 additions & 6 deletions plugin/src/main/resources/kotlin/tools/TypesAdapters.kt.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {
override fun fromNonNullString(nextString: String) : LocalDate = LocalDate.parse(nextString, formatter)
override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)
override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import com.squareup.moshi.Json

/**
* Represents a specific Pet in the store
* @property id Unique ID of this Pet
* @property category Optional category of the pet
* @property id Unique ID of this Pet
* @property name Name of this specific pet
* @property photoUrls Photo URls for this Pet on the bucket
* @property tags Pet status in the store
*/
data class Pet(
@Json(name = "name") @field:Json(name = "name") var name: String,
@Json(name = "photoUrls") @field:Json(name = "photoUrls") var photoUrls: List<String>,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "category") @field:Json(name = "category") var category: Category? = null,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "tags") @field:Json(name = "tags") var tags: List<Tag>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.math.BigDecimal
* Moshi Factory to handle all the custom types we want to support,
* such as [LocalDate], [ZonedDateTime], [BigDecimal].
*/
internal class TypesAdapterFactory : JsonAdapter.Factory {
class TypesAdapterFactory : JsonAdapter.Factory {
private val types = mapOf<Type, JsonAdapter<*>>(
LocalDate::class.java to LocalDateAdapter(),
ZonedDateTime::class.java to ZonedDateTimeAdapter(),
Expand Down Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {

override fun fromNonNullString(nextString: String): LocalDate = LocalDate.parse(nextString, formatter)

override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)

override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import java.lang.reflect.Type

internal class XNullableAdapterFactory : JsonAdapter.Factory {
class XNullableAdapterFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
if (annotations.any { it is XNullable }) {
return object : JsonAdapter<Any>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: getBracketsInParameterName"
)

@GET("/brackets/in/parameter/name")
fun getBracketsInParameterName(
@retrofit2.http.Query("page") page: String?,
Expand All @@ -55,7 +54,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_empty_endpoint"
)

@GET("/empty_endpoint")
fun getEmptyEndpoint(): Single<EmptyModel>

Expand All @@ -66,7 +64,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_format_endpoint"
)

@GET("/format_endpoint/{property_format}")
fun getFormatEndpoint(
@retrofit2.http.Path("property_format") propertyFormat: String
Expand All @@ -78,7 +75,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_nested_additional_properties"
)

@GET("/nested_additional_properties")
fun getNestedAdditionalProperties(): Single<NestedAdditionalProperties>

Expand All @@ -88,7 +84,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_nested_additional_properties_custom_description"
)

@GET("/nested_additional_properties/custom_description")
fun getNestedAdditionalPropertiesCustomDescription(): Single<NestedAdditionalPropertiesCustomDescription>

Expand All @@ -100,7 +95,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_property_array"
)

@GET("/property_array/{value_type}/{size}")
fun getPropertyArray(
@retrofit2.http.Path("value_type") valueType: String,
Expand All @@ -115,7 +109,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_property_map"
)

@GET("/property_map/{value_type}/{size}")
fun getPropertyMap(
@retrofit2.http.Path("value_type") valueType: String,
Expand All @@ -128,7 +121,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_required_type_endpoint"
)

@GET("/required/type_endpoint")
fun getRequiredTypeEndpoint(): Single<RequiredTypeResponses>

Expand All @@ -138,7 +130,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_reserved_keywords"
)

@GET("/reserved_keywords")
fun getReservedKeywords(): Single<ReservedKeywords>

Expand All @@ -148,7 +139,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_top_level_enum"
)

@GET("/top_level_enum")
fun getTopLevelEnum(): Single<TopLevelEnum>

Expand All @@ -159,7 +149,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_top_level_map"
)

@GET("/top_level_map/{size}")
fun getTopLevelMap(
@retrofit2.http.Path("size") size: String
Expand All @@ -172,7 +161,6 @@ interface ResourceApi {
@Headers(
"X-Operation-ID: get_type_endpoint"
)

@GET("/type_endpoint/{property_type}")
fun getTypeEndpoint(
@retrofit2.http.Path("property_type") propertyType: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_format_endpoint"
)

@GET("/xnullable/format_endpoint/{property_format}")
fun getXnullableFormatEndpoint(
@retrofit2.http.Path("property_format") propertyFormat: String
Expand All @@ -39,7 +38,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_nested_additional_properties"
)

@GET("/xnullable/nested_additional_properties")
fun getXnullableNestedAdditionalProperties(): Single<XnullableNestedAdditionalProperties>

Expand All @@ -51,7 +49,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_property_array"
)

@GET("/xnullable/property_array/{value_type}/{size}")
fun getXnullablePropertyArray(
@retrofit2.http.Path("value_type") valueType: String,
Expand All @@ -66,7 +63,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_property_map"
)

@GET("/xnullable/property_map/{value_type}/{size}")
fun getXnullablePropertyMap(
@retrofit2.http.Path("value_type") valueType: String,
Expand All @@ -80,7 +76,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_required_property_array"
)

@GET("/xnullable/required/property_array/{size}")
fun getXnullableRequiredPropertyArray(
@retrofit2.http.Path("size") size: String
Expand All @@ -93,7 +88,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_required_property_map"
)

@GET("/xnullable/required/property_map/{size}")
fun getXnullableRequiredPropertyMap(
@retrofit2.http.Path("size") size: String
Expand All @@ -106,7 +100,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_required_type_endpoint"
)

@GET("/xnullable/required/type_endpoint/{property_type}")
fun getXnullableRequiredTypeEndpoint(
@retrofit2.http.Path("property_type") propertyType: String
Expand All @@ -119,7 +112,6 @@ interface XnullableApi {
@Headers(
"X-Operation-ID: get_xnullable_type_endpoint"
)

@GET("/xnullable/type_endpoint/{property_type}")
fun getXnullableTypeEndpoint(
@retrofit2.http.Path("property_type") propertyType: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {

override fun fromNonNullString(nextString: String): LocalDate = LocalDate.parse(nextString, formatter)

override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)

override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}

0 comments on commit 64ce597

Please sign in to comment.