Skip to content

Commit 2a196ce

Browse files
Fixed scala-sttp generator (#11949)
* fixed scala-sttp generator * pr checklist * fixed issue with imports in scala-sttp * fixed samples * changed isUnit to isDefault * fixed errors * Update modules/openapi-generator/src/main/resources/scala-sttp/api.mustache Co-authored-by: João Ferreira <jtjeferreira@gmail.com> * updated samples Co-authored-by: João Ferreira <jtjeferreira@gmail.com>
1 parent 079f81f commit 2a196ce

File tree

8 files changed

+36
-31
lines changed

8 files changed

+36
-31
lines changed

docs/generators/scala-sttp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5353
|LocalDate|org.joda.time.*|
5454
|LocalDateTime|org.joda.time.*|
5555
|LocalTime|org.joda.time.*|
56+
|Seq|scala.collection.immutable.Seq|
57+
|Set|scala.collection.immutable.Set|
5658
|Timestamp|java.sql.Timestamp|
5759
|URI|java.net.URI|
5860
|UUID|java.util.UUID|

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CodegenOperation {
2727
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
2828
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
2929
isArray, isMultipart,
30-
isResponseBinary = false, isResponseFile = false, isResponseOptional = false, hasReference = false,
30+
isResponseBinary = false, isResponseFile = false, isResponseOptional = false, hasReference = false, defaultReturnType = false,
3131
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
3232
isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false,
3333
hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,9 @@ public CodegenOperation fromOperation(String path,
41874187
if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.is2xx) && Boolean.FALSE.equals(op.isResponseFile)) {
41884188
op.isResponseFile = Boolean.TRUE;
41894189
}
4190+
if (Boolean.TRUE.equals(r.isDefault)) {
4191+
op.defaultReturnType = Boolean.TRUE;
4192+
}
41904193

41914194
// check if any 4xx or 5xx response has an error response object defined
41924195
if ((Boolean.TRUE.equals(r.is4xx) || Boolean.TRUE.equals(r.is5xx)) &&

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ public ScalaSttpClientCodegen() {
127127
additionalProperties.put("fnCamelize", new CamelizeLambda(false));
128128
additionalProperties.put("fnEnumEntry", new EnumEntryLambda());
129129

130-
importMapping.remove("Seq");
131-
importMapping.remove("List");
132-
importMapping.remove("Set");
133-
importMapping.remove("Map");
130+
// importMapping.remove("Seq");
131+
// importMapping.remove("List");
132+
// importMapping.remove("Set");
133+
// importMapping.remove("Map");
134134

135135
// TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala
136136
typeMapping = new HashMap<>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class {{classname}}(baseUrl: String) {
2020
{{#javadocRenderer}}
2121
{{>javadoc}}
2222
{{/javadocRenderer}}
23-
def {{operationId}}({{>methodParameters}}): Request[{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}, Nothing] =
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] =
2424
basicRequest
2525
.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}}")
2626
.contentType({{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}}){{#headerParams}}
@@ -36,7 +36,7 @@ class {{classname}}(baseUrl: String) {
3636
{{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}}
3737
).flatten){{/isMultipart}}{{/formParams.0}}{{#bodyParam}}
3838
.body({{paramName}}){{/bodyParam}}
39-
.response({{#separateErrorChannel}}asJson{{/separateErrorChannel}}{{^separateErrorChannel}}asJsonAlwaysUnsafe{{/separateErrorChannel}}[{{>operationReturnType}}])
39+
.response({{#defaultReturnType}}asEither(asString, ignore){{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}asJson{{/separateErrorChannel}}{{^separateErrorChannel}}asJsonAlwaysUnsafe{{/separateErrorChannel}}[{{>operationReturnType}}]{{/defaultReturnType}})
4040

4141
{{/operation}}
4242
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PetApi(baseUrl: String) {
3535
* @param pet Pet object that needs to be added to the store
3636
*/
3737
def addPet(pet: Pet
38-
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
38+
): Request[Either[ResponseException[String, Exception], Pet], Any] =
3939
basicRequest
4040
.method(Method.POST, uri"$baseUrl/pet")
4141
.contentType("application/json")
@@ -52,7 +52,7 @@ class PetApi(baseUrl: String) {
5252
* @param apiKey
5353
*/
5454
def deletePet(petId: Long, apiKey: Option[String] = None
55-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
55+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
5656
basicRequest
5757
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
5858
.contentType("application/json")
@@ -69,7 +69,7 @@ class PetApi(baseUrl: String) {
6969
* @param status Status values that need to be considered for filter
7070
*/
7171
def findPetsByStatus(status: Seq[String]
72-
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] =
72+
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
7373
basicRequest
7474
.method(Method.GET, uri"$baseUrl/pet/findByStatus?status=${ status }")
7575
.contentType("application/json")
@@ -85,7 +85,7 @@ class PetApi(baseUrl: String) {
8585
* @param tags Tags to filter by
8686
*/
8787
def findPetsByTags(tags: Seq[String]
88-
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] =
88+
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
8989
basicRequest
9090
.method(Method.GET, uri"$baseUrl/pet/findByTags?tags=${ tags }")
9191
.contentType("application/json")
@@ -105,7 +105,7 @@ class PetApi(baseUrl: String) {
105105
* @param petId ID of pet to return
106106
*/
107107
def getPetById(apiKey: String)(petId: Long
108-
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
108+
): Request[Either[ResponseException[String, Exception], Pet], Any] =
109109
basicRequest
110110
.method(Method.GET, uri"$baseUrl/pet/${petId}")
111111
.contentType("application/json")
@@ -124,7 +124,7 @@ class PetApi(baseUrl: String) {
124124
* @param pet Pet object that needs to be added to the store
125125
*/
126126
def updatePet(pet: Pet
127-
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
127+
): Request[Either[ResponseException[String, Exception], Pet], Any] =
128128
basicRequest
129129
.method(Method.PUT, uri"$baseUrl/pet")
130130
.contentType("application/json")
@@ -142,7 +142,7 @@ class PetApi(baseUrl: String) {
142142
* @param status Updated status of the pet
143143
*/
144144
def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None
145-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
145+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
146146
basicRequest
147147
.method(Method.POST, uri"$baseUrl/pet/${petId}")
148148
.contentType("application/x-www-form-urlencoded")
@@ -163,7 +163,7 @@ class PetApi(baseUrl: String) {
163163
* @param file file to upload
164164
*/
165165
def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None
166-
): Request[Either[ResponseException[String, Exception], ApiResponse], Nothing] =
166+
): Request[Either[ResponseException[String, Exception], ApiResponse], Any] =
167167
basicRequest
168168
.method(Method.POST, uri"$baseUrl/pet/${petId}/uploadImage")
169169
.contentType("multipart/form-data")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class StoreApi(baseUrl: String) {
3333
* @param orderId ID of the order that needs to be deleted
3434
*/
3535
def deleteOrder(orderId: String
36-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
36+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
3737
basicRequest
3838
.method(Method.DELETE, uri"$baseUrl/store/order/${orderId}")
3939
.contentType("application/json")
@@ -49,7 +49,7 @@ class StoreApi(baseUrl: String) {
4949
* api_key (apiKey)
5050
*/
5151
def getInventory(apiKey: String)(
52-
): Request[Either[ResponseException[String, Exception], Map[String, Int]], Nothing] =
52+
): Request[Either[ResponseException[String, Exception], Map[String, Int]], Any] =
5353
basicRequest
5454
.method(Method.GET, uri"$baseUrl/store/inventory")
5555
.contentType("application/json")
@@ -67,7 +67,7 @@ class StoreApi(baseUrl: String) {
6767
* @param orderId ID of pet that needs to be fetched
6868
*/
6969
def getOrderById(orderId: Long
70-
): Request[Either[ResponseException[String, Exception], Order], Nothing] =
70+
): Request[Either[ResponseException[String, Exception], Order], Any] =
7171
basicRequest
7272
.method(Method.GET, uri"$baseUrl/store/order/${orderId}")
7373
.contentType("application/json")
@@ -83,7 +83,7 @@ class StoreApi(baseUrl: String) {
8383
* @param order order placed for purchasing the pet
8484
*/
8585
def placeOrder(order: Order
86-
): Request[Either[ResponseException[String, Exception], Order], Nothing] =
86+
): Request[Either[ResponseException[String, Exception], Order], Any] =
8787
basicRequest
8888
.method(Method.POST, uri"$baseUrl/store/order")
8989
.contentType("application/json")

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class UserApi(baseUrl: String) {
3636
* @param user Created user object
3737
*/
3838
def createUser(apiKey: String)(user: User
39-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
39+
): Request[Either[Either[String, String], Unit], Any] =
4040
basicRequest
4141
.method(Method.POST, uri"$baseUrl/user")
4242
.contentType("application/json")
4343
.header("api_key", apiKey)
4444
.body(user)
45-
.response(asJson[Unit])
45+
.response(asEither(asString, ignore))
4646

4747
/**
4848
*
@@ -56,13 +56,13 @@ class UserApi(baseUrl: String) {
5656
* @param user List of user object
5757
*/
5858
def createUsersWithArrayInput(apiKey: String)(user: Seq[User]
59-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
59+
): Request[Either[Either[String, String], Unit], Any] =
6060
basicRequest
6161
.method(Method.POST, uri"$baseUrl/user/createWithArray")
6262
.contentType("application/json")
6363
.header("api_key", apiKey)
6464
.body(user)
65-
.response(asJson[Unit])
65+
.response(asEither(asString, ignore))
6666

6767
/**
6868
*
@@ -76,13 +76,13 @@ class UserApi(baseUrl: String) {
7676
* @param user List of user object
7777
*/
7878
def createUsersWithListInput(apiKey: String)(user: Seq[User]
79-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
79+
): Request[Either[Either[String, String], Unit], Any] =
8080
basicRequest
8181
.method(Method.POST, uri"$baseUrl/user/createWithList")
8282
.contentType("application/json")
8383
.header("api_key", apiKey)
8484
.body(user)
85-
.response(asJson[Unit])
85+
.response(asEither(asString, ignore))
8686

8787
/**
8888
* This can only be done by the logged in user.
@@ -97,7 +97,7 @@ class UserApi(baseUrl: String) {
9797
* @param username The name that needs to be deleted
9898
*/
9999
def deleteUser(apiKey: String)(username: String
100-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
100+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
101101
basicRequest
102102
.method(Method.DELETE, uri"$baseUrl/user/${username}")
103103
.contentType("application/json")
@@ -115,7 +115,7 @@ class UserApi(baseUrl: String) {
115115
* @param username The name that needs to be fetched. Use user1 for testing.
116116
*/
117117
def getUserByName(username: String
118-
): Request[Either[ResponseException[String, Exception], User], Nothing] =
118+
): Request[Either[ResponseException[String, Exception], User], Any] =
119119
basicRequest
120120
.method(Method.GET, uri"$baseUrl/user/${username}")
121121
.contentType("application/json")
@@ -136,7 +136,7 @@ class UserApi(baseUrl: String) {
136136
* @param password The password for login in clear text
137137
*/
138138
def loginUser(username: String, password: String
139-
): Request[Either[ResponseException[String, Exception], String], Nothing] =
139+
): Request[Either[ResponseException[String, Exception], String], Any] =
140140
basicRequest
141141
.method(Method.GET, uri"$baseUrl/user/login?username=${ username }&password=${ password }")
142142
.contentType("application/json")
@@ -152,12 +152,12 @@ class UserApi(baseUrl: String) {
152152
* api_key (apiKey)
153153
*/
154154
def logoutUser(apiKey: String)(
155-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
155+
): Request[Either[Either[String, String], Unit], Any] =
156156
basicRequest
157157
.method(Method.GET, uri"$baseUrl/user/logout")
158158
.contentType("application/json")
159159
.header("api_key", apiKey)
160-
.response(asJson[Unit])
160+
.response(asEither(asString, ignore))
161161

162162
/**
163163
* This can only be done by the logged in user.
@@ -173,7 +173,7 @@ class UserApi(baseUrl: String) {
173173
* @param user Updated user object
174174
*/
175175
def updateUser(apiKey: String)(username: String, user: User
176-
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
176+
): Request[Either[ResponseException[String, Exception], Unit], Any] =
177177
basicRequest
178178
.method(Method.PUT, uri"$baseUrl/user/${username}")
179179
.contentType("application/json")

0 commit comments

Comments
 (0)