Skip to content

Commit afd3a78

Browse files
scala-sttp: fix for issue 15785 api returns unit. (OpenAPITools#18537)
1 parent 5e40fe7 commit afd3a78

5 files changed

Lines changed: 21 additions & 26 deletions

File tree

modules/openapi-generator/src/main/resources/scala-sttp/api.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import sttp.model.Method
1010

1111
{{#operations}}
1212
object {{classname}} {
13-
14-
def apply(baseUrl: String = "{{{basePath}}}") = new {{classname}}(baseUrl)
13+
def apply(baseUrl: String = "{{{basePath}}}") = new {{classname}}(baseUrl)
1514
}
1615

1716
class {{classname}}(baseUrl: String) {
@@ -20,7 +19,7 @@ class {{classname}}(baseUrl: String) {
2019
{{#javadocRenderer}}
2120
{{>javadoc}}
2221
{{/javadocRenderer}}
23-
def {{operationId}}({{>methodParameters}}): Request[{{#defaultReturnType}}Either[Either[String, String], Unit]{{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}{{/defaultReturnType}}, Any] =
22+
def {{operationId}}({{>methodParameters}}): Request[{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}, Any] =
2423
basicRequest
2524
.method(Method.{{httpMethod.toUpperCase}}, uri"$baseUrl{{{path}}}{{#queryParams.0}}?{{#queryParams}}{{baseName}}=${ {{{paramName}}} }{{^-last}}&{{/-last}}{{/queryParams}}{{/queryParams.0}}{{#isApiKey}}{{#isKeyInQuery}}{{^queryParams.0}}?{{/queryParams.0}}{{#queryParams.0}}&{{/queryParams.0}}{{keyParamName}}=${apiKey.value}&{{/isKeyInQuery}}{{/isApiKey}}")
2625
.contentType({{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}}){{#headerParams}}
@@ -36,7 +35,7 @@ class {{classname}}(baseUrl: String) {
3635
{{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}}
3736
).flatten){{/isMultipart}}{{/formParams.0}}{{#bodyParam}}
3837
.body({{paramName}}){{/bodyParam}}
39-
.response({{#defaultReturnType}}asEither(asString, ignore){{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}asJson{{/separateErrorChannel}}{{^separateErrorChannel}}asJsonAlwaysUnsafe{{/separateErrorChannel}}[{{>operationReturnType}}]{{/defaultReturnType}})
38+
.response({{#separateErrorChannel}}{{^returnType}}asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))){{/returnType}}{{#returnType}}asJson[{{>operationReturnType}}]{{/returnType}}{{/separateErrorChannel}}{{^separateErrorChannel}}{{^returnType}}asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))).getRight{{/returnType}}{{#returnType}}asJson[{{>operationReturnType}}].getRight{{/returnType}}{{/separateErrorChannel}})
4039

4140
{{/operation}}
4241
}

samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/FakeApi.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import sttp.client3._
1616
import sttp.model.Method
1717

1818
object FakeApi {
19-
20-
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new FakeApi(baseUrl)
19+
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new FakeApi(baseUrl)
2120
}
2221

2322
class FakeApi(baseUrl: String) {
@@ -38,6 +37,6 @@ class FakeApi(baseUrl: String) {
3837
.contentType("application/json")
3938
.header("_type", underscoreType.toString)
4039
.header("type_", typeWithUnderscore.toString)
41-
.response(asJson[Unit])
40+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
4241

4342
}

samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import sttp.client3._
1919
import sttp.model.Method
2020

2121
object PetApi {
22-
23-
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new PetApi(baseUrl)
22+
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new PetApi(baseUrl)
2423
}
2524

2625
class PetApi(baseUrl: String) {
@@ -57,7 +56,7 @@ class PetApi(baseUrl: String) {
5756
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
5857
.contentType("application/json")
5958
.header("api_key", apiKey.toString)
60-
.response(asJson[Unit])
59+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6160

6261
/**
6362
* Multiple status values can be provided with comma separated strings
@@ -150,7 +149,7 @@ class PetApi(baseUrl: String) {
150149
"name" -> name,
151150
"status" -> status
152151
))
153-
.response(asJson[Unit])
152+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
154153

155154
/**
156155
*

samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/StoreApi.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import sttp.client3._
1717
import sttp.model.Method
1818

1919
object StoreApi {
20-
21-
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new StoreApi(baseUrl)
20+
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new StoreApi(baseUrl)
2221
}
2322

2423
class StoreApi(baseUrl: String) {
@@ -37,7 +36,7 @@ class StoreApi(baseUrl: String) {
3736
basicRequest
3837
.method(Method.DELETE, uri"$baseUrl/store/order/${orderId}")
3938
.contentType("application/json")
40-
.response(asJson[Unit])
39+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
4140

4241
/**
4342
* Returns a map of status codes to quantities

samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/UserApi.scala

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import sttp.client3._
1818
import sttp.model.Method
1919

2020
object UserApi {
21-
22-
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new UserApi(baseUrl)
21+
def apply(baseUrl: String = "http://petstore.swagger.io/v2") = new UserApi(baseUrl)
2322
}
2423

2524
class UserApi(baseUrl: String) {
@@ -36,13 +35,13 @@ class UserApi(baseUrl: String) {
3635
* @param user Created user object
3736
*/
3837
def createUser(apiKey: String)(user: User
39-
): Request[Either[Either[String, String], Unit], Any] =
38+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
4039
basicRequest
4140
.method(Method.POST, uri"$baseUrl/user")
4241
.contentType("application/json")
4342
.header("api_key", apiKey)
4443
.body(user)
45-
.response(asEither(asString, ignore))
44+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
4645

4746
/**
4847
*
@@ -56,13 +55,13 @@ class UserApi(baseUrl: String) {
5655
* @param user List of user object
5756
*/
5857
def createUsersWithArrayInput(apiKey: String)(user: Seq[User]
59-
): Request[Either[Either[String, String], Unit], Any] =
58+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
6059
basicRequest
6160
.method(Method.POST, uri"$baseUrl/user/createWithArray")
6261
.contentType("application/json")
6362
.header("api_key", apiKey)
6463
.body(user)
65-
.response(asEither(asString, ignore))
64+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6665

6766
/**
6867
*
@@ -76,13 +75,13 @@ class UserApi(baseUrl: String) {
7675
* @param user List of user object
7776
*/
7877
def createUsersWithListInput(apiKey: String)(user: Seq[User]
79-
): Request[Either[Either[String, String], Unit], Any] =
78+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
8079
basicRequest
8180
.method(Method.POST, uri"$baseUrl/user/createWithList")
8281
.contentType("application/json")
8382
.header("api_key", apiKey)
8483
.body(user)
85-
.response(asEither(asString, ignore))
84+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
8685

8786
/**
8887
* This can only be done by the logged in user.
@@ -102,7 +101,7 @@ class UserApi(baseUrl: String) {
102101
.method(Method.DELETE, uri"$baseUrl/user/${username}")
103102
.contentType("application/json")
104103
.header("api_key", apiKey)
105-
.response(asJson[Unit])
104+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
106105

107106
/**
108107
*
@@ -152,12 +151,12 @@ class UserApi(baseUrl: String) {
152151
* api_key (apiKey)
153152
*/
154153
def logoutUser(apiKey: String)(
155-
): Request[Either[Either[String, String], Unit], Any] =
154+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
156155
basicRequest
157156
.method(Method.GET, uri"$baseUrl/user/logout")
158157
.contentType("application/json")
159158
.header("api_key", apiKey)
160-
.response(asEither(asString, ignore))
159+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
161160

162161
/**
163162
* This can only be done by the logged in user.
@@ -179,6 +178,6 @@ class UserApi(baseUrl: String) {
179178
.contentType("application/json")
180179
.header("api_key", apiKey)
181180
.body(user)
182-
.response(asJson[Unit])
181+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
183182

184183
}

0 commit comments

Comments
 (0)