@goast/kotlin 0.0.1 → 0.0.3
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/generators/index.js +1 -0
- package/cjs/lib/generators/models/model-generator.js +77 -25
- package/cjs/lib/generators/services/okhttp3-clients/index.js +6 -0
- package/cjs/lib/generators/services/okhttp3-clients/models.js +5 -0
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +411 -0
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +68 -0
- package/cjs/lib/generators/services/spring-controllers/spring-controller-generator.js +25 -45
- package/cjs/lib/utils.js +16 -1
- package/esm/lib/generators/index.js +1 -0
- package/esm/lib/generators/models/model-generator.js +77 -25
- package/esm/lib/generators/services/okhttp3-clients/index.js +3 -0
- package/esm/lib/generators/services/okhttp3-clients/models.js +2 -0
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +407 -0
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +64 -0
- package/esm/lib/generators/services/spring-controllers/spring-controller-generator.js +25 -45
- package/esm/lib/utils.js +14 -0
- package/package.json +2 -2
- package/types/lib/file-builder.d.ts +1 -1
- package/types/lib/generators/index.d.ts +1 -0
- package/types/lib/generators/models/model-generator.d.ts +9 -3
- package/types/lib/generators/services/okhttp3-clients/index.d.ts +3 -0
- package/types/lib/generators/services/okhttp3-clients/models.d.ts +29 -0
- package/types/lib/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +60 -0
- package/types/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.d.ts +22 -0
- package/types/lib/generators/services/spring-controllers/models.d.ts +3 -1
- package/types/lib/generators/services/spring-controllers/spring-controller-generator.d.ts +2 -0
- package/types/lib/utils.d.ts +1 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KotlinOkHttp3ClientsGenerator = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const fs_extra_1 = require("fs-extra");
|
|
7
|
+
const core_1 = require("@goast/core");
|
|
8
|
+
const models_1 = require("./models");
|
|
9
|
+
const okhttp3_client_generator_1 = require("./okhttp3-client-generator");
|
|
10
|
+
class KotlinOkHttp3ClientsGenerator extends core_1.OpenApiServicesGenerationProviderBase {
|
|
11
|
+
constructor(serviceGeneratorFactory) {
|
|
12
|
+
super();
|
|
13
|
+
this._serviceGeneratorFactory = serviceGeneratorFactory !== null && serviceGeneratorFactory !== void 0 ? serviceGeneratorFactory : core_1.Factory.fromValue(new okhttp3_client_generator_1.DefaultKotlinOkHttp3Generator());
|
|
14
|
+
}
|
|
15
|
+
onGenerate(ctx) {
|
|
16
|
+
this.copyInfrastructureFiles(ctx);
|
|
17
|
+
return super.onGenerate(ctx);
|
|
18
|
+
}
|
|
19
|
+
initResult() {
|
|
20
|
+
return {
|
|
21
|
+
clients: {},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
generateService(ctx, service) {
|
|
25
|
+
const serviceGenerator = this._serviceGeneratorFactory.create();
|
|
26
|
+
return serviceGenerator.generate(Object.assign(Object.assign({}, ctx), { service }));
|
|
27
|
+
}
|
|
28
|
+
addServiceResult(ctx, service, result) {
|
|
29
|
+
ctx.output.clients[service.id] = result;
|
|
30
|
+
}
|
|
31
|
+
buildContext(context, config) {
|
|
32
|
+
context.data.services = context.data.services.filter((x) => x.name !== 'exclude-from-generation');
|
|
33
|
+
const providerContext = this.getProviderContext(context, config, models_1.defaultKotlinOkHttp3ClientsGeneratorConfig);
|
|
34
|
+
return Object.assign(providerContext, {
|
|
35
|
+
packageName: this.getPackageName(providerContext.config),
|
|
36
|
+
infrastructurePackageName: this.getInfrastructurePackageName(providerContext.config),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
getInfrastructurePackageName(config) {
|
|
40
|
+
console.log(config);
|
|
41
|
+
if (typeof config.infrastructurePackageName === 'string') {
|
|
42
|
+
return config.infrastructurePackageName;
|
|
43
|
+
}
|
|
44
|
+
if (config.infrastructurePackageName.mode === 'append-package-name') {
|
|
45
|
+
return config.packageName + config.infrastructurePackageName.value;
|
|
46
|
+
}
|
|
47
|
+
if (config.infrastructurePackageName.mode === 'append-full-package-name') {
|
|
48
|
+
return this.getPackageName(config) + config.infrastructurePackageName.value;
|
|
49
|
+
}
|
|
50
|
+
return config.infrastructurePackageName.value;
|
|
51
|
+
}
|
|
52
|
+
getPackageName(config) {
|
|
53
|
+
return config.packageName + config.packageSuffix;
|
|
54
|
+
}
|
|
55
|
+
copyInfrastructureFiles(ctx) {
|
|
56
|
+
const sourceDir = (0, path_1.resolve)((0, path_1.dirname)(require.resolve('@goast/kotlin')), '../assets/client/okhttp3');
|
|
57
|
+
const targetDir = (0, path_1.resolve)(ctx.config.outputDir, ctx.infrastructurePackageName.replace(/\./g, '/'));
|
|
58
|
+
(0, fs_extra_1.ensureDirSync)(targetDir);
|
|
59
|
+
const files = (0, fs_extra_1.readdirSync)(sourceDir);
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
const fileContent = (0, fs_extra_1.readFileSync)((0, path_1.resolve)(sourceDir, file))
|
|
62
|
+
.toString()
|
|
63
|
+
.replace(/@PACKAGE_NAME@/g, ctx.infrastructurePackageName);
|
|
64
|
+
(0, fs_1.writeFileSync)((0, path_1.resolve)(targetDir, file), fileContent);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.KotlinOkHttp3ClientsGenerator = KotlinOkHttp3ClientsGenerator;
|
|
@@ -4,6 +4,7 @@ exports.DefaultKotlinSpringControllerGenerator = void 0;
|
|
|
4
4
|
const fs_extra_1 = require("fs-extra");
|
|
5
5
|
const core_1 = require("@goast/core");
|
|
6
6
|
const file_builder_1 = require("../../../file-builder");
|
|
7
|
+
const utils_1 = require("../../../utils");
|
|
7
8
|
const file_generator_1 = require("../../file-generator");
|
|
8
9
|
class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFileGenerator {
|
|
9
10
|
generate(ctx) {
|
|
@@ -33,10 +34,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
33
34
|
.ensureCurrentLineEmpty()
|
|
34
35
|
.apply((builder) => this.generateApiInterfaceSignature(ctx, builder))
|
|
35
36
|
.append(' ')
|
|
36
|
-
.
|
|
37
|
-
.appendLine()
|
|
38
|
-
.apply((builder) => this.generateApiInterfaceContent(ctx, builder))
|
|
39
|
-
.ensureCurrentLineEmpty());
|
|
37
|
+
.parenthesizeMultiline('{}', (builder) => this.generateApiInterfaceContent(ctx, builder));
|
|
40
38
|
}
|
|
41
39
|
generateApiInterfaceAnnotations(ctx, builder) {
|
|
42
40
|
builder
|
|
@@ -66,10 +64,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
66
64
|
.ensureCurrentLineEmpty()
|
|
67
65
|
.apply((builder) => this.generateApiInterfaceMethodSignature(ctx, builder, endpoint))
|
|
68
66
|
.append(' ')
|
|
69
|
-
.
|
|
70
|
-
.appendLine()
|
|
71
|
-
.apply((builder) => this.generateApiInterfaceMethodContent(ctx, builder, endpoint))
|
|
72
|
-
.appendLine());
|
|
67
|
+
.parenthesizeMultiline('{}', (builder) => this.generateApiInterfaceMethodContent(ctx, builder, endpoint));
|
|
73
68
|
}
|
|
74
69
|
generateApiInterfaceMethodAnnnotations(ctx, builder, endpoint) {
|
|
75
70
|
var _a, _b, _c;
|
|
@@ -80,9 +75,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
80
75
|
['description', this.toStringLiteral(ctx, (_b = endpoint.description) === null || _b === void 0 ? void 0 : _b.trim())],
|
|
81
76
|
[
|
|
82
77
|
'responses',
|
|
83
|
-
(builder) => builder.
|
|
84
|
-
.appendLine()
|
|
85
|
-
.forEachSeparated(endpoint.responses, ',\n', (builder, response) => builder
|
|
78
|
+
(builder) => builder.parenthesizeMultiline('[]', (builder) => builder.forEachSeparated(endpoint.responses, ',\n', (builder, response) => builder
|
|
86
79
|
.append('ApiResponse')
|
|
87
80
|
.addImport('ApiResponse', 'io.swagger.v3.oas.annotations.responses')
|
|
88
81
|
.parenthesize('()', (builder) => {
|
|
@@ -90,14 +83,13 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
90
83
|
return builder
|
|
91
84
|
.append(`responseCode = ${this.toStringLiteral(ctx, (_a = response.statusCode) === null || _a === void 0 ? void 0 : _a.toString())}, `)
|
|
92
85
|
.append(`description = ${this.toStringLiteral(ctx, (_b = response.description) === null || _b === void 0 ? void 0 : _b.trim())}`);
|
|
93
|
-
}))
|
|
94
|
-
.appendLine()),
|
|
86
|
+
}))),
|
|
95
87
|
endpoint.responses.length > 0,
|
|
96
88
|
],
|
|
97
89
|
])
|
|
98
90
|
.appendAnnotation('RequestMapping', 'org.springframework.web.bind.annotation', [
|
|
99
91
|
['method', '[RequestMethod.' + endpoint.method.toUpperCase() + ']'],
|
|
100
|
-
['value', '[' + this.toStringLiteral(ctx, endpoint
|
|
92
|
+
['value', '[' + this.toStringLiteral(ctx, this.getEndpointPath(ctx, endpoint)) + ']'],
|
|
101
93
|
[
|
|
102
94
|
'consumes',
|
|
103
95
|
'[' + ((_c = endpoint.requestBody) === null || _c === void 0 ? void 0 : _c.content.map((x) => this.toStringLiteral(ctx, x.type)).join(', ')) + ']',
|
|
@@ -109,10 +101,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
109
101
|
generateApiInterfaceMethodSignature(ctx, builder, endpoint) {
|
|
110
102
|
builder
|
|
111
103
|
.append(`suspend fun ${(0, core_1.toCasing)(endpoint.name, 'camel')}`)
|
|
112
|
-
.
|
|
113
|
-
.appendLine()
|
|
114
|
-
.apply((builder) => this.generateApiInterfaceMethodParameters(ctx, builder, endpoint))
|
|
115
|
-
.appendLine())
|
|
104
|
+
.parenthesizeMultiline('()', (builder) => this.generateApiInterfaceMethodParameters(ctx, builder, endpoint))
|
|
116
105
|
.append(': ')
|
|
117
106
|
.apply((builder) => this.generateApiInterfaceMethodReturnType(ctx, builder, endpoint));
|
|
118
107
|
}
|
|
@@ -197,10 +186,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
197
186
|
.ensureCurrentLineEmpty()
|
|
198
187
|
.apply((builder) => this.generateApiControllerSignature(ctx, builder))
|
|
199
188
|
.append(' ')
|
|
200
|
-
.
|
|
201
|
-
.appendLine()
|
|
202
|
-
.apply((builder) => this.generateApiControllerContent(ctx, builder))
|
|
203
|
-
.ensureCurrentLineEmpty());
|
|
189
|
+
.parenthesizeMultiline('{}', (builder) => this.generateApiControllerContent(ctx, builder));
|
|
204
190
|
}
|
|
205
191
|
generateApiControllerAnnotations(ctx, builder) {
|
|
206
192
|
builder
|
|
@@ -216,10 +202,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
216
202
|
builder
|
|
217
203
|
.append('class ')
|
|
218
204
|
.append(this.getApiControllerName(ctx))
|
|
219
|
-
.
|
|
220
|
-
.appendLine()
|
|
221
|
-
.apply((builder) => this.generateApiControllerParameters(ctx, builder))
|
|
222
|
-
.ensureCurrentLineEmpty())
|
|
205
|
+
.parenthesizeMultiline('()', (builder) => this.generateApiControllerParameters(ctx, builder))
|
|
223
206
|
.append(' : ')
|
|
224
207
|
.append(this.getApiInterfaceName(ctx));
|
|
225
208
|
}
|
|
@@ -236,12 +219,10 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
236
219
|
.appendLine(this.getApiDelegateInterfaceName(ctx))
|
|
237
220
|
.appendLine()
|
|
238
221
|
.append('init ')
|
|
239
|
-
.
|
|
240
|
-
.appendLine()
|
|
222
|
+
.parenthesizeMultiline('{}', (builder) => builder
|
|
241
223
|
.append('this.delegate = Optional.ofNullable(delegate).orElse')
|
|
242
224
|
.addImport('Optional', 'java.util')
|
|
243
|
-
.parenthesize('()', (builder) => builder.append('object : ').append(this.getApiDelegateInterfaceName(ctx)).append(' {}'))
|
|
244
|
-
.appendLine())
|
|
225
|
+
.parenthesize('()', (builder) => builder.append('object : ').append(this.getApiDelegateInterfaceName(ctx)).append(' {}')))
|
|
245
226
|
.appendLine()
|
|
246
227
|
.appendLine()
|
|
247
228
|
.appendLine(`override fun getDelegate(): ${this.getApiDelegateInterfaceName(ctx)} = delegate`);
|
|
@@ -262,10 +243,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
262
243
|
.ensureCurrentLineEmpty()
|
|
263
244
|
.apply((builder) => this.generateApiDelegateInterfaceSignature(ctx, builder))
|
|
264
245
|
.append(' ')
|
|
265
|
-
.
|
|
266
|
-
.appendLine()
|
|
267
|
-
.apply((builder) => this.generateApiDelegateInterfaceContent(ctx, builder))
|
|
268
|
-
.ensureCurrentLineEmpty());
|
|
246
|
+
.parenthesizeMultiline('{}', (builder) => this.generateApiDelegateInterfaceContent(ctx, builder));
|
|
269
247
|
}
|
|
270
248
|
generateApiDelegateInterfaceAnnotations(ctx, builder) {
|
|
271
249
|
builder.appendAnnotation('Generated', 'jakarta.annotation', [
|
|
@@ -290,10 +268,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
290
268
|
.ensureCurrentLineEmpty()
|
|
291
269
|
.apply((builder) => this.generateApiDelegateInterfaceMethodSignature(ctx, builder, endpoint))
|
|
292
270
|
.append(' ')
|
|
293
|
-
.
|
|
294
|
-
.appendLine()
|
|
295
|
-
.apply((builder) => this.generateApiDelegateInterfaceMethodContent(ctx, builder, endpoint))
|
|
296
|
-
.ensureCurrentLineEmpty());
|
|
271
|
+
.parenthesizeMultiline('{}', (builder) => this.generateApiDelegateInterfaceMethodContent(ctx, builder, endpoint));
|
|
297
272
|
}
|
|
298
273
|
generateApiDelegateInterfaceMethodAnnnotations(ctx, builder, endpoint) {
|
|
299
274
|
// None for now.
|
|
@@ -301,10 +276,7 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
301
276
|
generateApiDelegateInterfaceMethodSignature(ctx, builder, endpoint) {
|
|
302
277
|
builder
|
|
303
278
|
.append(`suspend fun ${(0, core_1.toCasing)(endpoint.name, 'camel')}`)
|
|
304
|
-
.
|
|
305
|
-
.appendLine()
|
|
306
|
-
.apply((builder) => this.generateApiDelegateInterfaceMethodParameters(ctx, builder, endpoint))
|
|
307
|
-
.appendLine())
|
|
279
|
+
.parenthesizeMultiline('()', (builder) => this.generateApiDelegateInterfaceMethodParameters(ctx, builder, endpoint))
|
|
308
280
|
.append(': ')
|
|
309
281
|
.apply((builder) => this.generateApiDelegateInterfaceMethodReturnType(ctx, builder, endpoint));
|
|
310
282
|
}
|
|
@@ -359,11 +331,17 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
359
331
|
}
|
|
360
332
|
}
|
|
361
333
|
getControllerRequestMapping(ctx, prefix) {
|
|
362
|
-
|
|
363
|
-
const basePath = (_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/';
|
|
334
|
+
const basePath = this.getBasePath(ctx);
|
|
364
335
|
prefix !== null && prefix !== void 0 ? prefix : (prefix = `openapi.${(0, core_1.toCasing)(ctx.service.name, 'camel')}`);
|
|
365
336
|
return this.toStringLiteral(ctx, `\${${prefix}.base-path:${basePath}}`);
|
|
366
337
|
}
|
|
338
|
+
getBasePath(ctx) {
|
|
339
|
+
var _a, _b, _c, _d, _e, _f;
|
|
340
|
+
return (0, utils_1.modifyString)((_f = (_e = (_d = (_c = ((_a = ctx.service.$src) !== null && _a !== void 0 ? _a : (_b = ctx.service.endpoints[0]) === null || _b === void 0 ? void 0 : _b.$src)) === null || _c === void 0 ? void 0 : _c.document.servers) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : '/', ctx.config.basePath, ctx.service);
|
|
341
|
+
}
|
|
342
|
+
getEndpointPath(ctx, endpoint) {
|
|
343
|
+
return (0, utils_1.modifyString)(endpoint.path, ctx.config.pathModifier, endpoint);
|
|
344
|
+
}
|
|
367
345
|
getDirectoryPath(ctx, packageName) {
|
|
368
346
|
return `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}`;
|
|
369
347
|
}
|
|
@@ -387,11 +365,13 @@ class DefaultKotlinSpringControllerGenerator extends file_generator_1.KotlinFile
|
|
|
387
365
|
const parameters = endpoint.parameters.filter((parameter) => parameter.target === 'query' || parameter.target === 'path');
|
|
388
366
|
if (endpoint.requestBody) {
|
|
389
367
|
const schema = endpoint.requestBody.content[0].schema;
|
|
368
|
+
const schemaInfo = this.getSchemaInfo(ctx, schema);
|
|
369
|
+
const name = /^Any\??$/.test(schemaInfo.typeName) ? 'body' : schemaInfo.typeName;
|
|
390
370
|
parameters.push({
|
|
391
371
|
$src: undefined,
|
|
392
372
|
$ref: undefined,
|
|
393
373
|
id: 'body',
|
|
394
|
-
name
|
|
374
|
+
name,
|
|
395
375
|
target: 'body',
|
|
396
376
|
schema,
|
|
397
377
|
required: endpoint.requestBody.required,
|
package/cjs/lib/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toKotlinPropertyName = exports.toKotlinStringLiteral = void 0;
|
|
3
|
+
exports.modifyString = exports.toKotlinPropertyName = exports.toKotlinStringLiteral = void 0;
|
|
4
4
|
function toKotlinStringLiteral(value) {
|
|
5
5
|
if (!value) {
|
|
6
6
|
return '""';
|
|
@@ -20,3 +20,18 @@ function toKotlinPropertyName(value) {
|
|
|
20
20
|
return `\`${value}\``;
|
|
21
21
|
}
|
|
22
22
|
exports.toKotlinPropertyName = toKotlinPropertyName;
|
|
23
|
+
function modifyString(value, modifier, ...args) {
|
|
24
|
+
var _a;
|
|
25
|
+
if (typeof modifier === 'string') {
|
|
26
|
+
return modifier;
|
|
27
|
+
}
|
|
28
|
+
if (modifier instanceof RegExp) {
|
|
29
|
+
const match = value.match(modifier);
|
|
30
|
+
return (_a = match === null || match === void 0 ? void 0 : match[0]) !== null && _a !== void 0 ? _a : value;
|
|
31
|
+
}
|
|
32
|
+
if (typeof modifier === 'function') {
|
|
33
|
+
return modifier(value, ...args);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
exports.modifyString = modifyString;
|
|
@@ -74,26 +74,77 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
77
|
-
this.
|
|
77
|
+
this.generateObjectPackageMember(ctx, builder, schema);
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
|
|
80
|
+
generateObjectPackageMember(ctx, builder, schema) {
|
|
81
|
+
if (schema.discriminator) {
|
|
82
|
+
this.generateObjectInterface(ctx, builder, schema);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.generateObjectDataClass(ctx, builder, schema);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
generateObjectInterface(ctx, builder, schema) {
|
|
89
|
+
builder
|
|
90
|
+
.apply((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
91
|
+
.ensureCurrentLineEmpty()
|
|
92
|
+
.apply((builder) => this.generateObjectInterfaceAnnotations(ctx, builder, schema))
|
|
93
|
+
.ensureCurrentLineEmpty()
|
|
94
|
+
.apply((builder) => this.generateObjectInterfaceSignature(ctx, builder, schema))
|
|
95
|
+
.append(' ')
|
|
96
|
+
.parenthesizeMultiline('{}', (builder) => this.generateObjectInterfaceMembers(ctx, builder, schema));
|
|
97
|
+
}
|
|
98
|
+
generateObjectInterfaceAnnotations(ctx, builder, schema) {
|
|
99
|
+
if (schema.discriminator) {
|
|
100
|
+
builder.appendAnnotation('JsonTypeInfo', 'com.fasterxml.jackson.annotation', [
|
|
101
|
+
['use', 'JsonTypeInfo.Id.NAME'],
|
|
102
|
+
['include', 'JsonTypeInfo.As.PROPERTY'],
|
|
103
|
+
['property', this.toStringLiteral(ctx, schema.discriminator.propertyName)],
|
|
104
|
+
['visible', 'true'],
|
|
105
|
+
]);
|
|
106
|
+
const entries = Object.entries(schema.discriminator.mapping);
|
|
107
|
+
if (entries.length > 0) {
|
|
108
|
+
builder.appendAnnotation('JsonSubTypes', 'com.fasterxml.jackson.annotation', entries.map(([value, schema]) => (builder) => {
|
|
109
|
+
const schemaResult = ctx.getSchemaResult(schema);
|
|
110
|
+
builder
|
|
111
|
+
.append(`JsonSubTypes.Type(value = ${schemaResult.typeName}::class, name = ${this.toStringLiteral(ctx, value)})`)
|
|
112
|
+
.addImport(schemaResult.typeName, schemaResult.packageName);
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
generateObjectInterfaceSignature(ctx, builder, schema) {
|
|
118
|
+
builder.append('interface ').append(this.getDeclarationTypeName(ctx));
|
|
119
|
+
}
|
|
120
|
+
generateObjectInterfaceMembers(ctx, builder, schema) {
|
|
121
|
+
builder.forEach(this.sortProperties(ctx, schema, schema.properties.values()), (builder, property) => builder
|
|
122
|
+
.ensurePreviousLineEmpty()
|
|
123
|
+
.apply((builder) => this.generateJsonPropertyAnnotation(ctx, builder, schema, property, 'get'))
|
|
124
|
+
.ensureCurrentLineEmpty()
|
|
125
|
+
.append(`val ${toCasing(property.name, 'camel')}: `)
|
|
126
|
+
.apply((builder) => this.generateType(ctx, builder, property.schema))
|
|
127
|
+
.applyIf(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')));
|
|
128
|
+
}
|
|
129
|
+
generateObjectDataClass(ctx, builder, schema) {
|
|
130
|
+
const inheritedSchemas = schema.inheritedSchemas.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated);
|
|
81
131
|
builder
|
|
82
132
|
.apply((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
83
133
|
.append('data class ')
|
|
84
134
|
.append(this.getDeclarationTypeName(ctx))
|
|
85
|
-
.
|
|
86
|
-
.appendLine()
|
|
87
|
-
.forEachSeparated(this.sortProperties(ctx, schema, schema.properties.values()), ',\n', (builder, property) => builder
|
|
135
|
+
.parenthesizeMultilineIf(schema.properties.size > 0, '()', (builder) => builder.forEachSeparated(this.sortProperties(ctx, schema, schema.properties.values()), ',\n', (builder, property) => builder
|
|
88
136
|
.ensurePreviousLineEmpty()
|
|
89
|
-
.apply((builder) => this.
|
|
137
|
+
.apply((builder) => this.generateObjectDataClassParameterAnnotations(ctx, builder, schema, property))
|
|
138
|
+
.appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
|
|
90
139
|
.append(`val ${toCasing(property.name, 'camel')}: `)
|
|
91
140
|
.apply((builder) => this.generateType(ctx, builder, property.schema))
|
|
92
|
-
.applyIf(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')))
|
|
93
|
-
.
|
|
94
|
-
.
|
|
95
|
-
.
|
|
96
|
-
|
|
141
|
+
.applyIf(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null'))))
|
|
142
|
+
.applyIf(inheritedSchemas.length > 0, (builder) => builder
|
|
143
|
+
.append(' : ')
|
|
144
|
+
.forEachSeparated(inheritedSchemas, ', ', (builder, schema) => builder.append(toCasing(schema.name, 'pascal'))) // TODO: Calling generateType here will lead to endless loop
|
|
145
|
+
)
|
|
146
|
+
.append(' ')
|
|
147
|
+
.parenthesizeMultilineIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false, '{}', (builder) => builder.applyIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false, (builder) => builder
|
|
97
148
|
.appendLine('@JsonIgnore')
|
|
98
149
|
.addImport('JsonIgnore', 'com.fasterxml.jackson.annotation')
|
|
99
150
|
.append('val additionalProperties: Mutable')
|
|
@@ -105,7 +156,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
105
156
|
.append('fun set')
|
|
106
157
|
.parenthesize('()', (builder) => builder.append('name: String, value: ').applyIfElse(schema.additionalProperties === true, (builder) => builder.append('Any?'), (builder) => this.generateType(ctx, builder, schema.additionalProperties)))
|
|
107
158
|
.append(' ')
|
|
108
|
-
.
|
|
159
|
+
.parenthesizeMultiline('{}', 'this.additionalProperties[name] = value')
|
|
109
160
|
.appendLine()
|
|
110
161
|
.appendLine()
|
|
111
162
|
.appendLine('@JsonAnyGetter')
|
|
@@ -113,10 +164,9 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
113
164
|
.append('fun getMap(): ')
|
|
114
165
|
.apply((builder) => this.generateMapType(ctx, builder, schema))
|
|
115
166
|
.append(' ')
|
|
116
|
-
.
|
|
117
|
-
.appendLine()));
|
|
167
|
+
.parenthesizeMultiline('{}', 'return this.additionalProperties')));
|
|
118
168
|
}
|
|
119
|
-
|
|
169
|
+
generateObjectDataClassParameterAnnotations(ctx, builder, schema, property) {
|
|
120
170
|
this.generatePropertyValidationAnnotations(ctx, builder, schema, property);
|
|
121
171
|
this.generatePropertySchemaAnnotation(ctx, builder, schema, property);
|
|
122
172
|
this.generateJsonPropertyAnnotation(ctx, builder, schema, property);
|
|
@@ -151,9 +201,9 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
151
201
|
.parenthesizeIf(parts.size > 0, '()', (builder) => builder.forEachSeparated(parts.entries(), ', ', (builder, [key, value]) => builder.append(`${key} = ${value}`)))
|
|
152
202
|
.appendLine();
|
|
153
203
|
}
|
|
154
|
-
generateJsonPropertyAnnotation(ctx, builder, schema, property) {
|
|
204
|
+
generateJsonPropertyAnnotation(ctx, builder, schema, property, scope) {
|
|
155
205
|
builder
|
|
156
|
-
.append('
|
|
206
|
+
.append(`@${scope ? scope + ':' : ''}JsonProperty`)
|
|
157
207
|
.addImport('JsonProperty', 'com.fasterxml.jackson.annotation')
|
|
158
208
|
.parenthesize('()', (builder) => builder
|
|
159
209
|
.append(this.toStringLiteral(ctx, property.name))
|
|
@@ -162,7 +212,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
162
212
|
.applyIf(property.schema.custom['exclude-when-null'] === true, (builder) => builder
|
|
163
213
|
.append('@get:JsonInclude')
|
|
164
214
|
.addImport('JsonInclude', 'com.fasterxml.jackson.annotation')
|
|
165
|
-
.parenthesize('()',
|
|
215
|
+
.parenthesize('()', 'JsonInclude.Include.NON_NULL')
|
|
166
216
|
.appendLine());
|
|
167
217
|
}
|
|
168
218
|
generateMapType(ctx, builder, schema) {
|
|
@@ -269,18 +319,15 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
269
319
|
.append('enum class ')
|
|
270
320
|
.append(this.getDeclarationTypeName(ctx))
|
|
271
321
|
.append('(val value: String) ')
|
|
272
|
-
.
|
|
322
|
+
.parenthesizeMultiline('{}', (builder) => {
|
|
273
323
|
var _a;
|
|
274
|
-
return builder
|
|
275
|
-
.appendLine()
|
|
276
|
-
.forEachSeparated((_a = schema.enum) !== null && _a !== void 0 ? _a : [], (builder) => builder.appendLine(',').appendLine(), (builder, value) => builder
|
|
324
|
+
return builder.forEachSeparated((_a = schema.enum) !== null && _a !== void 0 ? _a : [], (builder) => builder.appendLine(',').appendLine(), (builder, value) => builder
|
|
277
325
|
.append('@JsonProperty')
|
|
278
326
|
.addImport('JsonProperty', 'com.fasterxml.jackson.annotation')
|
|
279
|
-
.parenthesize('()',
|
|
327
|
+
.parenthesize('()', this.toStringLiteral(ctx, String(value)))
|
|
280
328
|
.appendLine()
|
|
281
329
|
.append(toCasing(String(value), 'snake'))
|
|
282
|
-
.parenthesize('()',
|
|
283
|
-
.appendLine();
|
|
330
|
+
.parenthesize('()', this.toStringLiteral(ctx, String(value))));
|
|
284
331
|
});
|
|
285
332
|
}
|
|
286
333
|
generateArrayType(ctx, builder, schema) {
|
|
@@ -330,4 +377,9 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
330
377
|
return bRequired - aRequired;
|
|
331
378
|
});
|
|
332
379
|
}
|
|
380
|
+
hasProperty(ctx, schema, name) {
|
|
381
|
+
return (('properties' in schema && schema.properties.has(name)) ||
|
|
382
|
+
('anyOf' in schema && schema.anyOf.some((x) => this.hasProperty(ctx, x, name))) ||
|
|
383
|
+
('allOf' in schema && schema.allOf.some((x) => this.hasProperty(ctx, x, name))));
|
|
384
|
+
}
|
|
333
385
|
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { defaultKotlinGeneratorConfig } from '../../../config';
|
|
2
|
+
export const defaultKotlinOkHttp3ClientsGeneratorConfig = Object.assign(Object.assign({}, defaultKotlinGeneratorConfig), { packageName: 'com.openapi.generated', packageSuffix: '.api.client', infrastructurePackageName: { mode: 'append-full-package-name', value: '.infrastructure' } });
|