Skip to content

Commit

Permalink
Non-Nullable client return types (#124)
Browse files Browse the repository at this point in the history
* ISSUE-120: Non-Nullable client return types

* ISSUE-120: Force return type of http client methods to be non-nullable
```
no content -> ApiResponse<Unit>
single content schema: MyTypeOne -> ApiResponse<MyTypeOne>
multiple content schemas: -> ApiResponse<JsonNode> or ApiResponse<Any>
```
  • Loading branch information
averabaq authored Mar 22, 2022
1 parent 70ccb61 commit 904d8db
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ object ClientGeneratorUtils {
} ?: Unit::class.asTypeName()
}

return "ApiResponse".toClassName(packages.client)
.parameterizedBy(returnType.copy(nullable = true))
return "ApiResponse".toClassName(packages.client).parameterizedBy(returnType)
}

fun simpleClientName(resourceName: String) = "$resourceName${ClientType.SIMPLE_CLIENT_SUFFIX}"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/client-code/http-util.kt.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down
12 changes: 6 additions & 6 deletions src/test/resources/examples/okHttpClient/client/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ExamplePath1Client(
explodeListQueryParam: List<String>?,
queryParam2: Int?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> {
): ApiResponse<QueryResult> {
val httpUrl: HttpUrl = "$baseUrl/example-path-1"
.toHttpUrl()
.newBuilder()
Expand Down Expand Up @@ -70,7 +70,7 @@ class ExamplePath1Client(
content: Content,
explodeListQueryParam: List<String>?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example-path-1"
.toHttpUrl()
.newBuilder()
Expand Down Expand Up @@ -110,7 +110,7 @@ class ExamplePath2Client(
queryParam2: Int?,
ifNoneMatch: String?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Content?> {
): ApiResponse<Content> {
val httpUrl: HttpUrl = "$baseUrl/example-path-2/{path_param}"
.pathParam("{path_param}" to pathParam)
.toHttpUrl()
Expand Down Expand Up @@ -145,7 +145,7 @@ class ExamplePath2Client(
queryParam3: Boolean?,
ifNoneMatch: String?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example-path-2/{path_param}"
.pathParam("{path_param}" to pathParam)
.toHttpUrl()
Expand Down Expand Up @@ -180,7 +180,7 @@ class ExamplePath2Client(
pathParam: String,
ifMatch: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example-path-2/{path_param}"
.pathParam("{path_param}" to pathParam)
.toHttpUrl()
Expand Down Expand Up @@ -223,7 +223,7 @@ class ExamplePath3SubresourceClient(
ifMatch: String,
csvListQueryParam: List<String>?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example-path-3/{path_param}/subresource"
.pathParam("{path_param}" to pathParam)
.toHttpUrl()
Expand Down
12 changes: 6 additions & 6 deletions src/test/resources/examples/okHttpClient/client/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ExamplePath1Service(
explodeListQueryParam: List<String>?,
queryParam2: Int?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> =
): ApiResponse<QueryResult> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExamplePath1(explodeListQueryParam, queryParam2, additionalHeaders)
}
Expand All @@ -49,7 +49,7 @@ class ExamplePath1Service(
content: Content,
explodeListQueryParam: List<String>?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.postExamplePath1(content, explodeListQueryParam, additionalHeaders)
}
Expand Down Expand Up @@ -80,7 +80,7 @@ class ExamplePath2Service(
queryParam2: Int?,
ifNoneMatch: String?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Content?> =
): ApiResponse<Content> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExamplePath2PathParam(pathParam, queryParam2, ifNoneMatch, additionalHeaders)
}
Expand All @@ -91,7 +91,7 @@ class ExamplePath2Service(
queryParam3: Boolean?,
ifNoneMatch: String?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.headOperationIdExample(pathParam, queryParam3, ifNoneMatch, additionalHeaders)
}
Expand All @@ -102,7 +102,7 @@ class ExamplePath2Service(
pathParam: String,
ifMatch: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.putExamplePath2PathParam(firstModel, pathParam, ifMatch, additionalHeaders)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ class ExamplePath3SubresourceService(
ifMatch: String,
csvListQueryParam: List<String>?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.putExamplePath3PathParamSubresource(firstModel, pathParam, ifMatch, csvListQueryParam, additionalHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ExamplePath1Client(
queryParam2: Int?,
acceptHeader: String = "application/vnd.custom.media+xml",
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> {
): ApiResponse<QueryResult> {
val httpUrl: HttpUrl = "$baseUrl/example-path-1"
.toHttpUrl()
.newBuilder()
Expand Down Expand Up @@ -78,7 +78,7 @@ class ExamplePath2Client(
queryParam2: Int?,
accept: ContentType?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> {
): ApiResponse<QueryResult> {
val httpUrl: HttpUrl = "$baseUrl/example-path-2"
.toHttpUrl()
.newBuilder()
Expand Down Expand Up @@ -117,7 +117,7 @@ class MultipleResponseSchemasClient(
accept: ContentType?,
additionalHeaders: Map<String, String> =
emptyMap()
): ApiResponse<JsonNode?> {
): ApiResponse<JsonNode> {
val httpUrl: HttpUrl = "$baseUrl/multiple-response-schemas"
.toHttpUrl()
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ExamplePath1Service(
queryParam2: Int?,
acceptHeader: String = "application/vnd.custom.media+xml",
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> =
): ApiResponse<QueryResult> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExamplePath1(explodeListQueryParam, queryParam2, acceptHeader, additionalHeaders)
}
Expand Down Expand Up @@ -69,7 +69,7 @@ class ExamplePath2Service(
queryParam2: Int?,
accept: ContentType?,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<QueryResult?> =
): ApiResponse<QueryResult> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExamplePath2(explodeListQueryParam, queryParam2, accept, additionalHeaders)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ class MultipleResponseSchemasService(
accept: ContentType?,
additionalHeaders: Map<String, String> =
emptyMap()
): ApiResponse<JsonNode?> =
): ApiResponse<JsonNode> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getMultipleResponseSchemas(accept, additionalHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ExampleClient(
*
*/
@Throws(ApiException::class)
fun putExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> {
fun putExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example"
.toHttpUrl()
.newBuilder()
Expand All @@ -47,7 +47,7 @@ class ExampleClient(
*
*/
@Throws(ApiException::class)
fun postExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> {
fun postExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example"
.toHttpUrl()
.newBuilder()
Expand All @@ -70,7 +70,7 @@ class ExampleClient(
*
*/
@Throws(ApiException::class)
fun patchExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> {
fun patchExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example"
.toHttpUrl()
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class ExampleService(
private val apiClient: ExampleClient = ExampleClient(objectMapper, baseUrl, client)

@Throws(ApiException::class)
fun putExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> =
fun putExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.putExample(additionalHeaders)
}

@Throws(ApiException::class)
fun postExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> =
fun postExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.postExample(additionalHeaders)
}

@Throws(ApiException::class)
fun patchExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit?> =
fun patchExample(additionalHeaders: Map<String, String> = emptyMap()): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.patchExample(additionalHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ExampleClient(
pathB: String,
queryB: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example/{b}"
.pathParam("{b}" to pathB)
.toHttpUrl()
Expand Down Expand Up @@ -65,7 +65,7 @@ class ExampleClient(
bodySomeObject: SomeObject,
querySomeObject: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example"
.toHttpUrl()
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExampleService(
pathB: String,
queryB: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExampleB(pathB, queryB, additionalHeaders)
}
Expand All @@ -44,7 +44,7 @@ class ExampleService(
bodySomeObject: SomeObject,
querySomeObject: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.postExample(bodySomeObject, querySomeObject, additionalHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ExampleClient(
a: String,
b: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> {
): ApiResponse<Unit> {
val httpUrl: HttpUrl = "$baseUrl/example"
.toHttpUrl()
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ExampleService(
a: String,
b: String,
additionalHeaders: Map<String, String> = emptyMap()
): ApiResponse<Unit?> =
): ApiResponse<Unit> =
withCircuitBreaker(circuitBreakerRegistry, circuitBreakerName) {
apiClient.getExample(a, b, additionalHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Headers.Builder.header(key: String, value: String?): Headers.Builder = this.
}

@Throws(ApiException::class)
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T?> =
fun <T> Request.execute(client: OkHttpClient, objectMapper: ObjectMapper, typeRef: TypeReference<T>): ApiResponse<T> =
client.newCall(this).execute().use { response ->
when {
response.isSuccessful ->
Expand Down

0 comments on commit 904d8db

Please sign in to comment.