1
1
package com.cjbooms.fabrikt.generators.client
2
2
3
+ import com.cjbooms.fabrikt.cli.ClientCodeGenOptionType
3
4
import com.cjbooms.fabrikt.configurations.Packages
4
5
import com.cjbooms.fabrikt.generators.GeneratorUtils
5
6
import com.cjbooms.fabrikt.generators.GeneratorUtils.getPrimaryContentMediaType
@@ -9,7 +10,9 @@ import com.cjbooms.fabrikt.generators.GeneratorUtils.hasMultipleSuccessResponseS
9
10
import com.cjbooms.fabrikt.generators.GeneratorUtils.toClassName
10
11
import com.cjbooms.fabrikt.generators.GeneratorUtils.toIncomingParameters
11
12
import com.cjbooms.fabrikt.generators.OasDefault
13
+ import com.cjbooms.fabrikt.generators.controller.metadata.SpringImports.RESPONSE_ENTITY
12
14
import com.cjbooms.fabrikt.generators.model.ModelGenerator.Companion.toModelType
15
+ import com.cjbooms.fabrikt.model.BodyParameter
13
16
import com.cjbooms.fabrikt.model.ClientType
14
17
import com.cjbooms.fabrikt.model.HeaderParam
15
18
import com.cjbooms.fabrikt.model.IncomingParameter
@@ -20,13 +23,15 @@ import com.reprezen.kaizen.oasparser.model3.Operation
20
23
import com.reprezen.kaizen.oasparser.model3.Path
21
24
import com.squareup.kotlinpoet.AnnotationSpec
22
25
import com.squareup.kotlinpoet.FunSpec
26
+ import com.squareup.kotlinpoet.KModifier
23
27
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
24
28
import com.squareup.kotlinpoet.TypeName
25
29
import com.squareup.kotlinpoet.asTypeName
26
30
27
31
object ClientGeneratorUtils {
28
32
const val ACCEPT_HEADER_NAME = " Accept"
29
33
const val ACCEPT_HEADER_VARIABLE_NAME = " acceptHeader"
34
+ const val CONTENT_TYPE_HEADER_NAME = " Content-Type"
30
35
const val ADDITIONAL_HEADERS_PARAMETER_NAME = " additionalHeaders"
31
36
const val ADDITIONAL_QUERY_PARAMETERS_PARAMETER_NAME = " additionalQueryParameters"
32
37
@@ -38,15 +43,15 @@ object ClientGeneratorUtils {
38
43
*/
39
44
fun Operation.getReturnType (packages : Packages ): TypeName {
40
45
return if (hasMultipleSuccessResponseSchemas()) {
41
- JsonNode ::class .asTypeName()
42
- } else {
43
- this .getPrimaryContentMediaType()?.let {
44
- toModelType(
45
- packages.base,
46
- KotlinTypeInfo .from(it.value.schema)
47
- )
48
- } ? : Unit ::class .asTypeName()
49
- }
46
+ JsonNode ::class .asTypeName()
47
+ } else {
48
+ this .getPrimaryContentMediaType()?.let {
49
+ toModelType(
50
+ packages.base,
51
+ KotlinTypeInfo .from(it.value.schema)
52
+ )
53
+ } ? : Unit ::class .asTypeName()
54
+ }
50
55
}
51
56
52
57
fun Operation.toClientReturnType (packages : Packages ): TypeName {
@@ -60,7 +65,12 @@ object ClientGeneratorUtils {
60
65
fun deriveClientParameters (path : Path , operation : Operation , basePackage : String ): List <IncomingParameter > {
61
66
fun needsAcceptHeaderParameter (path : Path , operation : Operation ): Boolean {
62
67
val hasAcceptParameter = GeneratorUtils .mergeParameters(path.parameters, operation.parameters)
63
- .any { parameter -> parameter.`in ` == " header" && parameter.name.equals(ACCEPT_HEADER_NAME , ignoreCase = true ) }
68
+ .any { parameter ->
69
+ parameter.`in ` == " header" && parameter.name.equals(
70
+ ACCEPT_HEADER_NAME ,
71
+ ignoreCase = true
72
+ )
73
+ }
64
74
return operation.hasMultipleContentMediaTypes() == true && ! hasAcceptParameter
65
75
}
66
76
@@ -88,7 +98,8 @@ object ClientGeneratorUtils {
88
98
89
99
fun FunSpec.Builder.addIncomingParameters (
90
100
parameters : List <IncomingParameter >,
91
- annotateRequestParameterWith : ((parameter: RequestParameter ) -> AnnotationSpec ? )? = null
101
+ annotateRequestParameterWith : ((parameter: RequestParameter ) -> AnnotationSpec ? )? = null,
102
+ annotateBodyParameterWith : ((parameter: BodyParameter ) -> AnnotationSpec ? )? = null,
92
103
): FunSpec .Builder {
93
104
val specs = parameters.map {
94
105
val builder = it.toParameterSpecBuilder()
@@ -100,8 +111,33 @@ object ClientGeneratorUtils {
100
111
builder.addAnnotation(annotationSpec)
101
112
}
102
113
}
114
+ if (it is BodyParameter ) {
115
+ annotateBodyParameterWith?.invoke(it)?.let { annotationSpec ->
116
+ builder.addAnnotation(annotationSpec)
117
+ }
118
+ }
103
119
builder.build()
104
120
}
105
121
return this .addParameters(specs)
106
122
}
123
+
124
+ /* *
125
+ * Adds suspend as modified to the func spec so that i can be used with CoroutineFeign
126
+ */
127
+ fun FunSpec.Builder.addSuspendModifier (options : Set <ClientCodeGenOptionType >): FunSpec .Builder {
128
+ if (options.contains(ClientCodeGenOptionType .SUSPEND_MODIFIER )) {
129
+ this .addModifiers(KModifier .SUSPEND )
130
+ }
131
+ return this
132
+ }
133
+
134
+ /* *
135
+ * Adds a ResponseEntity around the returned object so that we can get headers and statuscodes
136
+ */
137
+ fun TypeName.optionallyParameterizeWithResponseEntity (options : Set <ClientCodeGenOptionType >): TypeName {
138
+ if (options.contains(ClientCodeGenOptionType .SPRING_RESPONSE_ENTITY_WRAPPER )) {
139
+ return RESPONSE_ENTITY .parameterizedBy(this )
140
+ }
141
+ return this
142
+ }
107
143
}
0 commit comments