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>
This commit is contained in:
Christian Bartolo Burlò 2022-08-15 12:42:06 +02:00 committed by GitHub
parent 079f81f0dc
commit 2a196ce362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 31 deletions

View File

@ -53,6 +53,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|LocalDate|org.joda.time.*| |LocalDate|org.joda.time.*|
|LocalDateTime|org.joda.time.*| |LocalDateTime|org.joda.time.*|
|LocalTime|org.joda.time.*| |LocalTime|org.joda.time.*|
|Seq|scala.collection.immutable.Seq|
|Set|scala.collection.immutable.Set|
|Timestamp|java.sql.Timestamp| |Timestamp|java.sql.Timestamp|
|URI|java.net.URI| |URI|java.net.URI|
|UUID|java.util.UUID| |UUID|java.util.UUID|

View File

@ -27,7 +27,7 @@ public class CodegenOperation {
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams, public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
isArray, isMultipart, isArray, isMultipart,
isResponseBinary = false, isResponseFile = false, isResponseOptional = false, hasReference = false, isResponseBinary = false, isResponseFile = false, isResponseOptional = false, hasReference = false, defaultReturnType = false,
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy, isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false, isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false,
hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined

View File

@ -4187,6 +4187,9 @@ public class DefaultCodegen implements CodegenConfig {
if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.is2xx) && Boolean.FALSE.equals(op.isResponseFile)) { if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.is2xx) && Boolean.FALSE.equals(op.isResponseFile)) {
op.isResponseFile = Boolean.TRUE; op.isResponseFile = Boolean.TRUE;
} }
if (Boolean.TRUE.equals(r.isDefault)) {
op.defaultReturnType = Boolean.TRUE;
}
// check if any 4xx or 5xx response has an error response object defined // check if any 4xx or 5xx response has an error response object defined
if ((Boolean.TRUE.equals(r.is4xx) || Boolean.TRUE.equals(r.is5xx)) && if ((Boolean.TRUE.equals(r.is4xx) || Boolean.TRUE.equals(r.is5xx)) &&

View File

@ -127,10 +127,10 @@ public class ScalaSttpClientCodegen extends AbstractScalaCodegen implements Code
additionalProperties.put("fnCamelize", new CamelizeLambda(false)); additionalProperties.put("fnCamelize", new CamelizeLambda(false));
additionalProperties.put("fnEnumEntry", new EnumEntryLambda()); additionalProperties.put("fnEnumEntry", new EnumEntryLambda());
importMapping.remove("Seq"); // importMapping.remove("Seq");
importMapping.remove("List"); // importMapping.remove("List");
importMapping.remove("Set"); // importMapping.remove("Set");
importMapping.remove("Map"); // importMapping.remove("Map");
// TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala // TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala
typeMapping = new HashMap<>(); typeMapping = new HashMap<>();

View File

@ -20,7 +20,7 @@ class {{classname}}(baseUrl: String) {
{{#javadocRenderer}} {{#javadocRenderer}}
{{>javadoc}} {{>javadoc}}
{{/javadocRenderer}} {{/javadocRenderer}}
def {{operationId}}({{>methodParameters}}): Request[{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}, Nothing] = def {{operationId}}({{>methodParameters}}): Request[{{#defaultReturnType}}Either[Either[String, String], Unit]{{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}Either[ResponseException[String, Exception], {{>operationReturnType}}]{{/separateErrorChannel}}{{^separateErrorChannel}}{{>operationReturnType}}{{/separateErrorChannel}}{{/defaultReturnType}}, Any] =
basicRequest basicRequest
.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}}") .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}}")
.contentType({{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}}){{#headerParams}} .contentType({{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}}){{#headerParams}}
@ -36,7 +36,7 @@ class {{classname}}(baseUrl: String) {
{{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}} {{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}}
).flatten){{/isMultipart}}{{/formParams.0}}{{#bodyParam}} ).flatten){{/isMultipart}}{{/formParams.0}}{{#bodyParam}}
.body({{paramName}}){{/bodyParam}} .body({{paramName}}){{/bodyParam}}
.response({{#separateErrorChannel}}asJson{{/separateErrorChannel}}{{^separateErrorChannel}}asJsonAlwaysUnsafe{{/separateErrorChannel}}[{{>operationReturnType}}]) .response({{#defaultReturnType}}asEither(asString, ignore){{/defaultReturnType}}{{^defaultReturnType}}{{#separateErrorChannel}}asJson{{/separateErrorChannel}}{{^separateErrorChannel}}asJsonAlwaysUnsafe{{/separateErrorChannel}}[{{>operationReturnType}}]{{/defaultReturnType}})
{{/operation}} {{/operation}}
} }

View File

@ -35,7 +35,7 @@ class PetApi(baseUrl: String) {
* @param pet Pet object that needs to be added to the store * @param pet Pet object that needs to be added to the store
*/ */
def addPet(pet: Pet def addPet(pet: Pet
): Request[Either[ResponseException[String, Exception], Pet], Nothing] = ): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/pet") .method(Method.POST, uri"$baseUrl/pet")
.contentType("application/json") .contentType("application/json")
@ -52,7 +52,7 @@ class PetApi(baseUrl: String) {
* @param apiKey * @param apiKey
*/ */
def deletePet(petId: Long, apiKey: Option[String] = None def deletePet(petId: Long, apiKey: Option[String] = None
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest basicRequest
.method(Method.DELETE, uri"$baseUrl/pet/${petId}") .method(Method.DELETE, uri"$baseUrl/pet/${petId}")
.contentType("application/json") .contentType("application/json")
@ -69,7 +69,7 @@ class PetApi(baseUrl: String) {
* @param status Status values that need to be considered for filter * @param status Status values that need to be considered for filter
*/ */
def findPetsByStatus(status: Seq[String] def findPetsByStatus(status: Seq[String]
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] = ): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/pet/findByStatus?status=${ status }") .method(Method.GET, uri"$baseUrl/pet/findByStatus?status=${ status }")
.contentType("application/json") .contentType("application/json")
@ -85,7 +85,7 @@ class PetApi(baseUrl: String) {
* @param tags Tags to filter by * @param tags Tags to filter by
*/ */
def findPetsByTags(tags: Seq[String] def findPetsByTags(tags: Seq[String]
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] = ): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/pet/findByTags?tags=${ tags }") .method(Method.GET, uri"$baseUrl/pet/findByTags?tags=${ tags }")
.contentType("application/json") .contentType("application/json")
@ -105,7 +105,7 @@ class PetApi(baseUrl: String) {
* @param petId ID of pet to return * @param petId ID of pet to return
*/ */
def getPetById(apiKey: String)(petId: Long def getPetById(apiKey: String)(petId: Long
): Request[Either[ResponseException[String, Exception], Pet], Nothing] = ): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/pet/${petId}") .method(Method.GET, uri"$baseUrl/pet/${petId}")
.contentType("application/json") .contentType("application/json")
@ -124,7 +124,7 @@ class PetApi(baseUrl: String) {
* @param pet Pet object that needs to be added to the store * @param pet Pet object that needs to be added to the store
*/ */
def updatePet(pet: Pet def updatePet(pet: Pet
): Request[Either[ResponseException[String, Exception], Pet], Nothing] = ): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest basicRequest
.method(Method.PUT, uri"$baseUrl/pet") .method(Method.PUT, uri"$baseUrl/pet")
.contentType("application/json") .contentType("application/json")
@ -142,7 +142,7 @@ class PetApi(baseUrl: String) {
* @param status Updated status of the pet * @param status Updated status of the pet
*/ */
def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/pet/${petId}") .method(Method.POST, uri"$baseUrl/pet/${petId}")
.contentType("application/x-www-form-urlencoded") .contentType("application/x-www-form-urlencoded")
@ -163,7 +163,7 @@ class PetApi(baseUrl: String) {
* @param file file to upload * @param file file to upload
*/ */
def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None
): Request[Either[ResponseException[String, Exception], ApiResponse], Nothing] = ): Request[Either[ResponseException[String, Exception], ApiResponse], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/pet/${petId}/uploadImage") .method(Method.POST, uri"$baseUrl/pet/${petId}/uploadImage")
.contentType("multipart/form-data") .contentType("multipart/form-data")

View File

@ -33,7 +33,7 @@ class StoreApi(baseUrl: String) {
* @param orderId ID of the order that needs to be deleted * @param orderId ID of the order that needs to be deleted
*/ */
def deleteOrder(orderId: String def deleteOrder(orderId: String
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest basicRequest
.method(Method.DELETE, uri"$baseUrl/store/order/${orderId}") .method(Method.DELETE, uri"$baseUrl/store/order/${orderId}")
.contentType("application/json") .contentType("application/json")
@ -49,7 +49,7 @@ class StoreApi(baseUrl: String) {
* api_key (apiKey) * api_key (apiKey)
*/ */
def getInventory(apiKey: String)( def getInventory(apiKey: String)(
): Request[Either[ResponseException[String, Exception], Map[String, Int]], Nothing] = ): Request[Either[ResponseException[String, Exception], Map[String, Int]], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/store/inventory") .method(Method.GET, uri"$baseUrl/store/inventory")
.contentType("application/json") .contentType("application/json")
@ -67,7 +67,7 @@ class StoreApi(baseUrl: String) {
* @param orderId ID of pet that needs to be fetched * @param orderId ID of pet that needs to be fetched
*/ */
def getOrderById(orderId: Long def getOrderById(orderId: Long
): Request[Either[ResponseException[String, Exception], Order], Nothing] = ): Request[Either[ResponseException[String, Exception], Order], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/store/order/${orderId}") .method(Method.GET, uri"$baseUrl/store/order/${orderId}")
.contentType("application/json") .contentType("application/json")
@ -83,7 +83,7 @@ class StoreApi(baseUrl: String) {
* @param order order placed for purchasing the pet * @param order order placed for purchasing the pet
*/ */
def placeOrder(order: Order def placeOrder(order: Order
): Request[Either[ResponseException[String, Exception], Order], Nothing] = ): Request[Either[ResponseException[String, Exception], Order], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/store/order") .method(Method.POST, uri"$baseUrl/store/order")
.contentType("application/json") .contentType("application/json")

View File

@ -36,13 +36,13 @@ class UserApi(baseUrl: String) {
* @param user Created user object * @param user Created user object
*/ */
def createUser(apiKey: String)(user: User def createUser(apiKey: String)(user: User
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[Either[String, String], Unit], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/user") .method(Method.POST, uri"$baseUrl/user")
.contentType("application/json") .contentType("application/json")
.header("api_key", apiKey) .header("api_key", apiKey)
.body(user) .body(user)
.response(asJson[Unit]) .response(asEither(asString, ignore))
/** /**
* *
@ -56,13 +56,13 @@ class UserApi(baseUrl: String) {
* @param user List of user object * @param user List of user object
*/ */
def createUsersWithArrayInput(apiKey: String)(user: Seq[User] def createUsersWithArrayInput(apiKey: String)(user: Seq[User]
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[Either[String, String], Unit], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/user/createWithArray") .method(Method.POST, uri"$baseUrl/user/createWithArray")
.contentType("application/json") .contentType("application/json")
.header("api_key", apiKey) .header("api_key", apiKey)
.body(user) .body(user)
.response(asJson[Unit]) .response(asEither(asString, ignore))
/** /**
* *
@ -76,13 +76,13 @@ class UserApi(baseUrl: String) {
* @param user List of user object * @param user List of user object
*/ */
def createUsersWithListInput(apiKey: String)(user: Seq[User] def createUsersWithListInput(apiKey: String)(user: Seq[User]
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[Either[String, String], Unit], Any] =
basicRequest basicRequest
.method(Method.POST, uri"$baseUrl/user/createWithList") .method(Method.POST, uri"$baseUrl/user/createWithList")
.contentType("application/json") .contentType("application/json")
.header("api_key", apiKey) .header("api_key", apiKey)
.body(user) .body(user)
.response(asJson[Unit]) .response(asEither(asString, ignore))
/** /**
* This can only be done by the logged in user. * This can only be done by the logged in user.
@ -97,7 +97,7 @@ class UserApi(baseUrl: String) {
* @param username The name that needs to be deleted * @param username The name that needs to be deleted
*/ */
def deleteUser(apiKey: String)(username: String def deleteUser(apiKey: String)(username: String
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest basicRequest
.method(Method.DELETE, uri"$baseUrl/user/${username}") .method(Method.DELETE, uri"$baseUrl/user/${username}")
.contentType("application/json") .contentType("application/json")
@ -115,7 +115,7 @@ class UserApi(baseUrl: String) {
* @param username The name that needs to be fetched. Use user1 for testing. * @param username The name that needs to be fetched. Use user1 for testing.
*/ */
def getUserByName(username: String def getUserByName(username: String
): Request[Either[ResponseException[String, Exception], User], Nothing] = ): Request[Either[ResponseException[String, Exception], User], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/user/${username}") .method(Method.GET, uri"$baseUrl/user/${username}")
.contentType("application/json") .contentType("application/json")
@ -136,7 +136,7 @@ class UserApi(baseUrl: String) {
* @param password The password for login in clear text * @param password The password for login in clear text
*/ */
def loginUser(username: String, password: String def loginUser(username: String, password: String
): Request[Either[ResponseException[String, Exception], String], Nothing] = ): Request[Either[ResponseException[String, Exception], String], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/user/login?username=${ username }&password=${ password }") .method(Method.GET, uri"$baseUrl/user/login?username=${ username }&password=${ password }")
.contentType("application/json") .contentType("application/json")
@ -152,12 +152,12 @@ class UserApi(baseUrl: String) {
* api_key (apiKey) * api_key (apiKey)
*/ */
def logoutUser(apiKey: String)( def logoutUser(apiKey: String)(
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[Either[String, String], Unit], Any] =
basicRequest basicRequest
.method(Method.GET, uri"$baseUrl/user/logout") .method(Method.GET, uri"$baseUrl/user/logout")
.contentType("application/json") .contentType("application/json")
.header("api_key", apiKey) .header("api_key", apiKey)
.response(asJson[Unit]) .response(asEither(asString, ignore))
/** /**
* This can only be done by the logged in user. * This can only be done by the logged in user.
@ -173,7 +173,7 @@ class UserApi(baseUrl: String) {
* @param user Updated user object * @param user Updated user object
*/ */
def updateUser(apiKey: String)(username: String, user: User def updateUser(apiKey: String)(username: String, user: User
): Request[Either[ResponseException[String, Exception], Unit], Nothing] = ): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest basicRequest
.method(Method.PUT, uri"$baseUrl/user/${username}") .method(Method.PUT, uri"$baseUrl/user/${username}")
.contentType("application/json") .contentType("application/json")