Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed scala-sttp generator #11949

Merged
merged 12 commits into from
Aug 15, 2022
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, hasReference = false,
isResponseBinary = false, isResponseFile = 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 @@ -4023,6 +4023,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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @OpenAPITools/generator-core-team

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 @@ -121,10 +121,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}})
chrisbartoloburlo marked this conversation as resolved.
Show resolved Hide resolved

{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ 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")
.body(pet)
.response(asJson[Pet])
.response(asJson[Pet])

/**
*
Expand All @@ -52,12 +52,12 @@ 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")
.header("api_key", apiKey.toString)
.response(asJson[Unit])
.response(asJson[Unit])

/**
* Multiple status values can be provided with comma separated strings
Expand All @@ -69,11 +69,11 @@ 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")
.response(asJson[Seq[Pet]])
.response(asJson[Seq[Pet]])

/**
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
Expand All @@ -85,11 +85,11 @@ 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")
.response(asJson[Seq[Pet]])
.response(asJson[Seq[Pet]])

/**
* Returns a single pet
Expand All @@ -105,12 +105,12 @@ 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")
.header("api_key", apiKey)
.response(asJson[Pet])
.response(asJson[Pet])

/**
*
Expand All @@ -124,12 +124,12 @@ 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")
.body(pet)
.response(asJson[Pet])
.response(asJson[Pet])

/**
*
Expand All @@ -142,15 +142,15 @@ 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")
.body(Map(
"name" -> name,
"status" -> status
))
.response(asJson[Unit])
.response(asJson[Unit])

/**
*
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 All @@ -173,6 +173,6 @@ class PetApi(baseUrl: String) {
file.map(multipartFile("file", _))

).flatten)
.response(asJson[ApiResponse])
.response(asJson[ApiResponse])

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ 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")
.response(asJson[Unit])
.response(asJson[Unit])

/**
* Returns a map of status codes to quantities
Expand All @@ -49,12 +49,12 @@ 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")
.header("api_key", apiKey)
.response(asJson[Map[String, Int]])
.response(asJson[Map[String, Int]])

/**
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
Expand All @@ -67,11 +67,11 @@ 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")
.response(asJson[Order])
.response(asJson[Order])

/**
*
Expand All @@ -83,11 +83,11 @@ 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")
.body(order)
.response(asJson[Order])
.response(asJson[Order])

}
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,12 +97,12 @@ 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")
.header("api_key", apiKey)
.response(asJson[Unit])
.response(asJson[Unit])

/**
*
Expand All @@ -115,11 +115,11 @@ 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")
.response(asJson[User])
.response(asJson[User])

/**
*
Expand All @@ -136,11 +136,11 @@ 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")
.response(asJson[String])
.response(asJson[String])

/**
*
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,12 +173,12 @@ 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")
.header("api_key", apiKey)
.body(user)
.response(asJson[Unit])
.response(asJson[Unit])

}