Skip to content

Commit 6bfd5b2

Browse files
committed
fix akka-scala-client template compilation warnings
add COOKIE location authorization key regenerate template
1 parent 524ef63 commit 6bfd5b2

File tree

14 files changed

+213
-99
lines changed

14 files changed

+213
-99
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class {{classname}}(baseUrl: String) {
2222
{{/javadocRenderer}}
2323
def {{operationId}}({{>methodParameters}}): ApiRequest[{{>operationReturnType}}] =
2424
ApiRequest[{{>operationReturnType}}](ApiMethods.{{httpMethod.toUpperCase}}, baseUrl, "{{{path}}}", {{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{^consumes}}"application/json"{{/consumes}})
25-
{{#authMethods}}{{#isApiKey}}.withApiKey(apiKey, "{{keyParamName}}", {{#isKeyInQuery}}QUERY{{/isKeyInQuery}}{{#isKeyInHeader}}HEADER{{/isKeyInHeader}})
25+
{{#authMethods}}{{#isApiKey}}.withApiKey(apiKey, "{{keyParamName}}", {{#isKeyInQuery}}QUERY{{/isKeyInQuery}}{{#isKeyInHeader}}HEADER{{/isKeyInHeader}}{{#isKeyInCookie}}COOKIE{{/isKeyInCookie}})
2626
{{/isApiKey}}{{#isBasic}}.withCredentials(basicAuth)
2727
{{/isBasic}}{{/authMethods}}{{#bodyParam}}.withBody({{paramName}})
2828
{{/bodyParam}}{{#formParams}}.withFormParam({{>paramCreation}})

modules/openapi-generator/src/main/resources/scala-akka-client/apiInvoker.mustache

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object ApiInvoker {
4545
*
4646
* @param request the apiRequest to be executed
4747
*/
48-
implicit class ApiRequestImprovements[T](request: ApiRequest[T]) {
48+
implicit class ApiRequestImprovements[T: Manifest](request: ApiRequest[T]) {
4949
5050
def response(invoker: ApiInvoker)(implicit ec: ExecutionContext, system: ActorSystem): Future[ApiResponse[T]] =
5151
response(ec, system, invoker)
@@ -67,7 +67,7 @@ object ApiInvoker {
6767
def toAkkaHttpMethod: HttpMethod = HttpMethods.getForKey(method.value).getOrElse(HttpMethods.GET)
6868
}
6969
70-
case object DateTimeSerializer extends CustomSerializer[DateTime](format => ( {
70+
case object DateTimeSerializer extends CustomSerializer[DateTime](_ => ( {
7171
case JString(s) =>
7272
ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(s)
7373
}, {
@@ -215,7 +215,7 @@ class ApiInvoker(formats: Formats)(implicit system: ActorSystem) extends CustomC
215215
Uri(r.basePath + opPathWithParams).withQuery(query)
216216
}
217217
218-
def execute[T](r: ApiRequest[T]): Future[ApiResponse[T]] = {
218+
def execute[T: Manifest](r: ApiRequest[T]): Future[ApiResponse[T]] = {
219219
implicit val timeout: Timeout = settings.connectionTimeout
220220
221221
val request = createRequest(makeUri(r), r)
@@ -239,8 +239,8 @@ class ApiInvoker(formats: Formats)(implicit system: ActorSystem) extends CustomC
239239
.flatMap(unmarshallApiResponse(r))
240240
}
241241
242-
def unmarshallApiResponse[T](request: ApiRequest[T])(response: HttpResponse): Future[ApiResponse[T]] = {
243-
def responseForState[V](state: ResponseState, value: V) = {
242+
def unmarshallApiResponse[T: Manifest](request: ApiRequest[T])(response: HttpResponse): Future[ApiResponse[T]] = {
243+
def responseForState[V](state: ResponseState, value: V): ApiResponse[V] = {
244244
state match {
245245
case ResponseState.Success =>
246246
ApiResponse(response.status.intValue, value, response.headers.map(header => (header.name, header.value)).toMap)
@@ -253,31 +253,29 @@ class ApiInvoker(formats: Formats)(implicit system: ActorSystem) extends CustomC
253253
)
254254
}
255255
}
256-
256+
val mf = implicitly(manifest[T])
257257
request
258-
.responseForCode(response.status.intValue)
259-
.map {
260-
case (Manifest.Unit, state: ResponseState) =>
261-
// FIXME Casting is ugly, how to do better?
262-
Future(responseForState(state, Unit)).asInstanceOf[Future[ApiResponse[T]]]
263-
case (manifest: Manifest[T], state: ResponseState) =>
264-
implicit val m: Unmarshaller[HttpEntity, T] = unmarshaller[T](manifest, serialization, formats)
265-
266-
Unmarshal(response.entity)
267-
.to[T]
268-
.recoverWith {
269-
case e ⇒ throw ApiError(response.status.intValue, s"Unable to unmarshall content to [$manifest]", Some(response.entity.toString), e)
270-
}
271-
.map(value => responseForState(state, value))
272-
}
273-
.getOrElse(Future.failed(ApiError(response.status.intValue, "Unexpected response code", Some(response.entity.toString))))
258+
.responseForCode(response.status.intValue) match {
259+
case Some((Manifest.Unit, state: ResponseState)) =>
260+
Future(responseForState(state, Unit).asInstanceOf[ApiResponse[T]])
261+
case Some((manifest, state: ResponseState)) if manifest == mf =>
262+
implicit val m: Unmarshaller[HttpEntity, T] = unmarshaller[T](mf, serialization, formats)
263+
Unmarshal(response.entity)
264+
.to[T]
265+
.recoverWith {
266+
case e ⇒ throw ApiError(response.status.intValue, s"Unable to unmarshall content to [$manifest]", Some(response.entity.toString), e)
267+
}
268+
.map(value => responseForState(state, value))
269+
case None | Some(_) =>
270+
Future.failed(ApiError(response.status.intValue, "Unexpected response code", Some(response.entity.toString)))
271+
}
274272
}
275273
}
276274
277275
sealed trait CustomContentTypes {
278276
279277
protected def normalizedContentType(original: String): ContentType =
280-
ContentType(MediaTypes.forExtension(original), () => HttpCharsets.`UTF-8`)
278+
ContentType(parseContentType(original).mediaType, () => HttpCharsets.`UTF-8`)
281279
282280
protected def parseContentType(contentType: String): ContentType = {
283281

modules/openapi-generator/src/main/resources/scala-akka-client/requests.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ object ApiKeyLocations {
8585
8686
case object HEADER extends ApiKeyLocation
8787
88+
case object COOKIE extends ApiKeyLocation
89+
8890
}
8991

9092

modules/openapi-generator/src/test/resources/3_0/petstore.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ paths:
2626
description: ''
2727
operationId: addPet
2828
responses:
29+
'200':
30+
description: successful operation
31+
content:
32+
application/xml:
33+
schema:
34+
$ref: '#/components/schemas/Pet'
35+
application/json:
36+
schema:
37+
$ref: '#/components/schemas/Pet'
2938
'405':
3039
description: Invalid input
3140
security:
@@ -41,6 +50,15 @@ paths:
4150
description: ''
4251
operationId: updatePet
4352
responses:
53+
'200':
54+
description: successful operation
55+
content:
56+
application/xml:
57+
schema:
58+
$ref: '#/components/schemas/Pet'
59+
application/json:
60+
schema:
61+
$ref: '#/components/schemas/Pet'
4462
'400':
4563
description: Invalid ID supplied
4664
'404':

samples/client/petstore/scala-akka/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Class | Method | HTTP request | Description
9191

9292
- [ApiResponse](ApiResponse.md)
9393
- [Category](Category.md)
94+
- [InlineObject](InlineObject.md)
95+
- [InlineObject1](InlineObject1.md)
9496
- [Order](Order.md)
9597
- [Pet](Pet.md)
9698
- [Tag](Tag.md)
@@ -106,6 +108,12 @@ Authentication schemes defined for the API:
106108
- **API key parameter name**: api_key
107109
- **Location**: HTTP header
108110

111+
### auth_cookie
112+
113+
- **Type**: API key
114+
- **API key parameter name**: AUTH_KEY
115+
- **Location**:
116+
109117

110118
## Author
111119

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.2.8
1+
sbt.version=1.3.6

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class PetApi(baseUrl: String) {
2727

2828
/**
2929
* Expected answers:
30+
* code 200 : Pet (successful operation)
3031
* code 405 : (Invalid input)
3132
*
32-
* @param body Pet object that needs to be added to the store
33+
* @param pet Pet object that needs to be added to the store
3334
*/
34-
def addPet(body: Pet): ApiRequest[Unit] =
35-
ApiRequest[Unit](ApiMethods.POST, baseUrl, "/pet", "application/json")
36-
.withBody(body)
35+
def addPet(pet: Pet): ApiRequest[Pet] =
36+
ApiRequest[Pet](ApiMethods.POST, baseUrl, "/pet", "application/json")
37+
.withBody(pet)
38+
.withSuccessResponse[Pet](200)
3739
.withErrorResponse[Unit](405)
3840

3941

@@ -107,15 +109,17 @@ class PetApi(baseUrl: String) {
107109

108110
/**
109111
* Expected answers:
112+
* code 200 : Pet (successful operation)
110113
* code 400 : (Invalid ID supplied)
111114
* code 404 : (Pet not found)
112115
* code 405 : (Validation exception)
113116
*
114-
* @param body Pet object that needs to be added to the store
117+
* @param pet Pet object that needs to be added to the store
115118
*/
116-
def updatePet(body: Pet): ApiRequest[Unit] =
117-
ApiRequest[Unit](ApiMethods.PUT, baseUrl, "/pet", "application/json")
118-
.withBody(body)
119+
def updatePet(pet: Pet): ApiRequest[Pet] =
120+
ApiRequest[Pet](ApiMethods.PUT, baseUrl, "/pet", "application/json")
121+
.withBody(pet)
122+
.withSuccessResponse[Pet](200)
119123
.withErrorResponse[Unit](400)
120124
.withErrorResponse[Unit](404)
121125
.withErrorResponse[Unit](405)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ class StoreApi(baseUrl: String) {
7777
* code 200 : Order (successful operation)
7878
* code 400 : (Invalid Order)
7979
*
80-
* @param body order placed for purchasing the pet
80+
* @param order order placed for purchasing the pet
8181
*/
82-
def placeOrder(body: Order): ApiRequest[Order] =
82+
def placeOrder(order: Order): ApiRequest[Order] =
8383
ApiRequest[Order](ApiMethods.POST, baseUrl, "/store/order", "application/json")
84-
.withBody(body)
84+
.withBody(order)
8585
.withSuccessResponse[Order](200)
8686
.withErrorResponse[Unit](400)
8787

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,47 @@ class UserApi(baseUrl: String) {
2929
* Expected answers:
3030
* code 0 : (successful operation)
3131
*
32-
* @param body Created user object
32+
* Available security schemes:
33+
* auth_cookie (apiKey)
34+
*
35+
* @param user Created user object
3336
*/
34-
def createUser(body: User): ApiRequest[Unit] =
37+
def createUser(user: User)(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
3538
ApiRequest[Unit](ApiMethods.POST, baseUrl, "/user", "application/json")
36-
.withBody(body)
39+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
40+
.withBody(user)
3741
.withDefaultSuccessResponse[Unit]
3842

3943

4044
/**
4145
* Expected answers:
4246
* code 0 : (successful operation)
4347
*
44-
* @param body List of user object
48+
* Available security schemes:
49+
* auth_cookie (apiKey)
50+
*
51+
* @param user List of user object
4552
*/
46-
def createUsersWithArrayInput(body: Seq[User]): ApiRequest[Unit] =
53+
def createUsersWithArrayInput(user: Seq[User])(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
4754
ApiRequest[Unit](ApiMethods.POST, baseUrl, "/user/createWithArray", "application/json")
48-
.withBody(body)
55+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
56+
.withBody(user)
4957
.withDefaultSuccessResponse[Unit]
5058

5159

5260
/**
5361
* Expected answers:
5462
* code 0 : (successful operation)
5563
*
56-
* @param body List of user object
64+
* Available security schemes:
65+
* auth_cookie (apiKey)
66+
*
67+
* @param user List of user object
5768
*/
58-
def createUsersWithListInput(body: Seq[User]): ApiRequest[Unit] =
69+
def createUsersWithListInput(user: Seq[User])(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
5970
ApiRequest[Unit](ApiMethods.POST, baseUrl, "/user/createWithList", "application/json")
60-
.withBody(body)
71+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
72+
.withBody(user)
6173
.withDefaultSuccessResponse[Unit]
6274

6375

@@ -68,10 +80,14 @@ class UserApi(baseUrl: String) {
6880
* code 400 : (Invalid username supplied)
6981
* code 404 : (User not found)
7082
*
83+
* Available security schemes:
84+
* auth_cookie (apiKey)
85+
*
7186
* @param username The name that needs to be deleted
7287
*/
73-
def deleteUser(username: String): ApiRequest[Unit] =
88+
def deleteUser(username: String)(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
7489
ApiRequest[Unit](ApiMethods.DELETE, baseUrl, "/user/{username}", "application/json")
90+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
7591
.withPathParam("username", username)
7692
.withErrorResponse[Unit](400)
7793
.withErrorResponse[Unit](404)
@@ -97,6 +113,7 @@ class UserApi(baseUrl: String) {
97113
* Expected answers:
98114
* code 200 : String (successful operation)
99115
* Headers :
116+
* Set-Cookie - Cookie authentication key for use with the `auth_cookie` apiKey authentication.
100117
* X-Rate-Limit - calls per hour allowed by the user
101118
* X-Expires-After - date in UTC when toekn expires
102119
* code 400 : (Invalid username/password supplied)
@@ -112,16 +129,21 @@ class UserApi(baseUrl: String) {
112129
.withErrorResponse[Unit](400)
113130

114131
object LoginUserHeaders {
132+
def setCookie(r: ApiReturnWithHeaders) = r.getStringHeader("Set-Cookie")
115133
def xRateLimit(r: ApiReturnWithHeaders) = r.getIntHeader("X-Rate-Limit")
116134
def xExpiresAfter(r: ApiReturnWithHeaders) = r.getDateTimeHeader("X-Expires-After")
117135
}
118136

119137
/**
120138
* Expected answers:
121139
* code 0 : (successful operation)
140+
*
141+
* Available security schemes:
142+
* auth_cookie (apiKey)
122143
*/
123-
def logoutUser(): ApiRequest[Unit] =
144+
def logoutUser()(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
124145
ApiRequest[Unit](ApiMethods.GET, baseUrl, "/user/logout", "application/json")
146+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
125147
.withDefaultSuccessResponse[Unit]
126148

127149

@@ -132,12 +154,16 @@ class UserApi(baseUrl: String) {
132154
* code 400 : (Invalid user supplied)
133155
* code 404 : (User not found)
134156
*
157+
* Available security schemes:
158+
* auth_cookie (apiKey)
159+
*
135160
* @param username name that need to be deleted
136-
* @param body Updated user object
161+
* @param user Updated user object
137162
*/
138-
def updateUser(username: String, body: User): ApiRequest[Unit] =
163+
def updateUser(username: String, user: User)(implicit apiKey: ApiKeyValue): ApiRequest[Unit] =
139164
ApiRequest[Unit](ApiMethods.PUT, baseUrl, "/user/{username}", "application/json")
140-
.withBody(body)
165+
.withApiKey(apiKey, "AUTH_KEY", COOKIE)
166+
.withBody(user)
141167
.withPathParam("username", username)
142168
.withErrorResponse[Unit](400)
143169
.withErrorResponse[Unit](404)

0 commit comments

Comments
 (0)