@goast/kotlin 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/lib/file-builder.js +6 -6
- package/cjs/lib/generators/models/model-generator.js +61 -58
- package/cjs/lib/generators/models/models-generator.js +1 -1
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +57 -51
- package/cjs/lib/generators/services/spring-controllers/spring-controller-generator.js +51 -37
- package/esm/lib/file-builder.js +6 -6
- package/esm/lib/generators/models/model-generator.js +62 -59
- package/esm/lib/generators/models/models-generator.js +1 -1
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +57 -51
- package/esm/lib/generators/services/spring-controllers/spring-controller-generator.js +51 -37
- package/package.json +2 -2
- package/types/lib/generators/models/model-generator.d.ts +2 -1
- package/types/lib/generators/models/models.d.ts +0 -1
package/cjs/lib/file-builder.js
CHANGED
|
@@ -38,9 +38,9 @@ class KotlinFileBuilder extends core_1.SourceBuilder {
|
|
|
38
38
|
allArgs.reduce((c, [key, value]) => { var _a; return c + (key ? key.length + 3 : 0) + ((_a = value === null || value === void 0 ? void 0 : value.length) !== null && _a !== void 0 ? _a : 0); }, 0) > 80;
|
|
39
39
|
this.parenthesize('()', (builder) => builder
|
|
40
40
|
.appendLineIf(multiline)
|
|
41
|
-
.
|
|
41
|
+
.forEach(allArgs, (builder, [name, value]) => builder
|
|
42
42
|
.append(name ? `${name} = ` : '')
|
|
43
|
-
.
|
|
43
|
+
.append((builder) => (typeof value === 'string' ? builder.append(value) : value(builder))), { separator: multiline ? ',\n' : ', ' })
|
|
44
44
|
.appendLineIf(multiline));
|
|
45
45
|
}
|
|
46
46
|
this.appendLine();
|
|
@@ -52,11 +52,11 @@ class KotlinFileBuilder extends core_1.SourceBuilder {
|
|
|
52
52
|
}
|
|
53
53
|
toString(addPadding = true) {
|
|
54
54
|
return new core_1.SourceBuilder(this.options)
|
|
55
|
-
.
|
|
56
|
-
.
|
|
57
|
-
.
|
|
55
|
+
.if(this.packageName !== undefined, (builder) => builder.appendLine(`package ${this.packageName}`).appendLine())
|
|
56
|
+
.append((builder) => this.imports.writeTo(builder))
|
|
57
|
+
.if(addPadding, (builder) => builder.ensurePreviousLineEmpty())
|
|
58
58
|
.append(super.toString())
|
|
59
|
-
.
|
|
59
|
+
.if(addPadding, (builder) => builder.ensureCurrentLineEmpty())
|
|
60
60
|
.toString();
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -31,7 +31,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
|
|
34
|
-
const typeName = this.getDeclarationTypeName(ctx);
|
|
34
|
+
const typeName = this.getDeclarationTypeName(ctx, ctx.schema);
|
|
35
35
|
const packageName = ctx.config.packageName + ctx.config.packageSuffix;
|
|
36
36
|
const filePath = `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}/${typeName}.kt`;
|
|
37
37
|
console.log(`Generating model ${packageName}.${typeName} to ${filePath}...`);
|
|
@@ -52,8 +52,9 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
52
52
|
generateFileContent(ctx, builder) {
|
|
53
53
|
let schema = ctx.schema;
|
|
54
54
|
if (schema.kind === 'oneOf') {
|
|
55
|
-
schema =
|
|
56
|
-
|
|
55
|
+
schema =
|
|
56
|
+
ctx.config.oneOfBehavior === 'treat-as-any-of'
|
|
57
|
+
? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
|
|
57
58
|
ctx.schema = schema;
|
|
58
59
|
}
|
|
59
60
|
if (schema.kind === 'object' || schema.kind === 'combined') {
|
|
@@ -72,6 +73,20 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
72
73
|
this.generateType(ctx, builder, schema);
|
|
73
74
|
}
|
|
74
75
|
}
|
|
76
|
+
generateTypeUsage(ctx, builder, schema) {
|
|
77
|
+
schema = (0, core_1.getSchemaReference)(schema, ['description']);
|
|
78
|
+
if (this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
79
|
+
const name = this.getDeclarationTypeName(ctx, schema);
|
|
80
|
+
const packageName = ctx.config.packageName + ctx.config.packageSuffix;
|
|
81
|
+
if (packageName) {
|
|
82
|
+
builder.addImport(name, packageName);
|
|
83
|
+
}
|
|
84
|
+
builder.append(name);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.generateType(ctx, builder, schema);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
75
90
|
generateObjectType(ctx, builder, schema) {
|
|
76
91
|
if (schema.properties.size === 0) {
|
|
77
92
|
if (schema.additionalProperties) {
|
|
@@ -95,13 +110,13 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
95
110
|
}
|
|
96
111
|
generateObjectInterface(ctx, builder, schema) {
|
|
97
112
|
builder
|
|
98
|
-
.
|
|
113
|
+
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
99
114
|
.ensureCurrentLineEmpty()
|
|
100
|
-
.
|
|
115
|
+
.append((builder) => this.generateObjectInterfaceAnnotations(ctx, builder, schema))
|
|
101
116
|
.ensureCurrentLineEmpty()
|
|
102
|
-
.
|
|
117
|
+
.append((builder) => this.generateObjectInterfaceSignature(ctx, builder, schema))
|
|
103
118
|
.append(' ')
|
|
104
|
-
.
|
|
119
|
+
.parenthesize('{}', (builder) => this.generateObjectInterfaceMembers(ctx, builder, schema), { multiline: true });
|
|
105
120
|
}
|
|
106
121
|
generateObjectInterfaceAnnotations(ctx, builder, schema) {
|
|
107
122
|
if (schema.discriminator) {
|
|
@@ -113,66 +128,64 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
113
128
|
]);
|
|
114
129
|
const entries = Object.entries(schema.discriminator.mapping);
|
|
115
130
|
if (entries.length > 0) {
|
|
116
|
-
builder.appendAnnotation('JsonSubTypes', 'com.fasterxml.jackson.annotation', entries.map(([value, schema]) => (builder) => {
|
|
117
|
-
const schemaResult = ctx.getSchemaResult(schema);
|
|
118
|
-
builder
|
|
119
|
-
.append(`JsonSubTypes.Type(value = ${schemaResult.typeName}::class, name = ${this.toStringLiteral(ctx, value)})`)
|
|
120
|
-
.addImport(schemaResult.typeName, schemaResult.packageName);
|
|
121
|
-
}));
|
|
131
|
+
builder.appendAnnotation('JsonSubTypes', 'com.fasterxml.jackson.annotation', entries.map(([value, schema]) => (builder) => builder.append('JsonSubTypes.Type(value = ', (builder) => this.generateTypeUsage(ctx, builder, schema), `::class, name = ${this.toStringLiteral(ctx, value)})`)));
|
|
122
132
|
}
|
|
123
133
|
}
|
|
124
134
|
}
|
|
125
135
|
generateObjectInterfaceSignature(ctx, builder, schema) {
|
|
126
|
-
builder.append('interface ').append(this.getDeclarationTypeName(ctx));
|
|
136
|
+
builder.append('interface ').append(this.getDeclarationTypeName(ctx, schema));
|
|
127
137
|
}
|
|
128
138
|
generateObjectInterfaceMembers(ctx, builder, schema) {
|
|
129
139
|
builder.forEach(this.sortProperties(ctx, schema, schema.properties.values()), (builder, property) => builder
|
|
130
140
|
.ensurePreviousLineEmpty()
|
|
131
|
-
.
|
|
141
|
+
.append((builder) => this.generateJsonPropertyAnnotation(ctx, builder, schema, property, 'get'))
|
|
132
142
|
.ensureCurrentLineEmpty()
|
|
133
143
|
.append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
|
|
134
|
-
.
|
|
135
|
-
.
|
|
144
|
+
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
145
|
+
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')));
|
|
136
146
|
}
|
|
137
147
|
generateObjectDataClass(ctx, builder, schema) {
|
|
138
|
-
const inheritedSchemas = schema.inheritedSchemas
|
|
148
|
+
const inheritedSchemas = schema.inheritedSchemas
|
|
149
|
+
.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated)
|
|
150
|
+
.filter((item, index, self) => self.indexOf(item) === index);
|
|
139
151
|
builder
|
|
140
|
-
.
|
|
152
|
+
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
141
153
|
.append('data class ')
|
|
142
|
-
.append(this.getDeclarationTypeName(ctx))
|
|
143
|
-
.
|
|
154
|
+
.append(this.getDeclarationTypeName(ctx, schema))
|
|
155
|
+
.parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(this.sortProperties(ctx, schema, schema.properties.values()), (builder, property) => builder
|
|
144
156
|
.ensurePreviousLineEmpty()
|
|
145
|
-
.
|
|
157
|
+
.append((builder) => this.generateObjectDataClassParameterAnnotations(ctx, builder, schema, property))
|
|
146
158
|
.appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
|
|
147
159
|
.append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
|
|
148
|
-
.
|
|
149
|
-
.
|
|
150
|
-
.
|
|
160
|
+
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
161
|
+
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')), { separator: ',\n' }), { multiline: true })
|
|
162
|
+
.if(inheritedSchemas.length > 0, (builder) => builder
|
|
151
163
|
.append(' : ')
|
|
152
|
-
.
|
|
153
|
-
|
|
164
|
+
.forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
|
|
165
|
+
separator: ', ',
|
|
166
|
+
}))
|
|
154
167
|
.append(' ')
|
|
155
|
-
.
|
|
168
|
+
.parenthesizeIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false, '{}', (builder) => builder.if(schema.additionalProperties !== undefined && schema.additionalProperties !== false, (builder) => builder
|
|
156
169
|
.appendLine('@JsonIgnore')
|
|
157
170
|
.addImport('JsonIgnore', 'com.fasterxml.jackson.annotation')
|
|
158
171
|
.append('val additionalProperties: Mutable')
|
|
159
|
-
.
|
|
172
|
+
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
160
173
|
.appendLine(' = mutableMapOf()')
|
|
161
174
|
.appendLine()
|
|
162
175
|
.appendLine('@JsonAnySetter')
|
|
163
176
|
.addImport('JsonAnySetter', 'com.fasterxml.jackson.annotation')
|
|
164
177
|
.append('fun set')
|
|
165
|
-
.parenthesize('()', (builder) => builder.append('name: String, value: ').
|
|
178
|
+
.parenthesize('()', (builder) => builder.append('name: String, value: ').if(schema.additionalProperties === true, (builder) => builder.append('Any?'), (builder) => this.generateTypeUsage(ctx, builder, schema.additionalProperties)))
|
|
166
179
|
.append(' ')
|
|
167
|
-
.
|
|
180
|
+
.parenthesize('{}', 'this.additionalProperties[name] = value', { multiline: true })
|
|
168
181
|
.appendLine()
|
|
169
182
|
.appendLine()
|
|
170
183
|
.appendLine('@JsonAnyGetter')
|
|
171
184
|
.addImport('JsonAnyGetter', 'com.fasterxml.jackson.annotation')
|
|
172
185
|
.append('fun getMap(): ')
|
|
173
|
-
.
|
|
186
|
+
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
174
187
|
.append(' ')
|
|
175
|
-
.
|
|
188
|
+
.parenthesize('{}', 'return this.additionalProperties', { multiline: true })), { multiline: true });
|
|
176
189
|
}
|
|
177
190
|
generateObjectDataClassParameterAnnotations(ctx, builder, schema, property) {
|
|
178
191
|
this.generatePropertyValidationAnnotations(ctx, builder, schema, property);
|
|
@@ -206,7 +219,9 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
206
219
|
builder
|
|
207
220
|
.append('@Schema')
|
|
208
221
|
.addImport('Schema', 'io.swagger.v3.oas.annotations.media')
|
|
209
|
-
.parenthesizeIf(parts.size > 0, '()', (builder) => builder.
|
|
222
|
+
.parenthesizeIf(parts.size > 0, '()', (builder) => builder.forEach(parts.entries(), (builder, [key, value]) => builder.append(`${key} = ${value}`), {
|
|
223
|
+
separator: ', ',
|
|
224
|
+
}))
|
|
210
225
|
.appendLine();
|
|
211
226
|
}
|
|
212
227
|
generateJsonPropertyAnnotation(ctx, builder, schema, property, scope) {
|
|
@@ -217,7 +232,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
217
232
|
.append(this.toStringLiteral(ctx, property.name))
|
|
218
233
|
.appendIf(schema.required.has(property.name), ', required = true'))
|
|
219
234
|
.appendLine()
|
|
220
|
-
.
|
|
235
|
+
.if(property.schema.custom['exclude-when-null'] === true, (builder) => builder
|
|
221
236
|
.append('@get:JsonInclude')
|
|
222
237
|
.addImport('JsonInclude', 'com.fasterxml.jackson.annotation')
|
|
223
238
|
.parenthesize('()', 'JsonInclude.Include.NON_NULL')
|
|
@@ -231,22 +246,10 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
231
246
|
const propertiesType = schema.additionalProperties;
|
|
232
247
|
builder
|
|
233
248
|
.append('Map')
|
|
234
|
-
.parenthesize('<>', (builder) => builder.append('String, ').
|
|
249
|
+
.parenthesize('<>', (builder) => builder.append('String, ').append((builder) => this.generateTypeUsage(ctx, builder, propertiesType)));
|
|
235
250
|
}
|
|
236
251
|
}
|
|
237
252
|
generateType(ctx, builder, schema) {
|
|
238
|
-
if (this.shouldGenerateTypeDeclaration(ctx, schema)) {
|
|
239
|
-
if (schema === ctx.schema) {
|
|
240
|
-
builder.append('Any?');
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
const schemaResult = ctx.getSchemaResult(schema);
|
|
244
|
-
builder.append(schemaResult.typeName);
|
|
245
|
-
if (schemaResult.packageName) {
|
|
246
|
-
builder.addImport(schemaResult.typeName, schemaResult.packageName);
|
|
247
|
-
}
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
253
|
switch (schema.kind) {
|
|
251
254
|
case 'boolean':
|
|
252
255
|
builder.append('Boolean');
|
|
@@ -323,23 +326,23 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
323
326
|
}
|
|
324
327
|
generateEnum(ctx, builder, schema) {
|
|
325
328
|
builder
|
|
326
|
-
.
|
|
329
|
+
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
327
330
|
.append('enum class ')
|
|
328
|
-
.append(this.getDeclarationTypeName(ctx))
|
|
331
|
+
.append(this.getDeclarationTypeName(ctx, schema))
|
|
329
332
|
.append('(val value: String) ')
|
|
330
|
-
.
|
|
333
|
+
.parenthesize('{}', (builder) => {
|
|
331
334
|
var _a;
|
|
332
|
-
return builder.
|
|
335
|
+
return builder.forEach((_a = schema.enum) !== null && _a !== void 0 ? _a : [], (builder, value) => builder
|
|
333
336
|
.append('@JsonProperty')
|
|
334
337
|
.addImport('JsonProperty', 'com.fasterxml.jackson.annotation')
|
|
335
338
|
.parenthesize('()', this.toStringLiteral(ctx, String(value)))
|
|
336
339
|
.appendLine()
|
|
337
340
|
.append((0, core_1.toCasing)(String(value), 'snake'))
|
|
338
|
-
.parenthesize('()', this.toStringLiteral(ctx, String(value))));
|
|
339
|
-
});
|
|
341
|
+
.parenthesize('()', this.toStringLiteral(ctx, String(value))), { separator: (builder) => builder.appendLine(',').appendLine() });
|
|
342
|
+
}, { multiline: true });
|
|
340
343
|
}
|
|
341
344
|
generateArrayType(ctx, builder, schema) {
|
|
342
|
-
builder.append('List').parenthesize('<>', (builder) => builder.
|
|
345
|
+
builder.append('List').parenthesize('<>', (builder) => builder.if(schema.items === undefined, (builder) => builder.append('Any?'), (builder) => this.generateTypeUsage(ctx, builder, schema.items)));
|
|
343
346
|
}
|
|
344
347
|
generateDocumentation(ctx, builder, schema) {
|
|
345
348
|
var _a, _b;
|
|
@@ -348,7 +351,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
348
351
|
builder
|
|
349
352
|
.ensurePreviousLineEmpty()
|
|
350
353
|
.appendLine('/**')
|
|
351
|
-
.
|
|
354
|
+
.appendWithLinePrefix(' * ', (builder) => builder
|
|
352
355
|
.appendLineIf(!!schema.description, schema.description)
|
|
353
356
|
.forEach(propertiesWithDescription, (builder, property) => { var _a; return builder.appendLine(`@param ${(0, core_1.toCasing)(property.name, 'camel')} ${(_a = property.schema.description) === null || _a === void 0 ? void 0 : _a.trim()}`); }))
|
|
354
357
|
.appendLine(' */');
|
|
@@ -375,8 +378,8 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
375
378
|
}
|
|
376
379
|
return true;
|
|
377
380
|
}
|
|
378
|
-
getDeclarationTypeName(ctx) {
|
|
379
|
-
return (0, core_1.toCasing)(
|
|
381
|
+
getDeclarationTypeName(ctx, schema) {
|
|
382
|
+
return (0, core_1.toCasing)(schema.name, 'pascal');
|
|
380
383
|
}
|
|
381
384
|
sortProperties(ctx, schema, properties) {
|
|
382
385
|
return [...properties].sort((a, b) => {
|
|
@@ -19,7 +19,7 @@ class KotlinModelsGenerator extends core_1.OpenApiSchemasGenerationProviderBase
|
|
|
19
19
|
}
|
|
20
20
|
generateSchema(ctx, schema) {
|
|
21
21
|
const modelGenerator = this._modelGeneratorFactory.create();
|
|
22
|
-
return modelGenerator.generate(Object.assign(Object.assign({}, ctx), { schema
|
|
22
|
+
return modelGenerator.generate(Object.assign(Object.assign({}, ctx), { schema }));
|
|
23
23
|
}
|
|
24
24
|
addSchemaResult(ctx, schema, result) {
|
|
25
25
|
ctx.output.models[schema.id] = result;
|
|
@@ -21,11 +21,11 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
21
21
|
}
|
|
22
22
|
generateApiClientFileContent(ctx, builder) {
|
|
23
23
|
builder
|
|
24
|
-
.
|
|
24
|
+
.append((builder) => this.generateApiClientClassAnnotations(ctx, builder))
|
|
25
25
|
.ensureCurrentLineEmpty()
|
|
26
|
-
.
|
|
26
|
+
.append((builder) => this.generateApiClientClassSignature(ctx, builder))
|
|
27
27
|
.append(' ')
|
|
28
|
-
.
|
|
28
|
+
.parenthesize('{}', (builder) => this.generateApiClientClassContent(ctx, builder), { multiline: true });
|
|
29
29
|
}
|
|
30
30
|
generateApiClientClassAnnotations(ctx, builder) {
|
|
31
31
|
// None for now
|
|
@@ -34,31 +34,31 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
34
34
|
builder
|
|
35
35
|
.append('class ')
|
|
36
36
|
.append(this.getApiClientName(ctx))
|
|
37
|
-
.
|
|
37
|
+
.parenthesize('()', (builder) => builder
|
|
38
38
|
.appendLine('basePath: String = defaultBasePath,')
|
|
39
39
|
.appendLine('client: OkHttpClient = ApiClient.defaultClient')
|
|
40
40
|
.addImport('OkHttpClient', 'okhttp3')
|
|
41
|
-
.addImport('ApiClient', ctx.infrastructurePackageName))
|
|
41
|
+
.addImport('ApiClient', ctx.infrastructurePackageName), { multiline: true })
|
|
42
42
|
.append(' : ')
|
|
43
43
|
.append('ApiClient(basePath, client)');
|
|
44
44
|
}
|
|
45
45
|
generateApiClientClassContent(ctx, builder) {
|
|
46
46
|
builder
|
|
47
|
-
.
|
|
47
|
+
.append((builder) => this.generateApiClientCompanionObject(ctx, builder))
|
|
48
48
|
.forEach(ctx.service.endpoints, (builder, endpoint) => builder
|
|
49
49
|
.ensurePreviousLineEmpty()
|
|
50
|
-
.
|
|
50
|
+
.append((builder) => this.generateApiClientMethod(ctx, builder, endpoint))
|
|
51
51
|
.ensurePreviousLineEmpty()
|
|
52
|
-
.
|
|
52
|
+
.append((builder) => this.generateApiClientHttpInfoMethod(ctx, builder, endpoint))
|
|
53
53
|
.ensurePreviousLineEmpty()
|
|
54
|
-
.
|
|
54
|
+
.append((builder) => this.generateApiClientRequestConfigMethod(ctx, builder, endpoint)))
|
|
55
55
|
.ensurePreviousLineEmpty()
|
|
56
|
-
.
|
|
56
|
+
.append((builder) => this.generateAdditionalMethods(ctx, builder));
|
|
57
57
|
}
|
|
58
58
|
generateApiClientCompanionObject(ctx, builder) {
|
|
59
59
|
builder
|
|
60
60
|
.append('companion object ')
|
|
61
|
-
.
|
|
61
|
+
.parenthesize('{}', (builder) => this.generateApiClientCompanionObjectContent(ctx, builder), { multiline: true });
|
|
62
62
|
}
|
|
63
63
|
generateApiClientCompanionObjectContent(ctx, builder) {
|
|
64
64
|
this.generateApiClientCompanionObjectDefaultBasePathProperty(ctx, builder);
|
|
@@ -67,30 +67,32 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
67
67
|
builder
|
|
68
68
|
.appendAnnotation('JvmStatic')
|
|
69
69
|
.append('val defaultBasePath: String by lazy ')
|
|
70
|
-
.
|
|
70
|
+
.parenthesize('{}', (builder) => builder
|
|
71
71
|
.appendLine(`System.getProperties().getProperty(ApiClient.baseUrlKey, ${this.toStringLiteral(ctx, this.getBasePath(ctx))})`)
|
|
72
|
-
.addImport('ApiClient', ctx.infrastructurePackageName));
|
|
72
|
+
.addImport('ApiClient', ctx.infrastructurePackageName), { multiline: true });
|
|
73
73
|
}
|
|
74
74
|
generateApiClientMethod(ctx, builder, endpoint) {
|
|
75
75
|
builder
|
|
76
|
-
.
|
|
76
|
+
.append((builder) => this.generateApiClientMethodDocumentation(ctx, builder, endpoint))
|
|
77
77
|
.ensureCurrentLineEmpty()
|
|
78
|
-
.
|
|
78
|
+
.append((builder) => this.generateApiClientMethodAnnotations(ctx, builder, endpoint))
|
|
79
79
|
.ensureCurrentLineEmpty()
|
|
80
|
-
.
|
|
80
|
+
.append((builder) => this.generateApiClientMethodSignature(ctx, builder, endpoint))
|
|
81
81
|
.append(' ')
|
|
82
|
-
.
|
|
82
|
+
.parenthesize('{}', (builder) => this.generateApiClientMethodContent(ctx, builder, endpoint), {
|
|
83
|
+
multiline: true,
|
|
84
|
+
});
|
|
83
85
|
}
|
|
84
86
|
generateApiClientMethodDocumentation(ctx, builder, endpoint) {
|
|
85
87
|
builder
|
|
86
88
|
.appendLine('/**')
|
|
87
|
-
.
|
|
89
|
+
.appendWithLinePrefix(' * ', (builder) => {
|
|
88
90
|
var _a;
|
|
89
91
|
return builder
|
|
90
92
|
.appendLine(`${(_a = endpoint.summary) !== null && _a !== void 0 ? _a : 'TODO: Provide summary'}`)
|
|
91
|
-
.
|
|
93
|
+
.append((builder) => this.generateParamDocEntries(ctx, builder, endpoint))
|
|
92
94
|
.append('@return ')
|
|
93
|
-
.
|
|
95
|
+
.append((builder) => this.generateApiClientMethodReturnType(ctx, builder, endpoint))
|
|
94
96
|
.appendLine()
|
|
95
97
|
.appendLine('@throws IllegalStateException If the request is not correctly configured')
|
|
96
98
|
.appendLine('@throws IOException Rethrows the OkHttp execute method exception')
|
|
@@ -119,7 +121,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
119
121
|
.append((0, core_1.toCasing)(endpoint.name, 'camel'))
|
|
120
122
|
.parenthesize('()', (builder) => this.generateApiClientMethodParameters(ctx, builder, endpoint))
|
|
121
123
|
.append(': ')
|
|
122
|
-
.
|
|
124
|
+
.append((builder) => this.generateApiClientMethodReturnType(ctx, builder, endpoint));
|
|
123
125
|
}
|
|
124
126
|
generateApiClientMethodParameters(ctx, builder, endpoint) {
|
|
125
127
|
this.generateParams(ctx, builder, endpoint, true);
|
|
@@ -135,38 +137,40 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
135
137
|
.appendLine()
|
|
136
138
|
.appendLine()
|
|
137
139
|
.append('return when (localVarResponse.responseType) ')
|
|
138
|
-
.
|
|
140
|
+
.parenthesize('{}', (builder) => builder
|
|
139
141
|
.append('ResponseType.Success -> ')
|
|
140
|
-
.
|
|
142
|
+
.if(responseSchema === undefined, (builder) => builder.append('Unit'), (builder) => builder
|
|
141
143
|
.append('(localVarResponse as Success<*>).data as ')
|
|
142
144
|
.addImport('Success', ctx.infrastructurePackageName)
|
|
143
|
-
.
|
|
145
|
+
.append((builder) => this.generateTypeUsage(ctx, builder, responseSchema)))
|
|
144
146
|
.ensureCurrentLineEmpty()
|
|
145
147
|
.appendLine(responseErrorHandlingCode)
|
|
146
148
|
.addImport('ClientError', ctx.infrastructurePackageName)
|
|
147
149
|
.addImport('ServerError', ctx.infrastructurePackageName)
|
|
148
|
-
.addImport('ResponseType', ctx.infrastructurePackageName));
|
|
150
|
+
.addImport('ResponseType', ctx.infrastructurePackageName), { multiline: true });
|
|
149
151
|
}
|
|
150
152
|
generateApiClientHttpInfoMethod(ctx, builder, endpoint) {
|
|
151
153
|
builder
|
|
152
|
-
.
|
|
154
|
+
.append((builder) => this.generateApiClientHttpInfoMethodDocumentation(ctx, builder, endpoint))
|
|
153
155
|
.ensureCurrentLineEmpty()
|
|
154
|
-
.
|
|
156
|
+
.append((builder) => this.generateApiClientHttpInfoMethodAnnotations(ctx, builder, endpoint))
|
|
155
157
|
.ensureCurrentLineEmpty()
|
|
156
|
-
.
|
|
158
|
+
.append((builder) => this.generateApiClientHttpInfoMethodSignature(ctx, builder, endpoint))
|
|
157
159
|
.append(' ')
|
|
158
|
-
.
|
|
160
|
+
.parenthesize('{}', (builder) => this.generateApiClientHttpInfoMethodContent(ctx, builder, endpoint), {
|
|
161
|
+
multiline: true,
|
|
162
|
+
});
|
|
159
163
|
}
|
|
160
164
|
generateApiClientHttpInfoMethodDocumentation(ctx, builder, endpoint) {
|
|
161
165
|
builder
|
|
162
166
|
.appendLine('/**')
|
|
163
|
-
.
|
|
167
|
+
.appendWithLinePrefix(' * ', (builder) => {
|
|
164
168
|
var _a;
|
|
165
169
|
return builder
|
|
166
170
|
.appendLine(`${(_a = endpoint.summary) !== null && _a !== void 0 ? _a : 'TODO: Provide summary'}`)
|
|
167
|
-
.
|
|
171
|
+
.append((builder) => this.generateParamDocEntries(ctx, builder, endpoint))
|
|
168
172
|
.append('@return ')
|
|
169
|
-
.
|
|
173
|
+
.append((builder) => this.generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint))
|
|
170
174
|
.appendLine()
|
|
171
175
|
.appendLine('@throws IllegalStateException If the request is not correctly configured')
|
|
172
176
|
.appendLine('@throws IOException Rethrows the OkHttp execute method exception')
|
|
@@ -187,7 +191,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
187
191
|
.append((0, core_1.toCasing)(endpoint.name, 'camel'), 'WithHttpInfo')
|
|
188
192
|
.parenthesize('()', (builder) => this.generateApiClientHttpInfoMethodSignatureParameters(ctx, builder, endpoint))
|
|
189
193
|
.append(': ')
|
|
190
|
-
.
|
|
194
|
+
.append((builder) => this.generateApiClientHttpInfoMethodReturnType(ctx, builder, endpoint));
|
|
191
195
|
}
|
|
192
196
|
generateApiClientHttpInfoMethodSignatureParameters(ctx, builder, endpoint) {
|
|
193
197
|
this.generateParams(ctx, builder, endpoint, true);
|
|
@@ -206,27 +210,29 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
206
210
|
.appendLine()
|
|
207
211
|
.append('return request')
|
|
208
212
|
.parenthesize('<>', (builder) => builder
|
|
209
|
-
.
|
|
213
|
+
.append((builder) => { var _a; return this.generateTypeUsage(ctx, builder, (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0].schema, 'Unit'); })
|
|
210
214
|
.append(', ')
|
|
211
|
-
.
|
|
212
|
-
.
|
|
215
|
+
.append((builder) => this.generateTypeUsage(ctx, builder, this.getResponseSchema(ctx, endpoint), 'Unit')))
|
|
216
|
+
.parenthesize('()', (builder) => builder.append('localVariableConfig'), { multiline: true });
|
|
213
217
|
}
|
|
214
218
|
generateApiClientRequestConfigMethod(ctx, builder, endpoint) {
|
|
215
219
|
builder
|
|
216
|
-
.
|
|
220
|
+
.append((builder) => this.generateApiClientRequestConfigMethodDocumentation(ctx, builder, endpoint))
|
|
217
221
|
.ensureCurrentLineEmpty()
|
|
218
|
-
.
|
|
222
|
+
.append((builder) => this.generateApiClientRequestConfigMethodAnnotations(ctx, builder, endpoint))
|
|
219
223
|
.ensureCurrentLineEmpty()
|
|
220
|
-
.
|
|
224
|
+
.append((builder) => this.generateApiClientRequestConfigMethodSignature(ctx, builder, endpoint))
|
|
221
225
|
.append(' ')
|
|
222
|
-
.
|
|
226
|
+
.parenthesize('{}', (builder) => this.generateApiClientRequestConfigMethodContent(ctx, builder, endpoint), {
|
|
227
|
+
multiline: true,
|
|
228
|
+
});
|
|
223
229
|
}
|
|
224
230
|
generateApiClientRequestConfigMethodDocumentation(ctx, builder, endpoint) {
|
|
225
231
|
builder
|
|
226
232
|
.appendLine('/**')
|
|
227
|
-
.
|
|
233
|
+
.appendWithLinePrefix(' * ', (builder) => builder
|
|
228
234
|
.appendLine(`To obtain the request config of the operation ${(0, core_1.toCasing)(endpoint.name, 'camel')}`)
|
|
229
|
-
.
|
|
235
|
+
.append((builder) => this.generateParamDocEntries(ctx, builder, endpoint))
|
|
230
236
|
.append('@return RequestConfig'))
|
|
231
237
|
.appendLine(' */');
|
|
232
238
|
}
|
|
@@ -239,7 +245,7 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
239
245
|
.append((0, core_1.toCasing)(endpoint.name, 'camel'), 'RequestConfig')
|
|
240
246
|
.parenthesize('()', (builder) => this.generateApiClientRequestConfigMethodSignatureParameters(ctx, builder, endpoint))
|
|
241
247
|
.append(': ')
|
|
242
|
-
.
|
|
248
|
+
.append((builder) => this.generateApiClientRequestConfigMethodReturnType(ctx, builder, endpoint));
|
|
243
249
|
}
|
|
244
250
|
generateApiClientRequestConfigMethodSignatureParameters(ctx, builder, endpoint) {
|
|
245
251
|
this.generateParams(ctx, builder, endpoint, true);
|
|
@@ -257,24 +263,24 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
257
263
|
.appendLineIf(!!endpoint.requestBody, `val localVariableBody = ${(0, core_1.toCasing)(this.getRequestBodyParamName(ctx, endpoint), 'camel')}`)
|
|
258
264
|
.appendLine('val localVariableQuery: MultiValueMap = mutableMapOf<String, List<String>>()')
|
|
259
265
|
.addImport('MultiValueMap', ctx.infrastructurePackageName)
|
|
260
|
-
.
|
|
266
|
+
.if(queryParameters.length > 0, (builder) => builder.indent((builder) => builder.append('.apply ').parenthesize('{}', (builder) => builder.forEach(queryParameters, (builder, param) => builder
|
|
261
267
|
.appendIf(!param.required, `if (${(0, core_1.toCasing)(param.name, 'camel')} != null) `)
|
|
262
|
-
.
|
|
263
|
-
.appendLine()))))
|
|
268
|
+
.parenthesizeIf(!param.required, '{}', (builder) => builder.appendLine(`put(${this.toStringLiteral(ctx, (0, core_1.toCasing)(param.name, 'camel'))}, listOf(${(0, core_1.toCasing)(param.name, 'camel')}.toString()))`), { multiline: true })
|
|
269
|
+
.appendLine()), { multiline: true })))
|
|
264
270
|
.ensureCurrentLineEmpty()
|
|
265
271
|
.appendLine('val localVariableHeaders: MutableMap<String, String> = mutableMapOf()')
|
|
266
272
|
.appendLineIf(((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) !== undefined, `localVariableHeaders["Content-Type"] = "${(_b = endpoint.requestBody) === null || _b === void 0 ? void 0 : _b.content[0].type}"`)
|
|
267
273
|
.appendLine()
|
|
268
274
|
.append('return RequestConfig')
|
|
269
275
|
.addImport('RequestConfig', ctx.infrastructurePackageName)
|
|
270
|
-
.
|
|
276
|
+
.parenthesize('()', (builder) => builder
|
|
271
277
|
.appendLine(`method = RequestMethod.${endpoint.method.toUpperCase()},`)
|
|
272
278
|
.addImport('RequestMethod', ctx.infrastructurePackageName)
|
|
273
279
|
.appendLine(`path = "${this.getPathWithInterpolation(ctx, endpoint)}",`)
|
|
274
280
|
.appendLine('query = localVariableQuery,')
|
|
275
281
|
.appendLine('headers = localVariableHeaders,')
|
|
276
282
|
.appendLine('requiresAuthentication = false,')
|
|
277
|
-
.appendLineIf(!!endpoint.requestBody, 'body = localVariableBody'));
|
|
283
|
+
.appendLineIf(!!endpoint.requestBody, 'body = localVariableBody'), { multiline: true });
|
|
278
284
|
}
|
|
279
285
|
generateAdditionalMethods(ctx, builder) {
|
|
280
286
|
this.generateEncodeUriComponentMethod(ctx, builder);
|
|
@@ -291,10 +297,10 @@ class DefaultKotlinOkHttp3Generator extends file_generator_1.KotlinFileGenerator
|
|
|
291
297
|
}
|
|
292
298
|
generateParams(ctx, builder, endpoint, includeTypeDefinition) {
|
|
293
299
|
const parameters = this.getAllParameters(ctx, endpoint);
|
|
294
|
-
builder.
|
|
300
|
+
builder.forEach(parameters, (builder, parameter) => builder.append((0, core_1.toCasing)(parameter.name, 'camel')).if(includeTypeDefinition, (builder) => builder
|
|
295
301
|
.append(': ')
|
|
296
|
-
.
|
|
297
|
-
.appendIf(!parameter.required, '? = ', this.getDefaultValue(ctx, parameter.schema))));
|
|
302
|
+
.append((builder) => this.generateTypeUsage(ctx, builder, parameter.schema))
|
|
303
|
+
.appendIf(!parameter.required, '? = ', this.getDefaultValue(ctx, parameter.schema))), { separator: ', ' });
|
|
298
304
|
}
|
|
299
305
|
generateTypeUsage(ctx, builder, schema, fallback, nullable) {
|
|
300
306
|
if (schema && schema.kind === 'array') {
|