Skip to content

Commit

Permalink
Fixed scala-sttp generator (#11949)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* updated samples

Co-authored-by: João Ferreira <[email protected]>
  • Loading branch information
chrisbartoloburlo and jtjeferreira committed Aug 15, 2022
1 parent 079f81f commit 2a196ce
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 31 deletions.
2 changes: 2 additions & 0 deletions docs/generators/scala-sttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|LocalDate|org.joda.time.*|
|LocalDateTime|org.joda.time.*|
|LocalTime|org.joda.time.*|
|Seq|scala.collection.immutable.Seq|
|Set|scala.collection.immutable.Set|
|Timestamp|java.sql.Timestamp|
|URI|java.net.URI|
|UUID|java.util.UUID|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CodegenOperation {
public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams,
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap,
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,
isRestful, isDeprecated, isCallbackRequest, uniqueItems, hasDefaultResponse = false,
hasErrorResponseObject; // if 4xx, 5xx responses have at least one error object defined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4187,6 +4187,9 @@ public CodegenOperation fromOperation(String path,
if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.is2xx) && Boolean.FALSE.equals(op.isResponseFile)) {
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
if ((Boolean.TRUE.equals(r.is4xx) || Boolean.TRUE.equals(r.is5xx)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public ScalaSttpClientCodegen() {
additionalProperties.put("fnCamelize", new CamelizeLambda(false));
additionalProperties.put("fnEnumEntry", new EnumEntryLambda());

importMapping.remove("Seq");
importMapping.remove("List");
importMapping.remove("Set");
importMapping.remove("Map");
// importMapping.remove("Seq");
// importMapping.remove("List");
// importMapping.remove("Set");
// importMapping.remove("Map");

// TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala
typeMapping = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class {{classname}}(baseUrl: String) {
{{#javadocRenderer}}
{{>javadoc}}
{{/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
.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}}
Expand All @@ -36,7 +36,7 @@ class {{classname}}(baseUrl: String) {
{{>paramMultipartCreation}}{{^-last}}, {{/-last}}{{/formParams}}
).flatten){{/isMultipart}}{{/formParams.0}}{{#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}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PetApi(baseUrl: String) {
* @param pet Pet object that needs to be added to the store
*/
def addPet(pet: Pet
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/pet")
.contentType("application/json")
Expand All @@ -52,7 +52,7 @@ class PetApi(baseUrl: String) {
* @param apiKey
*/
def deletePet(petId: Long, apiKey: Option[String] = None
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
.contentType("application/json")
Expand All @@ -69,7 +69,7 @@ class PetApi(baseUrl: String) {
* @param status Status values that need to be considered for filter
*/
def findPetsByStatus(status: Seq[String]
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] =
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/pet/findByStatus?status=${ status }")
.contentType("application/json")
Expand All @@ -85,7 +85,7 @@ class PetApi(baseUrl: String) {
* @param tags Tags to filter by
*/
def findPetsByTags(tags: Seq[String]
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Nothing] =
): Request[Either[ResponseException[String, Exception], Seq[Pet]], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/pet/findByTags?tags=${ tags }")
.contentType("application/json")
Expand All @@ -105,7 +105,7 @@ class PetApi(baseUrl: String) {
* @param petId ID of pet to return
*/
def getPetById(apiKey: String)(petId: Long
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/pet/${petId}")
.contentType("application/json")
Expand All @@ -124,7 +124,7 @@ class PetApi(baseUrl: String) {
* @param pet Pet object that needs to be added to the store
*/
def updatePet(pet: Pet
): Request[Either[ResponseException[String, Exception], Pet], Nothing] =
): Request[Either[ResponseException[String, Exception], Pet], Any] =
basicRequest
.method(Method.PUT, uri"$baseUrl/pet")
.contentType("application/json")
Expand All @@ -142,7 +142,7 @@ class PetApi(baseUrl: String) {
* @param status Updated status of the pet
*/
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
.method(Method.POST, uri"$baseUrl/pet/${petId}")
.contentType("application/x-www-form-urlencoded")
Expand All @@ -163,7 +163,7 @@ class PetApi(baseUrl: String) {
* @param file file to upload
*/
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
.method(Method.POST, uri"$baseUrl/pet/${petId}/uploadImage")
.contentType("multipart/form-data")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StoreApi(baseUrl: String) {
* @param orderId ID of the order that needs to be deleted
*/
def deleteOrder(orderId: String
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest
.method(Method.DELETE, uri"$baseUrl/store/order/${orderId}")
.contentType("application/json")
Expand All @@ -49,7 +49,7 @@ class StoreApi(baseUrl: String) {
* api_key (apiKey)
*/
def getInventory(apiKey: String)(
): Request[Either[ResponseException[String, Exception], Map[String, Int]], Nothing] =
): Request[Either[ResponseException[String, Exception], Map[String, Int]], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/store/inventory")
.contentType("application/json")
Expand All @@ -67,7 +67,7 @@ class StoreApi(baseUrl: String) {
* @param orderId ID of pet that needs to be fetched
*/
def getOrderById(orderId: Long
): Request[Either[ResponseException[String, Exception], Order], Nothing] =
): Request[Either[ResponseException[String, Exception], Order], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/store/order/${orderId}")
.contentType("application/json")
Expand All @@ -83,7 +83,7 @@ class StoreApi(baseUrl: String) {
* @param order order placed for purchasing the pet
*/
def placeOrder(order: Order
): Request[Either[ResponseException[String, Exception], Order], Nothing] =
): Request[Either[ResponseException[String, Exception], Order], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/store/order")
.contentType("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class UserApi(baseUrl: String) {
* @param user Created user object
*/
def createUser(apiKey: String)(user: User
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[Either[String, String], Unit], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/user")
.contentType("application/json")
.header("api_key", apiKey)
.body(user)
.response(asJson[Unit])
.response(asEither(asString, ignore))

/**
*
Expand All @@ -56,13 +56,13 @@ class UserApi(baseUrl: String) {
* @param user List of user object
*/
def createUsersWithArrayInput(apiKey: String)(user: Seq[User]
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[Either[String, String], Unit], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/user/createWithArray")
.contentType("application/json")
.header("api_key", apiKey)
.body(user)
.response(asJson[Unit])
.response(asEither(asString, ignore))

/**
*
Expand All @@ -76,13 +76,13 @@ class UserApi(baseUrl: String) {
* @param user List of user object
*/
def createUsersWithListInput(apiKey: String)(user: Seq[User]
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[Either[String, String], Unit], Any] =
basicRequest
.method(Method.POST, uri"$baseUrl/user/createWithList")
.contentType("application/json")
.header("api_key", apiKey)
.body(user)
.response(asJson[Unit])
.response(asEither(asString, ignore))

/**
* This can only be done by the logged in user.
Expand All @@ -97,7 +97,7 @@ class UserApi(baseUrl: String) {
* @param username The name that needs to be deleted
*/
def deleteUser(apiKey: String)(username: String
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[ResponseException[String, Exception], Unit], Any] =
basicRequest
.method(Method.DELETE, uri"$baseUrl/user/${username}")
.contentType("application/json")
Expand All @@ -115,7 +115,7 @@ class UserApi(baseUrl: String) {
* @param username The name that needs to be fetched. Use user1 for testing.
*/
def getUserByName(username: String
): Request[Either[ResponseException[String, Exception], User], Nothing] =
): Request[Either[ResponseException[String, Exception], User], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/user/${username}")
.contentType("application/json")
Expand All @@ -136,7 +136,7 @@ class UserApi(baseUrl: String) {
* @param password The password for login in clear text
*/
def loginUser(username: String, password: String
): Request[Either[ResponseException[String, Exception], String], Nothing] =
): Request[Either[ResponseException[String, Exception], String], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/user/login?username=${ username }&password=${ password }")
.contentType("application/json")
Expand All @@ -152,12 +152,12 @@ class UserApi(baseUrl: String) {
* api_key (apiKey)
*/
def logoutUser(apiKey: String)(
): Request[Either[ResponseException[String, Exception], Unit], Nothing] =
): Request[Either[Either[String, String], Unit], Any] =
basicRequest
.method(Method.GET, uri"$baseUrl/user/logout")
.contentType("application/json")
.header("api_key", apiKey)
.response(asJson[Unit])
.response(asEither(asString, ignore))

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

0 comments on commit 2a196ce

Please sign in to comment.