From 16fd809e4e5abd9dd2eb4a80cb3436f0e2b457b0 Mon Sep 17 00:00:00 2001 From: nosix Date: Wed, 4 Nov 2020 13:25:01 +0900 Subject: [PATCH] Fix: Response was null but response body type was declared as non-null https://github.com/square/retrofit/issues/3075 --- .../src/main/kotlin/com/example/api/CustomerService.kt | 2 +- backend-api/src/main/kotlin/com/example/api/Optional.kt | 8 ++++++++ .../com/example/backend/controller/CustomerController.kt | 5 +++-- .../test/kotlin/com/example/backend/CustomerApiTests.kt | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 backend-api/src/main/kotlin/com/example/api/Optional.kt diff --git a/backend-api/src/main/kotlin/com/example/api/CustomerService.kt b/backend-api/src/main/kotlin/com/example/api/CustomerService.kt index 7b5ac45..a47139f 100644 --- a/backend-api/src/main/kotlin/com/example/api/CustomerService.kt +++ b/backend-api/src/main/kotlin/com/example/api/CustomerService.kt @@ -11,7 +11,7 @@ interface CustomerService { suspend fun getAllCustomers(): List @GET("customers/{id}") - suspend fun getCustomer(@Path("id") id: Long): Customer? + suspend fun getCustomer(@Path("id") id: Long): Optional @GET("customers/search") suspend fun getCustomersByLastName( diff --git a/backend-api/src/main/kotlin/com/example/api/Optional.kt b/backend-api/src/main/kotlin/com/example/api/Optional.kt new file mode 100644 index 0000000..d245469 --- /dev/null +++ b/backend-api/src/main/kotlin/com/example/api/Optional.kt @@ -0,0 +1,8 @@ +package com.example.api + +import kotlinx.serialization.Serializable + +// FIXME https://github.com/square/retrofit/issues/3075 + +@Serializable +data class Optional(val unboxed: T?) \ No newline at end of file diff --git a/backend/src/main/kotlin/com/example/backend/controller/CustomerController.kt b/backend/src/main/kotlin/com/example/backend/controller/CustomerController.kt index 559eba5..64be850 100644 --- a/backend/src/main/kotlin/com/example/backend/controller/CustomerController.kt +++ b/backend/src/main/kotlin/com/example/backend/controller/CustomerController.kt @@ -2,6 +2,7 @@ package com.example.backend.controller import com.example.api.Customer import com.example.api.CustomerService +import com.example.api.Optional import com.example.backend.repository.CustomerRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map @@ -32,8 +33,8 @@ class CustomerController( @GetMapping("customers/{id}") override suspend fun getCustomer( @PathVariable("id") id: Long - ): Customer? { - return repository.findById(id)?.asApi() + ): Optional { + return Optional(repository.findById(id)?.asApi()) } @GetMapping("customers/search") diff --git a/backend/src/test/kotlin/com/example/backend/CustomerApiTests.kt b/backend/src/test/kotlin/com/example/backend/CustomerApiTests.kt index 98c6d77..d6065ed 100644 --- a/backend/src/test/kotlin/com/example/backend/CustomerApiTests.kt +++ b/backend/src/test/kotlin/com/example/backend/CustomerApiTests.kt @@ -106,7 +106,7 @@ class CustomerApiTests { Mockito .`when`(customerRepository.findById(1)) .thenReturn(entity) - val customer = customerService.getCustomer(1) + val customer = customerService.getCustomer(1).unboxed assertEquals(expected, customer) }