@@ -101,21 +101,25 @@ private fun TypeSpec.Builder.apiConstructor(): TypeSpec.Builder =
101
101
.build()
102
102
)
103
103
104
+ context(OpenAPIContext )
104
105
private fun Route.nestedTypes (): List <TypeSpec > = inputs() + returns() + bodies()
105
106
107
+ context(OpenAPIContext )
106
108
private fun Route.inputs (): List <TypeSpec > =
107
- input.mapNotNull { (it.type as ? Resolved .Value )?.value?.toTypeSpec () }
109
+ input.mapNotNull { (it.type as ? Resolved .Value )?.value?.toTypeSpecOrNull () }
108
110
111
+ context(OpenAPIContext )
109
112
private fun Route.returns (): List <TypeSpec > =
110
- returnType.types.values.mapNotNull { (it.type as ? Resolved .Value )?.value?.toTypeSpec () }
113
+ returnType.types.values.mapNotNull { (it.type as ? Resolved .Value )?.value?.toTypeSpecOrNull () }
111
114
115
+ context(OpenAPIContext )
112
116
private fun Route.bodies (): List <TypeSpec > =
113
117
body.types.values.flatMap { body ->
114
118
when (body) {
115
119
is Route .Body .Json .Defined ->
116
- listOfNotNull((body.type as ? Resolved .Value )?.value?.toTypeSpec ())
120
+ listOfNotNull((body.type as ? Resolved .Value )?.value?.toTypeSpecOrNull ())
117
121
118
- is Route .Body .Multipart .Value -> body.parameters.mapNotNull { it.type.value.toTypeSpec () }
122
+ is Route .Body .Multipart .Value -> body.parameters.mapNotNull { it.type.value.toTypeSpecOrNull () }
119
123
is Route .Body .Multipart .Ref ,
120
124
is Route .Body .Xml ,
121
125
is Route .Body .Json .FreeForm ,
@@ -198,85 +202,67 @@ private fun Route.toFun(implemented: Boolean): FunSpec =
198
202
if (implemented) {
199
203
addCode(
200
204
CodeBlock .builder()
201
- .addStatement(
202
- " val response = client.%M {" ,
203
- MemberName (" io.ktor.client.request" , " request" , isExtension = true )
204
- )
205
+ .addStatement(" val response = client.%M {" , request)
205
206
.withIndent {
206
207
addStatement(" configure()" )
207
- addStatement(" method = %T.%L" , ClassName (" io.ktor.http" , " HttpMethod" ), method.value)
208
- val replace =
209
- input
210
- .mapNotNull {
211
- if (it.input == Parameter .Input .Path )
212
- " .replace(\" {${it.name} }\" , ${toParamName(Named (it.name))} )"
213
- else null
214
- }
215
- .joinToString(separator = " " )
216
- addStatement(
217
- " url { %M(%S$replace ) }" ,
218
- MemberName (" io.ktor.http" , " path" , isExtension = true ),
219
- path
220
- )
221
- addContentType(body)
222
- addBody(body)
208
+ addStatement(" method = %T.%L" , HttpMethod , method.name())
209
+ addPathAndContent()
210
+ addBody()
223
211
}
224
212
.addStatement(" }" )
225
- .addStatement(
226
- " return response.%M()" ,
227
- MemberName (" io.ktor.client.call" , " body" , isExtension = true )
228
- )
213
+ .addStatement(" return response.%M()" , MemberName (" io.ktor.client.call" , " body" , isExtension = true ))
229
214
.build()
230
215
)
231
216
}
232
217
}
233
218
.build()
234
219
235
- context(OpenAPIContext )
236
- fun CodeBlock.Builder.addContentType (bodies : Route .Bodies ): CodeBlock .Builder =
220
+ context(OpenAPIContext , CodeBlock .Builder )
221
+ fun Route.addPathAndContent () {
222
+ val replace = input
223
+ .mapNotNull {
224
+ if (it.input == Parameter .Input .Path )
225
+ " .replace(\" {${it.name} }\" , ${toParamName(Named (it.name))} )"
226
+ else null
227
+ }
228
+ .joinToString(separator = " " )
229
+ addStatement(
230
+ " url { %M(%S$replace ) }" ,
231
+ MemberName (" io.ktor.http" , " path" , isExtension = true ),
232
+ path
233
+ )
234
+ addContentType(body)
235
+ }
236
+
237
+ context(OpenAPIContext , CodeBlock .Builder )
238
+ fun addContentType (bodies : Route .Bodies ) {
237
239
bodies.firstNotNullOfOrNull { (_, body) ->
238
240
when (body) {
239
241
is Route .Body .Json -> addStatement(
240
- " %M(%T.%L)" ,
241
- MemberName (" io.ktor.http" , " contentType" ),
242
- ClassName (" io.ktor.http" , " ContentType" , " Application" ),
243
- " Json"
242
+ " %M(%T.%L)" , contentType, ContentType .nested(" Application" ), " Json"
244
243
)
245
244
246
245
is Route .Body .Xml -> TODO (" Xml input body not supported yet." )
247
246
is Route .Body .OctetStream -> TODO (" OctetStream input body not supported yet." )
248
247
is Route .Body .Multipart .Ref ,
249
- is Route .Body .Multipart .Value -> addStatement(
250
- " %M(%T.%L)" ,
251
- MemberName (" io.ktor.http" , " contentType" ),
252
- ClassName (" io.ktor.http" , " ContentType" , " MultiPart" ),
253
- " FormData"
254
- )
248
+ is Route .Body .Multipart .Value ->
249
+ addStatement(" %M(%T.%L)" , contentType, ContentType .nested(" MultiPart" ), " FormData" )
255
250
}
256
251
}
257
- ? : this
252
+ }
258
253
259
- context(OpenAPIContext )
260
- fun CodeBlock.Builder. addBody (bodies : Route . Bodies ): CodeBlock . Builder =
261
- bodies .firstNotNullOfOrNull { (_, body) ->
254
+ context(OpenAPIContext , CodeBlock . Builder )
255
+ fun Route. addBody () {
256
+ body .firstNotNullOfOrNull { (_, body) ->
262
257
when (body) {
263
- is Route .Body .Json -> addStatement(
264
- " %M(%L)" ,
265
- MemberName (" io.ktor.client.request" , " setBody" , isExtension = true ),
266
- " body"
267
- )
258
+ is Route .Body .Json -> addStatement(" %M(%L)" , setBody, " body" )
268
259
269
260
is Route .Body .Xml -> TODO (" Xml input body not supported yet." )
270
261
is Route .Body .OctetStream -> TODO (" OctetStream input body not supported yet." )
271
- is Route .Body .Multipart .Ref ,
272
- is Route .Body .Multipart .Value -> {
273
- body as Route .Body .Multipart
274
- addStatement(" %M(" , MemberName (" io.ktor.client.request" , " setBody" , isExtension = true ))
262
+ is Route .Body .Multipart -> {
263
+ addStatement(" %M(" , setBody)
275
264
withIndent {
276
- addStatement(
277
- " %M {" ,
278
- MemberName (" io.ktor.client.request.forms" , " formData" , isExtension = true )
279
- )
265
+ addStatement(" %M {" , formData)
280
266
withIndent {
281
267
when (body) {
282
268
is Route .Body .Multipart .Value ->
@@ -304,7 +290,7 @@ fun CodeBlock.Builder.addBody(bodies: Route.Bodies): CodeBlock.Builder =
304
290
}
305
291
}
306
292
}
307
- ? : this
293
+ }
308
294
309
295
context(OpenAPIContext )
310
296
fun Route.params (defaults : Boolean ): List <ParameterSpec > =
@@ -335,6 +321,7 @@ fun Route.requestBody(defaults: Boolean): List<ParameterSpec> {
335
321
description : String?
336
322
): ParameterSpec =
337
323
ParameterSpec .builder(name, type.toTypeName().copy(nullable = nullable))
324
+ .description(description)
338
325
.apply { if (defaults && nullable) defaultValue(" null" ) }
339
326
.build()
340
327
0 commit comments