Skip to content

Commit 5e40fe7

Browse files
scala-sttp4: fix for issue 15785 api returns unit. (#18536)
1 parent 2a1b4f9 commit 5e40fe7

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class {{classname}}(baseUrl: String) {
1919
{{#javadocRenderer}}
2020
{{>javadoc}}
2121
{{/javadocRenderer}}
22-
def {{operationId}}({{>methodParameters}}): Request[{{#defaultReturnType}}Either[Either[String, String], Unit]{{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}{{/defaultReturnType}}] =
22+
def {{operationId}}({{>methodParameters}}): Request[{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}] =
2323
basicRequest
2424
.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}}")
2525
.contentType({{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}}){{#headerParams}}
@@ -35,7 +35,7 @@ class {{classname}}(baseUrl: String) {
3535
{{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}}
3636
).flatten){{/isMultipart}}{{/formParams.0}}{{#bodyParam}}
3737
.body({{paramName}}){{/bodyParam}}
38-
.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}})
3939

4040
{{/operation}}
4141
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PetApi(baseUrl: String) {
5454
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
5555
.contentType("application/json")
5656
.header("api_key", apiKey.toString)
57-
.response(asJson[Unit])
57+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
5858

5959
/**
6060
* Multiple status values can be provided with comma separated strings
@@ -142,7 +142,7 @@ class PetApi(baseUrl: String) {
142142
"name" -> name,
143143
"status" -> status
144144
))
145-
.response(asJson[Unit])
145+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
146146

147147
/**
148148
*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class StoreApi(baseUrl: String) {
3535
basicRequest
3636
.method(Method.DELETE, uri"$baseUrl/store/order/${orderId}")
3737
.contentType("application/json")
38-
.response(asJson[Unit])
38+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
3939

4040
/**
4141
* Returns a map of status codes to quantities

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class UserApi(baseUrl: String) {
3434
*
3535
* @param user Created user object
3636
*/
37-
def createUser(apiKey: String)(user: User): Request[Either[Either[String, String], Unit]] =
37+
def createUser(apiKey: String)(user: User): Request[Either[ResponseException[String, Exception], Unit]] =
3838
basicRequest
3939
.method(Method.POST, uri"$baseUrl/user")
4040
.contentType("application/json")
4141
.header("api_key", apiKey)
4242
.body(user)
43-
.response(asEither(asString, ignore))
43+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
4444

4545
/**
4646
*
@@ -53,13 +53,13 @@ class UserApi(baseUrl: String) {
5353
*
5454
* @param user List of user object
5555
*/
56-
def createUsersWithArrayInput(apiKey: String)(user: Seq[User]): Request[Either[Either[String, String], Unit]] =
56+
def createUsersWithArrayInput(apiKey: String)(user: Seq[User]): Request[Either[ResponseException[String, Exception], Unit]] =
5757
basicRequest
5858
.method(Method.POST, uri"$baseUrl/user/createWithArray")
5959
.contentType("application/json")
6060
.header("api_key", apiKey)
6161
.body(user)
62-
.response(asEither(asString, ignore))
62+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6363

6464
/**
6565
*
@@ -72,13 +72,13 @@ class UserApi(baseUrl: String) {
7272
*
7373
* @param user List of user object
7474
*/
75-
def createUsersWithListInput(apiKey: String)(user: Seq[User]): Request[Either[Either[String, String], Unit]] =
75+
def createUsersWithListInput(apiKey: String)(user: Seq[User]): Request[Either[ResponseException[String, Exception], Unit]] =
7676
basicRequest
7777
.method(Method.POST, uri"$baseUrl/user/createWithList")
7878
.contentType("application/json")
7979
.header("api_key", apiKey)
8080
.body(user)
81-
.response(asEither(asString, ignore))
81+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
8282

8383
/**
8484
* This can only be done by the logged in user.
@@ -97,7 +97,7 @@ class UserApi(baseUrl: String) {
9797
.method(Method.DELETE, uri"$baseUrl/user/${username}")
9898
.contentType("application/json")
9999
.header("api_key", apiKey)
100-
.response(asJson[Unit])
100+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
101101

102102
/**
103103
*
@@ -144,12 +144,12 @@ class UserApi(baseUrl: String) {
144144
* Available security schemes:
145145
* api_key (apiKey)
146146
*/
147-
def logoutUser(apiKey: String)(): Request[Either[Either[String, String], Unit]] =
147+
def logoutUser(apiKey: String)(): Request[Either[ResponseException[String, Exception], Unit]] =
148148
basicRequest
149149
.method(Method.GET, uri"$baseUrl/user/logout")
150150
.contentType("application/json")
151151
.header("api_key", apiKey)
152-
.response(asEither(asString, ignore))
152+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
153153

154154
/**
155155
* This can only be done by the logged in user.
@@ -170,6 +170,6 @@ class UserApi(baseUrl: String) {
170170
.contentType("application/json")
171171
.header("api_key", apiKey)
172172
.body(user)
173-
.response(asJson[Unit])
173+
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
174174

175175
}

0 commit comments

Comments
 (0)