@goast/kotlin 0.1.18 → 0.2.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/index.js +3 -0
- package/cjs/lib/ast/_index.js +81 -0
- package/cjs/lib/ast/common.js +2 -0
- package/cjs/lib/ast/index.js +5 -0
- package/cjs/lib/ast/node.js +10 -0
- package/cjs/lib/ast/nodes/annotation.js +39 -0
- package/cjs/lib/ast/nodes/argument.js +32 -0
- package/cjs/lib/ast/nodes/call.js +37 -0
- package/cjs/lib/ast/nodes/class.js +94 -0
- package/cjs/lib/ast/nodes/collection-literal.js +24 -0
- package/cjs/lib/ast/nodes/constructor.js +75 -0
- package/cjs/lib/ast/nodes/doc-tag.js +60 -0
- package/cjs/lib/ast/nodes/doc.js +55 -0
- package/cjs/lib/ast/nodes/enum-value.js +50 -0
- package/cjs/lib/ast/nodes/enum.js +83 -0
- package/cjs/lib/ast/nodes/function.js +105 -0
- package/cjs/lib/ast/nodes/generic-parameter.js +29 -0
- package/cjs/lib/ast/nodes/init-block.js +19 -0
- package/cjs/lib/ast/nodes/interface.js +68 -0
- package/cjs/lib/ast/nodes/lambda-type.js +44 -0
- package/cjs/lib/ast/nodes/lambda.js +36 -0
- package/cjs/lib/ast/nodes/object.js +58 -0
- package/cjs/lib/ast/nodes/parameter.js +57 -0
- package/cjs/lib/ast/nodes/property.js +142 -0
- package/cjs/lib/ast/nodes/reference.js +79 -0
- package/cjs/lib/ast/nodes/string.js +87 -0
- package/cjs/lib/ast/nodes/types.js +2 -0
- package/cjs/lib/ast/references/index.js +12 -0
- package/cjs/lib/ast/references/jackson.js +12 -0
- package/cjs/lib/ast/references/jakarta.js +10 -0
- package/cjs/lib/ast/references/java.js +14 -0
- package/cjs/lib/ast/references/kotlin.js +27 -0
- package/cjs/lib/ast/references/okhttp3.js +7 -0
- package/cjs/lib/ast/references/reactor.js +6 -0
- package/cjs/lib/ast/references/spring.js +21 -0
- package/cjs/lib/ast/references/swagger.js +11 -0
- package/cjs/lib/ast/utils/get-kotlin-builder-options.js +19 -0
- package/cjs/lib/ast/utils/to-kt-node.js +22 -0
- package/cjs/lib/ast/utils/write-kt-annotations.js +13 -0
- package/cjs/lib/ast/utils/write-kt-arguments.js +33 -0
- package/cjs/lib/ast/utils/write-kt-enum-values.js +17 -0
- package/cjs/lib/ast/utils/write-kt-generic-parameters.js +12 -0
- package/cjs/lib/ast/utils/write-kt-members.js +16 -0
- package/cjs/lib/ast/utils/write-kt-node.js +24 -0
- package/cjs/lib/ast/utils/write-kt-parameters.js +13 -0
- package/cjs/lib/config.js +15 -1
- package/cjs/lib/file-builder.js +17 -2
- package/cjs/lib/generators/models/args.js +2 -0
- package/cjs/lib/generators/models/index.js +2 -0
- package/cjs/lib/generators/models/model-generator.js +277 -322
- package/cjs/lib/generators/services/okhttp3-clients/args.js +2 -0
- package/cjs/lib/generators/services/okhttp3-clients/index.js +2 -0
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +240 -348
- package/cjs/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +4 -1
- package/cjs/lib/generators/services/okhttp3-clients/refs.js +34 -0
- package/cjs/lib/generators/services/spring-controllers/args.js +2 -0
- package/cjs/lib/generators/services/spring-controllers/index.js +2 -0
- package/cjs/lib/generators/services/spring-controllers/spring-controller-generator.js +270 -346
- package/cjs/lib/import-collection.js +24 -15
- package/esm/index.js +3 -0
- package/esm/lib/ast/_index.js +27 -0
- package/esm/lib/ast/common.js +1 -0
- package/esm/lib/ast/index.js +2 -0
- package/esm/lib/ast/node.js +6 -0
- package/esm/lib/ast/nodes/annotation.js +35 -0
- package/esm/lib/ast/nodes/argument.js +28 -0
- package/esm/lib/ast/nodes/call.js +33 -0
- package/esm/lib/ast/nodes/class.js +90 -0
- package/esm/lib/ast/nodes/collection-literal.js +20 -0
- package/esm/lib/ast/nodes/constructor.js +71 -0
- package/esm/lib/ast/nodes/doc-tag.js +56 -0
- package/esm/lib/ast/nodes/doc.js +51 -0
- package/esm/lib/ast/nodes/enum-value.js +46 -0
- package/esm/lib/ast/nodes/enum.js +79 -0
- package/esm/lib/ast/nodes/function.js +101 -0
- package/esm/lib/ast/nodes/generic-parameter.js +25 -0
- package/esm/lib/ast/nodes/init-block.js +15 -0
- package/esm/lib/ast/nodes/interface.js +64 -0
- package/esm/lib/ast/nodes/lambda-type.js +40 -0
- package/esm/lib/ast/nodes/lambda.js +32 -0
- package/esm/lib/ast/nodes/object.js +54 -0
- package/esm/lib/ast/nodes/parameter.js +53 -0
- package/esm/lib/ast/nodes/property.js +135 -0
- package/esm/lib/ast/nodes/reference.js +75 -0
- package/esm/lib/ast/nodes/string.js +83 -0
- package/esm/lib/ast/nodes/types.js +1 -0
- package/esm/lib/ast/references/index.js +15 -0
- package/esm/lib/ast/references/jackson.js +9 -0
- package/esm/lib/ast/references/jakarta.js +7 -0
- package/esm/lib/ast/references/java.js +11 -0
- package/esm/lib/ast/references/kotlin.js +24 -0
- package/esm/lib/ast/references/okhttp3.js +4 -0
- package/esm/lib/ast/references/reactor.js +3 -0
- package/esm/lib/ast/references/spring.js +18 -0
- package/esm/lib/ast/references/swagger.js +8 -0
- package/esm/lib/ast/utils/get-kotlin-builder-options.js +15 -0
- package/esm/lib/ast/utils/to-kt-node.js +18 -0
- package/esm/lib/ast/utils/write-kt-annotations.js +9 -0
- package/esm/lib/ast/utils/write-kt-arguments.js +29 -0
- package/esm/lib/ast/utils/write-kt-enum-values.js +13 -0
- package/esm/lib/ast/utils/write-kt-generic-parameters.js +8 -0
- package/esm/lib/ast/utils/write-kt-members.js +12 -0
- package/esm/lib/ast/utils/write-kt-node.js +19 -0
- package/esm/lib/ast/utils/write-kt-parameters.js +9 -0
- package/esm/lib/config.js +15 -1
- package/esm/lib/file-builder.js +16 -2
- package/esm/lib/generators/models/args.js +1 -0
- package/esm/lib/generators/models/index.js +2 -0
- package/esm/lib/generators/models/model-generator.js +278 -323
- package/esm/lib/generators/services/okhttp3-clients/args.js +1 -0
- package/esm/lib/generators/services/okhttp3-clients/index.js +2 -0
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-client-generator.js +242 -350
- package/esm/lib/generators/services/okhttp3-clients/okhttp3-clients-generator.js +4 -1
- package/esm/lib/generators/services/okhttp3-clients/refs.js +30 -0
- package/esm/lib/generators/services/spring-controllers/args.js +1 -0
- package/esm/lib/generators/services/spring-controllers/index.js +2 -0
- package/esm/lib/generators/services/spring-controllers/spring-controller-generator.js +269 -346
- package/esm/lib/import-collection.js +23 -14
- package/package.json +6 -6
- package/types/index.d.ts +2 -0
- package/types/lib/ast/_index.d.ts +28 -0
- package/types/lib/ast/common.d.ts +1 -0
- package/types/lib/ast/index.d.ts +1 -0
- package/types/lib/ast/node.d.ts +4 -0
- package/types/lib/ast/nodes/annotation.d.ts +23 -0
- package/types/lib/ast/nodes/argument.d.ts +22 -0
- package/types/lib/ast/nodes/call.d.ts +22 -0
- package/types/lib/ast/nodes/class.d.ts +54 -0
- package/types/lib/ast/nodes/collection-literal.d.ts +17 -0
- package/types/lib/ast/nodes/constructor.d.ts +34 -0
- package/types/lib/ast/nodes/doc-tag.d.ts +34 -0
- package/types/lib/ast/nodes/doc.d.ts +26 -0
- package/types/lib/ast/nodes/enum-value.d.ts +30 -0
- package/types/lib/ast/nodes/enum.d.ts +45 -0
- package/types/lib/ast/nodes/function.d.ts +58 -0
- package/types/lib/ast/nodes/generic-parameter.d.ts +21 -0
- package/types/lib/ast/nodes/init-block.d.ts +16 -0
- package/types/lib/ast/nodes/interface.d.ts +41 -0
- package/types/lib/ast/nodes/lambda-type.d.ts +21 -0
- package/types/lib/ast/nodes/lambda.d.ts +20 -0
- package/types/lib/ast/nodes/object.d.ts +34 -0
- package/types/lib/ast/nodes/parameter.d.ts +48 -0
- package/types/lib/ast/nodes/property.d.ts +87 -0
- package/types/lib/ast/nodes/reference.d.ts +63 -0
- package/types/lib/ast/nodes/string.d.ts +30 -0
- package/types/lib/ast/nodes/types.d.ts +10 -0
- package/types/lib/ast/references/index.d.ts +8 -0
- package/types/lib/ast/references/jackson.d.ts +77 -0
- package/types/lib/ast/references/jakarta.d.ts +33 -0
- package/types/lib/ast/references/java.d.ts +71 -0
- package/types/lib/ast/references/kotlin.d.ts +265 -0
- package/types/lib/ast/references/okhttp3.d.ts +22 -0
- package/types/lib/ast/references/reactor.d.ts +16 -0
- package/types/lib/ast/references/spring.d.ts +126 -0
- package/types/lib/ast/references/swagger.d.ts +44 -0
- package/types/lib/ast/utils/get-kotlin-builder-options.d.ts +3 -0
- package/types/lib/ast/utils/to-kt-node.d.ts +7 -0
- package/types/lib/ast/utils/write-kt-annotations.d.ts +5 -0
- package/types/lib/ast/utils/write-kt-arguments.d.ts +3 -0
- package/types/lib/ast/utils/write-kt-enum-values.d.ts +3 -0
- package/types/lib/ast/utils/write-kt-generic-parameters.d.ts +3 -0
- package/types/lib/ast/utils/write-kt-members.d.ts +5 -0
- package/types/lib/ast/utils/write-kt-node.d.ts +7 -0
- package/types/lib/ast/utils/write-kt-parameters.d.ts +3 -0
- package/types/lib/config.d.ts +4 -0
- package/types/lib/file-builder.d.ts +10 -3
- package/types/lib/generators/models/args.d.ts +99 -0
- package/types/lib/generators/models/index.d.ts +1 -0
- package/types/lib/generators/models/model-generator.d.ts +35 -32
- package/types/lib/generators/models/models.d.ts +4 -4
- package/types/lib/generators/services/okhttp3-clients/args.d.ts +66 -0
- package/types/lib/generators/services/okhttp3-clients/index.d.ts +1 -0
- package/types/lib/generators/services/okhttp3-clients/models.d.ts +2 -0
- package/types/lib/generators/services/okhttp3-clients/okhttp3-client-generator.d.ts +25 -47
- package/types/lib/generators/services/okhttp3-clients/refs.d.ts +235 -0
- package/types/lib/generators/services/spring-controllers/args.d.ts +70 -0
- package/types/lib/generators/services/spring-controllers/index.d.ts +1 -0
- package/types/lib/generators/services/spring-controllers/spring-controller-generator.d.ts +37 -51
- package/types/lib/import-collection.d.ts +7 -1
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultKotlinModelGenerator = void 0;
|
|
4
|
+
/* eslint-disable unused-imports/no-unused-vars */
|
|
4
5
|
const path_1 = require("path");
|
|
5
6
|
const fs_extra_1 = require("fs-extra");
|
|
6
7
|
const core_1 = require("@goast/core");
|
|
8
|
+
const ast_1 = require("../../ast");
|
|
7
9
|
const file_builder_1 = require("../../file-builder");
|
|
8
10
|
const file_generator_1 = require("../file-generator");
|
|
9
11
|
class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
10
12
|
generate(ctx) {
|
|
11
13
|
if (/\/(anyOf|allOf)(\/[0-9]+)?$/.test(ctx.schema.$src.path)) {
|
|
12
14
|
// Do not generate types that are only used for anyOf and/or allOf
|
|
13
|
-
return {
|
|
15
|
+
return { type: ast_1.kt.refs.any({ nullable: true }) };
|
|
14
16
|
}
|
|
15
17
|
if (ctx.schema.id === ctx.schema.name) {
|
|
16
18
|
// TODO: Add this to @goast/core
|
|
@@ -30,356 +32,306 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
|
-
if (this.shouldGenerateTypeDeclaration(ctx, ctx.schema)) {
|
|
34
|
-
const typeName = this.getDeclarationTypeName(ctx, ctx.schema);
|
|
35
|
-
const packageName = this.getPackageName(ctx, ctx.schema);
|
|
35
|
+
if (this.shouldGenerateTypeDeclaration(ctx, { schema: ctx.schema })) {
|
|
36
|
+
const typeName = this.getDeclarationTypeName(ctx, { schema: ctx.schema });
|
|
37
|
+
const packageName = this.getPackageName(ctx, { schema: ctx.schema });
|
|
36
38
|
const filePath = `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}/${typeName}.kt`;
|
|
37
39
|
console.log(`Generating model ${packageName}.${typeName} to ${filePath}...`);
|
|
38
40
|
(0, fs_extra_1.ensureDirSync)((0, path_1.dirname)(filePath));
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
(0, fs_extra_1.writeFileSync)(filePath, builder.toString());
|
|
42
|
-
return { typeName, packageName, additionalImports: [] };
|
|
41
|
+
(0, fs_extra_1.writeFileSync)(filePath, new file_builder_1.KotlinFileBuilder(packageName, ctx.config).append(this.getFileContent(ctx, {})).toString());
|
|
42
|
+
return { type: ast_1.kt.reference(typeName, packageName) };
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
|
-
|
|
46
|
-
this.generateType(ctx, builder, ctx.schema);
|
|
47
|
-
const additionalImports = builder.imports.imports;
|
|
48
|
-
builder.imports.clear();
|
|
49
|
-
return { typeName: builder.toString(false), packageName: undefined, additionalImports };
|
|
45
|
+
return { type: this.getType(ctx, { schema: ctx.schema }) };
|
|
50
46
|
}
|
|
51
47
|
}
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
getFileContent(ctx, args) {
|
|
49
|
+
return (0, core_1.appendValueGroup)([this.getSchemaDeclaration(ctx, { schema: this.normalizeSchema(ctx, { schema: ctx.schema }) })], '\n\n');
|
|
50
|
+
}
|
|
51
|
+
getSchemaDeclaration(ctx, args) {
|
|
52
|
+
const { schema } = args;
|
|
54
53
|
if (schema.kind === 'object') {
|
|
55
|
-
this.
|
|
54
|
+
return schema.discriminator ? this.getInterface(ctx, { schema }) : this.getClass(ctx, { schema });
|
|
56
55
|
}
|
|
57
56
|
else if (schema.enum !== undefined && schema.enum.length > 0) {
|
|
58
|
-
this.
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
this.generateType(ctx, builder, schema);
|
|
57
|
+
return this.getEnum(ctx, { schema });
|
|
62
58
|
}
|
|
59
|
+
return '// The generator was not able to generate this schema.\n// This should not happend. If you see this comment, please open an Issue on Github.';
|
|
63
60
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
61
|
+
getClass(ctx, args) {
|
|
62
|
+
var _a;
|
|
63
|
+
const { schema } = args;
|
|
64
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, { schema });
|
|
65
|
+
const parameters = this.getClassProperties(ctx, { schema });
|
|
66
|
+
return ast_1.kt.class(this.getDeclarationTypeName(ctx, { schema }), {
|
|
67
|
+
doc: ast_1.kt.doc((_a = schema.description) === null || _a === void 0 ? void 0 : _a.trim()),
|
|
68
|
+
classKind: parameters.length === 0 ? null : 'data',
|
|
69
|
+
implements: inheritedSchemas.map((schema) => this.getType(ctx, { schema })),
|
|
70
|
+
primaryConstructor: ast_1.kt.constructor(parameters.map((property) => this.getClassParameter(ctx, Object.assign(Object.assign({}, args), { inheritedSchemas, parameters, property })))),
|
|
71
|
+
members: [
|
|
72
|
+
...(schema.additionalProperties !== undefined && schema.additionalProperties !== false
|
|
73
|
+
? [
|
|
74
|
+
this.getAdditionalPropertiesProperty(ctx, { schema }),
|
|
75
|
+
this.getAdditionalPropertiesSetter(ctx, { schema }),
|
|
76
|
+
this.getAdditionalPropertiesGetter(ctx, { schema }),
|
|
77
|
+
]
|
|
78
|
+
: []),
|
|
79
|
+
],
|
|
80
|
+
});
|
|
77
81
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
getInterface(ctx, args) {
|
|
83
|
+
var _a;
|
|
84
|
+
const { schema } = args;
|
|
85
|
+
return ast_1.kt.interface(this.getDeclarationTypeName(ctx, { schema }), {
|
|
86
|
+
doc: ast_1.kt.doc((_a = schema.description) === null || _a === void 0 ? void 0 : _a.trim()),
|
|
87
|
+
annotations: [
|
|
88
|
+
this.getJacksonJsonTypeInfoAnnotation(ctx, { schema }),
|
|
89
|
+
this.getJacksonJsonSubTypesAnnotation(ctx, { schema }),
|
|
90
|
+
].filter(core_1.notNullish),
|
|
91
|
+
members: this.sortProperties(ctx, { schema, properties: schema.properties.values() }).map((property) => this.getInterfaceProperty(ctx, { schema, property })),
|
|
92
|
+
});
|
|
85
93
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
getEnum(ctx, args) {
|
|
95
|
+
var _a, _b, _c;
|
|
96
|
+
const { schema } = args;
|
|
97
|
+
return ast_1.kt.enum(this.getDeclarationTypeName(ctx, { schema }), (_b = (_a = schema.enum) === null || _a === void 0 ? void 0 : _a.map((x) => ast_1.kt.enumValue((0, core_1.toCasing)(String(x), ctx.config.enumValueNameCasing), {
|
|
98
|
+
annotations: [ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonProperty(), [ast_1.kt.argument(ast_1.kt.string(String(x)))])],
|
|
99
|
+
arguments: [ast_1.kt.argument(ast_1.kt.string(String(x)))],
|
|
100
|
+
}))) !== null && _b !== void 0 ? _b : [], {
|
|
101
|
+
doc: ast_1.kt.doc((_c = schema.description) === null || _c === void 0 ? void 0 : _c.trim()),
|
|
102
|
+
primaryConstructor: ast_1.kt.constructor([
|
|
103
|
+
ast_1.kt.parameter.class((0, core_1.toCasing)('value', ctx.config.propertyNameCasing), ast_1.kt.refs.string(), {
|
|
104
|
+
property: 'readonly',
|
|
105
|
+
}),
|
|
106
|
+
]),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
getType(ctx, args) {
|
|
110
|
+
var _a;
|
|
111
|
+
const { schema } = args;
|
|
112
|
+
const generatedType = this.getGeneratedType(ctx, { schema, nullable: args.nullable });
|
|
113
|
+
if (generatedType) {
|
|
114
|
+
return generatedType;
|
|
89
115
|
}
|
|
90
|
-
|
|
91
|
-
|
|
116
|
+
const nullable = (_a = args.nullable) !== null && _a !== void 0 ? _a : schema.nullable;
|
|
117
|
+
switch (schema.kind) {
|
|
118
|
+
case 'boolean':
|
|
119
|
+
return ast_1.kt.refs.boolean({ nullable });
|
|
120
|
+
case 'integer':
|
|
121
|
+
case 'number':
|
|
122
|
+
switch (schema.format) {
|
|
123
|
+
case 'int32':
|
|
124
|
+
return ast_1.kt.refs.int({ nullable });
|
|
125
|
+
case 'int64':
|
|
126
|
+
return ast_1.kt.refs.long({ nullable });
|
|
127
|
+
case 'float':
|
|
128
|
+
return ast_1.kt.refs.float({ nullable });
|
|
129
|
+
case 'double':
|
|
130
|
+
return ast_1.kt.refs.double({ nullable });
|
|
131
|
+
default:
|
|
132
|
+
return schema.kind === 'integer' ? ast_1.kt.refs.int({ nullable }) : ast_1.kt.refs.double({ nullable });
|
|
133
|
+
}
|
|
134
|
+
case 'string':
|
|
135
|
+
switch (schema.format) {
|
|
136
|
+
case 'date-time':
|
|
137
|
+
return ast_1.kt.refs.java.offsetDateTime({ nullable });
|
|
138
|
+
default:
|
|
139
|
+
return ast_1.kt.refs.string({ nullable });
|
|
140
|
+
}
|
|
141
|
+
case 'null':
|
|
142
|
+
return ast_1.kt.refs.nothing({ nullable });
|
|
143
|
+
case 'unknown':
|
|
144
|
+
return ast_1.kt.refs.any({ nullable });
|
|
145
|
+
case 'array':
|
|
146
|
+
return ast_1.kt.refs.list([schema.items ? this.getType(ctx, { schema: schema.items }) : ast_1.kt.refs.any({ nullable: true })], { nullable });
|
|
147
|
+
case 'object':
|
|
148
|
+
return schema.properties.size === 0 && schema.additionalProperties
|
|
149
|
+
? ast_1.kt.refs.map([ast_1.kt.refs.string(), this.getAdditionalPropertiesType(ctx, { schema })], { nullable })
|
|
150
|
+
: ast_1.kt.refs.any({ nullable });
|
|
151
|
+
default:
|
|
152
|
+
return ast_1.kt.refs.any({ nullable });
|
|
92
153
|
}
|
|
93
154
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
.append(' ')
|
|
102
|
-
.parenthesize('{}', (builder) => this.generateObjectInterfaceMembers(ctx, builder, schema), { multiline: true });
|
|
103
|
-
}
|
|
104
|
-
generateObjectInterfaceAnnotations(ctx, builder, schema) {
|
|
105
|
-
if (schema.discriminator && ctx.config.addJacksonAnnotations) {
|
|
106
|
-
builder.appendAnnotation('JsonTypeInfo', 'com.fasterxml.jackson.annotation', [
|
|
107
|
-
['use', 'JsonTypeInfo.Id.NAME'],
|
|
108
|
-
['include', 'JsonTypeInfo.As.EXISTING_PROPERTY'],
|
|
109
|
-
['property', this.toStringLiteral(ctx, schema.discriminator.propertyName)],
|
|
110
|
-
['visible', 'true'],
|
|
111
|
-
]);
|
|
112
|
-
const entries = Object.entries(schema.discriminator.mapping);
|
|
113
|
-
if (entries.length > 0) {
|
|
114
|
-
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)})`)));
|
|
115
|
-
}
|
|
155
|
+
getGeneratedType(ctx, args) {
|
|
156
|
+
var _a;
|
|
157
|
+
const schema = (0, core_1.getSchemaReference)(args.schema, ['description']);
|
|
158
|
+
if (this.shouldGenerateTypeDeclaration(ctx, { schema })) {
|
|
159
|
+
return ast_1.kt.reference(this.getDeclarationTypeName(ctx, { schema }), this.getPackageName(ctx, { schema }), {
|
|
160
|
+
nullable: (_a = args.nullable) !== null && _a !== void 0 ? _a : schema.nullable,
|
|
161
|
+
});
|
|
116
162
|
}
|
|
163
|
+
return null;
|
|
117
164
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
.ensurePreviousLineEmpty()
|
|
124
|
-
.append((builder) => this.generateJsonPropertyAnnotation(ctx, builder, schema, property, 'get'))
|
|
125
|
-
.ensureCurrentLineEmpty()
|
|
126
|
-
.append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
|
|
127
|
-
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
128
|
-
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?')));
|
|
129
|
-
}
|
|
130
|
-
generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property) {
|
|
131
|
-
builder
|
|
132
|
-
.ensurePreviousLineEmpty()
|
|
133
|
-
.append((builder) => this.generateObjectDataClassParameterAnnotations(ctx, builder, schema, property))
|
|
134
|
-
.appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
|
|
135
|
-
.append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
|
|
136
|
-
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
137
|
-
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?'))
|
|
138
|
-
.appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema));
|
|
165
|
+
getAdditionalPropertiesType(ctx, args) {
|
|
166
|
+
const { schema } = args;
|
|
167
|
+
return typeof schema.additionalProperties === 'object'
|
|
168
|
+
? this.getType(ctx, { schema: schema.additionalProperties })
|
|
169
|
+
: ast_1.kt.refs.any({ nullable: true });
|
|
139
170
|
}
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
const params = this.getClassProperties(ctx, schema);
|
|
143
|
-
builder
|
|
144
|
-
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
145
|
-
.append(params.length === 0 ? 'class' : 'data class', ' ')
|
|
146
|
-
.append(this.getDeclarationTypeName(ctx, schema))
|
|
147
|
-
.parenthesizeIf(params.length > 0, '()', (builder) => builder.forEach(params, (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true })
|
|
148
|
-
.if(inheritedSchemas.length > 0, (builder) => builder
|
|
149
|
-
.append(' : ')
|
|
150
|
-
.forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
|
|
151
|
-
separator: ', ',
|
|
152
|
-
}))
|
|
153
|
-
.append(' ')
|
|
154
|
-
.parenthesizeIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false, '{}', (builder) => builder.if(schema.additionalProperties !== undefined && schema.additionalProperties !== false, (builder) => builder
|
|
155
|
-
.if(ctx.config.addJacksonAnnotations, (builder) => builder.appendLine('@JsonIgnore').addImport('JsonIgnore', 'com.fasterxml.jackson.annotation'))
|
|
156
|
-
.append('val additionalProperties: Mutable')
|
|
157
|
-
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
158
|
-
.appendLine(' = mutableMapOf()')
|
|
159
|
-
.appendLine()
|
|
160
|
-
.if(ctx.config.addJacksonAnnotations, (builder) => builder.appendLine('@JsonAnySetter').addImport('JsonAnySetter', 'com.fasterxml.jackson.annotation'))
|
|
161
|
-
.append('fun set')
|
|
162
|
-
.parenthesize('()', (builder) => builder.append('name: String, value: ').if(schema.additionalProperties === true, (builder) => builder.append('Any?'), (builder) => this.generateTypeUsage(ctx, builder, schema.additionalProperties)))
|
|
163
|
-
.append(' ')
|
|
164
|
-
.parenthesize('{}', 'this.additionalProperties[name] = value', { multiline: true })
|
|
165
|
-
.appendLine()
|
|
166
|
-
.appendLine()
|
|
167
|
-
.if(ctx.config.addJacksonAnnotations, (builder) => builder.appendLine('@JsonAnyGetter').addImport('JsonAnyGetter', 'com.fasterxml.jackson.annotation'))
|
|
168
|
-
.append('fun getMap(): ')
|
|
169
|
-
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
170
|
-
.append(' ')
|
|
171
|
-
.parenthesize('{}', 'return this.additionalProperties', { multiline: true })), { multiline: true });
|
|
172
|
-
}
|
|
173
|
-
generateDefaultValue(ctx, builder, schema) {
|
|
171
|
+
getDefaultValue(ctx, args) {
|
|
172
|
+
const { schema } = args;
|
|
174
173
|
if (schema.default === null || schema.default === undefined) {
|
|
175
|
-
|
|
174
|
+
return 'null';
|
|
176
175
|
}
|
|
177
176
|
else {
|
|
178
177
|
switch (schema.kind) {
|
|
179
178
|
case 'boolean':
|
|
180
|
-
|
|
181
|
-
break;
|
|
179
|
+
return Boolean(schema.default) || String(schema.default).toLowerCase() === 'true' ? 'true' : 'false';
|
|
182
180
|
case 'integer':
|
|
183
181
|
case 'number':
|
|
184
|
-
|
|
185
|
-
break;
|
|
182
|
+
return String(schema.default);
|
|
186
183
|
case 'string':
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
.append('.')
|
|
191
|
-
.append((0, core_1.toCasing)(String(schema.default), ctx.config.enumValueNameCasing));
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
builder.append(this.toStringLiteral(ctx, String(schema.default)));
|
|
195
|
-
}
|
|
196
|
-
break;
|
|
184
|
+
return schema.enum && schema.enum.length > 0
|
|
185
|
+
? ast_1.kt.call([this.getType(ctx, { schema }), (0, core_1.toCasing)(String(schema.default), ctx.config.enumValueNameCasing)])
|
|
186
|
+
: ast_1.kt.string(String(schema.default));
|
|
197
187
|
default:
|
|
198
|
-
|
|
199
|
-
break;
|
|
188
|
+
return 'null';
|
|
200
189
|
}
|
|
201
190
|
}
|
|
202
191
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
192
|
+
// #region Members
|
|
193
|
+
getClassParameter(ctx, args) {
|
|
194
|
+
var _a;
|
|
195
|
+
const { schema, inheritedSchemas, property } = args;
|
|
196
|
+
return ast_1.kt.parameter.class((0, core_1.toCasing)(property.name, ctx.config.propertyNameCasing), this.getType(ctx, { schema: property.schema, nullable: schema.required.has(property.name) ? undefined : true }), {
|
|
197
|
+
description: (_a = property.schema.description) === null || _a === void 0 ? void 0 : _a.trim(),
|
|
198
|
+
annotations: [
|
|
199
|
+
this.getJakartaPatternAnnotation(ctx, { schema, property }),
|
|
200
|
+
this.getJakartaValidAnnotation(ctx, { schema, property }),
|
|
201
|
+
this.getSwaggerSchemaAnnotation(ctx, { schema, property }),
|
|
202
|
+
this.getJacksonJsonPropertyAnnotation(ctx, { schema, property }),
|
|
203
|
+
(0, core_1.modify)(this.getJacksonJsonIncludeAnnotation(ctx, { schema, property }), (x) => (x.target = 'get')),
|
|
204
|
+
].filter(core_1.notNullish),
|
|
205
|
+
override: inheritedSchemas.some((schema) => this.hasProperty(ctx, { schema, propertyName: property.name })),
|
|
206
|
+
property: 'readonly',
|
|
207
|
+
default: property.schema.default !== undefined || !schema.required.has(property.name)
|
|
208
|
+
? this.getDefaultValue(ctx, { schema: property.schema })
|
|
209
|
+
: null,
|
|
210
|
+
});
|
|
207
211
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
}
|
|
212
|
+
getInterfaceProperty(ctx, args) {
|
|
213
|
+
var _a;
|
|
214
|
+
const { schema, property } = args;
|
|
215
|
+
return ast_1.kt.property((0, core_1.toCasing)(property.name, ctx.config.propertyNameCasing), {
|
|
216
|
+
doc: ast_1.kt.doc((_a = property.schema.description) === null || _a === void 0 ? void 0 : _a.trim()),
|
|
217
|
+
annotations: (0, core_1.modifyEach)([
|
|
218
|
+
this.getJacksonJsonPropertyAnnotation(ctx, { schema, property }),
|
|
219
|
+
this.getJacksonJsonIncludeAnnotation(ctx, { schema, property }),
|
|
220
|
+
].filter(core_1.notNullish), (x) => (x.target = 'get')),
|
|
221
|
+
type: this.getType(ctx, {
|
|
222
|
+
schema: property.schema,
|
|
223
|
+
nullable: schema.required.has(property.name) ? undefined : true,
|
|
224
|
+
}),
|
|
225
|
+
});
|
|
222
226
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (property.schema.description !== undefined) {
|
|
233
|
-
parts.set('description', this.toStringLiteral(ctx, property.schema.description));
|
|
234
|
-
}
|
|
235
|
-
builder
|
|
236
|
-
.append('@Schema')
|
|
237
|
-
.addImport('Schema', 'io.swagger.v3.oas.annotations.media')
|
|
238
|
-
.parenthesizeIf(parts.size > 0, '()', (builder) => builder.forEach(parts.entries(), (builder, [key, value]) => builder.append(`${key} = ${value}`), {
|
|
239
|
-
separator: ', ',
|
|
240
|
-
}))
|
|
241
|
-
.appendLine();
|
|
242
|
-
}
|
|
227
|
+
getAdditionalPropertiesProperty(ctx, args) {
|
|
228
|
+
const { schema } = args;
|
|
229
|
+
return ast_1.kt.property((0, core_1.toCasing)('additionalProperties', ctx.config.propertyNameCasing), {
|
|
230
|
+
annotations: [ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonIgnore())],
|
|
231
|
+
type: ast_1.kt.reference('MutableMap', null, {
|
|
232
|
+
generics: ['String', this.getAdditionalPropertiesType(ctx, { schema })],
|
|
233
|
+
}),
|
|
234
|
+
default: 'mutableMapOf()',
|
|
235
|
+
});
|
|
243
236
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
.
|
|
253
|
-
|
|
254
|
-
.if(ctx.config.addJacksonAnnotations, (builder) => builder.append('@get:JsonInclude').addImport('JsonInclude', 'com.fasterxml.jackson.annotation'))
|
|
255
|
-
.parenthesize('()', 'JsonInclude.Include.NON_NULL')
|
|
256
|
-
.appendLine());
|
|
237
|
+
getAdditionalPropertiesSetter(ctx, args) {
|
|
238
|
+
const { schema } = args;
|
|
239
|
+
return ast_1.kt.function((0, core_1.toCasing)('set', ctx.config.functionNameCasing), {
|
|
240
|
+
annotations: [ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonAnySetter())],
|
|
241
|
+
parameters: [
|
|
242
|
+
ast_1.kt.parameter((0, core_1.toCasing)('name', ctx.config.parameterNameCasing), 'String'),
|
|
243
|
+
ast_1.kt.parameter((0, core_1.toCasing)('value', ctx.config.parameterNameCasing), this.getAdditionalPropertiesType(ctx, { schema })),
|
|
244
|
+
],
|
|
245
|
+
body: `this.${(0, core_1.toCasing)('additionalProperties', ctx.config.propertyNameCasing)}[name] = value`,
|
|
246
|
+
});
|
|
257
247
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
.append('Map')
|
|
266
|
-
.parenthesize('<>', (builder) => builder.append('String, ').append((builder) => this.generateTypeUsage(ctx, builder, propertiesType)));
|
|
267
|
-
}
|
|
248
|
+
getAdditionalPropertiesGetter(ctx, args) {
|
|
249
|
+
const { schema } = args;
|
|
250
|
+
return ast_1.kt.function((0, core_1.toCasing)('getMap', ctx.config.functionNameCasing), {
|
|
251
|
+
annotations: [ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonAnyGetter())],
|
|
252
|
+
returnType: ast_1.kt.refs.map([ast_1.kt.refs.string(), this.getAdditionalPropertiesType(ctx, { schema })]),
|
|
253
|
+
body: `return this.${(0, core_1.toCasing)('additionalProperties', ctx.config.propertyNameCasing)}`,
|
|
254
|
+
});
|
|
268
255
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
this.generateStringType(ctx, builder, schema);
|
|
284
|
-
}
|
|
285
|
-
break;
|
|
286
|
-
case 'null':
|
|
287
|
-
builder.append('Nothing');
|
|
288
|
-
break;
|
|
289
|
-
case 'unknown':
|
|
290
|
-
builder.append('Any');
|
|
291
|
-
break;
|
|
292
|
-
case 'array':
|
|
293
|
-
this.generateArrayType(ctx, builder, schema);
|
|
294
|
-
break;
|
|
295
|
-
case 'object':
|
|
296
|
-
this.generateObjectType(ctx, builder, schema);
|
|
297
|
-
break;
|
|
298
|
-
case 'combined':
|
|
299
|
-
builder.append('Any');
|
|
300
|
-
break;
|
|
301
|
-
case 'multi-type':
|
|
302
|
-
builder.append('Any');
|
|
303
|
-
break;
|
|
304
|
-
case 'oneOf':
|
|
305
|
-
builder.append('Any');
|
|
306
|
-
break;
|
|
307
|
-
default:
|
|
308
|
-
builder.append('Any');
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
if (schema.nullable) {
|
|
312
|
-
builder.append('?');
|
|
313
|
-
}
|
|
256
|
+
// #endregion
|
|
257
|
+
// #region Annotations
|
|
258
|
+
getJacksonJsonTypeInfoAnnotation(ctx, args) {
|
|
259
|
+
const { schema } = args;
|
|
260
|
+
return ctx.config.addJacksonAnnotations && schema.discriminator
|
|
261
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonTypeInfo(), [
|
|
262
|
+
ast_1.kt.argument.named('use', 'JsonTypeInfo.Id.NAME'),
|
|
263
|
+
ast_1.kt.argument.named('include', 'JsonTypeInfo.As.EXISTING_PROPERTY'),
|
|
264
|
+
ast_1.kt.argument.named('property', ast_1.kt.string(schema.discriminator.propertyName)),
|
|
265
|
+
ast_1.kt.argument.named('visible', 'true'),
|
|
266
|
+
])
|
|
267
|
+
: null;
|
|
314
268
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
case 'double':
|
|
327
|
-
builder.append('Double');
|
|
328
|
-
break;
|
|
329
|
-
default:
|
|
330
|
-
builder.append(schema.kind === 'integer' ? 'Int' : 'Double');
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
269
|
+
getJacksonJsonSubTypesAnnotation(ctx, args) {
|
|
270
|
+
const { schema } = args;
|
|
271
|
+
if (!ctx.config.addJacksonAnnotations || !schema.discriminator)
|
|
272
|
+
return null;
|
|
273
|
+
const entries = Object.entries(schema.discriminator.mapping);
|
|
274
|
+
return entries.length > 0
|
|
275
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonSubTypes(), entries.map(([value, schema]) => ast_1.kt.argument(ast_1.kt.call([ast_1.kt.refs.jackson.jsonSubTypes(), 'Type'], [
|
|
276
|
+
ast_1.kt.argument.named('value', (0, core_1.modify)(this.getType(ctx, { schema }), (x) => (x.classReference = true))),
|
|
277
|
+
ast_1.kt.argument.named('name', ast_1.kt.string(value)),
|
|
278
|
+
]))))
|
|
279
|
+
: null;
|
|
333
280
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
281
|
+
getJacksonJsonPropertyAnnotation(ctx, args) {
|
|
282
|
+
const { schema, property } = args;
|
|
283
|
+
return ctx.config.addJacksonAnnotations
|
|
284
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonProperty(), [
|
|
285
|
+
ast_1.kt.argument(ast_1.kt.string(property.name)),
|
|
286
|
+
schema.required.has(property.name) ? ast_1.kt.argument.named('required', 'true') : null,
|
|
287
|
+
])
|
|
288
|
+
: null;
|
|
343
289
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
var _a;
|
|
352
|
-
return builder.forEach((_a = schema.enum) !== null && _a !== void 0 ? _a : [], (builder, value) => builder
|
|
353
|
-
.if(ctx.config.addJacksonAnnotations, (builder) => builder
|
|
354
|
-
.append('@JsonProperty')
|
|
355
|
-
.addImport('JsonProperty', 'com.fasterxml.jackson.annotation')
|
|
356
|
-
.parenthesize('()', this.toStringLiteral(ctx, String(value)))
|
|
357
|
-
.appendLine())
|
|
358
|
-
.append((0, core_1.toCasing)(String(value), 'snake'))
|
|
359
|
-
.parenthesize('()', this.toStringLiteral(ctx, String(value))), { separator: (builder) => builder.appendLine(',').appendLine() });
|
|
360
|
-
}, { multiline: true });
|
|
290
|
+
getJacksonJsonIncludeAnnotation(ctx, args) {
|
|
291
|
+
const { property } = args;
|
|
292
|
+
return ctx.config.addJacksonAnnotations && property.schema.custom['exclude-when-null'] === true
|
|
293
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jackson.jsonInclude(), [
|
|
294
|
+
ast_1.kt.argument(ast_1.kt.call([ast_1.kt.refs.jackson.jsonInclude(), 'Include', 'NON_NULL'])),
|
|
295
|
+
])
|
|
296
|
+
: null;
|
|
361
297
|
}
|
|
362
|
-
|
|
363
|
-
|
|
298
|
+
getJakartaPatternAnnotation(ctx, args) {
|
|
299
|
+
const { property } = args;
|
|
300
|
+
return ctx.config.addJakartaValidationAnnotations && property.schema.kind === 'string' && property.schema.pattern
|
|
301
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jakarta.pattern(), [ast_1.kt.argument.named('regexp', ast_1.kt.string(property.schema.pattern))], {
|
|
302
|
+
target: 'get',
|
|
303
|
+
})
|
|
304
|
+
: null;
|
|
364
305
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
306
|
+
getJakartaValidAnnotation(ctx, args) {
|
|
307
|
+
const { property } = args;
|
|
308
|
+
return ctx.config.addJakartaValidationAnnotations &&
|
|
309
|
+
this.shouldGenerateTypeDeclaration(ctx, { schema: property.schema })
|
|
310
|
+
? ast_1.kt.annotation(ast_1.kt.refs.jakarta.valid(), [], { target: 'field' })
|
|
311
|
+
: null;
|
|
312
|
+
}
|
|
313
|
+
getSwaggerSchemaAnnotation(ctx, args) {
|
|
314
|
+
const { schema, property } = args;
|
|
315
|
+
return ctx.config.addSwaggerAnnotations
|
|
316
|
+
? ast_1.kt.annotation(ast_1.kt.refs.swagger.schema(), [
|
|
317
|
+
property.schema.example !== undefined
|
|
318
|
+
? ast_1.kt.argument.named('example', ast_1.kt.string(String(property.schema.example)))
|
|
319
|
+
: null,
|
|
320
|
+
schema.required.has(property.name) ? ast_1.kt.argument.named('required', 'true') : null,
|
|
321
|
+
property.schema.description !== undefined
|
|
322
|
+
? ast_1.kt.argument.named('description', ast_1.kt.string(property.schema.description))
|
|
323
|
+
: null,
|
|
324
|
+
])
|
|
325
|
+
: null;
|
|
377
326
|
}
|
|
378
|
-
|
|
327
|
+
// #endregion
|
|
328
|
+
getPackageName(ctx, args) {
|
|
329
|
+
const { schema } = args;
|
|
379
330
|
const packageSuffix = typeof ctx.config.packageSuffix === 'string' ? ctx.config.packageSuffix : ctx.config.packageSuffix(schema);
|
|
380
331
|
return ctx.config.packageName + packageSuffix;
|
|
381
332
|
}
|
|
382
|
-
shouldGenerateTypeDeclaration(ctx,
|
|
333
|
+
shouldGenerateTypeDeclaration(ctx, args) {
|
|
334
|
+
let { schema } = args;
|
|
383
335
|
// All enum types should have its own type declaration
|
|
384
336
|
if (schema.enum !== undefined && schema.enum.length > 0) {
|
|
385
337
|
return true;
|
|
@@ -395,7 +347,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
395
347
|
if (schema.kind === 'multi-type') {
|
|
396
348
|
return false;
|
|
397
349
|
}
|
|
398
|
-
schema = this.normalizeSchema(ctx, schema);
|
|
350
|
+
schema = this.normalizeSchema(ctx, { schema });
|
|
399
351
|
if (schema.kind === 'combined' || schema.kind === 'oneOf') {
|
|
400
352
|
return false;
|
|
401
353
|
}
|
|
@@ -405,17 +357,18 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
405
357
|
}
|
|
406
358
|
return true;
|
|
407
359
|
}
|
|
408
|
-
getDeclarationTypeName(ctx,
|
|
409
|
-
return (0, core_1.toCasing)(schema.name,
|
|
360
|
+
getDeclarationTypeName(ctx, args) {
|
|
361
|
+
return (0, core_1.toCasing)(args.schema.name, ctx.config.typeNameCasing);
|
|
410
362
|
}
|
|
411
|
-
getInheritedSchemas(ctx,
|
|
412
|
-
return schema.inheritedSchemas
|
|
413
|
-
.filter((
|
|
363
|
+
getInheritedSchemas(ctx, args) {
|
|
364
|
+
return args.schema.inheritedSchemas
|
|
365
|
+
.filter((schema) => this.shouldGenerateTypeDeclaration(ctx, { schema }) && !schema.isNameGenerated)
|
|
414
366
|
.filter((item, index, self) => self.indexOf(item) === index);
|
|
415
367
|
}
|
|
416
|
-
getClassProperties(ctx,
|
|
368
|
+
getClassProperties(ctx, args) {
|
|
417
369
|
var _a;
|
|
418
|
-
const
|
|
370
|
+
const { schema } = args;
|
|
371
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, { schema });
|
|
419
372
|
const properties = [];
|
|
420
373
|
const appendedProperties = [];
|
|
421
374
|
for (const property of schema.properties.values()) {
|
|
@@ -433,19 +386,20 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
433
386
|
}
|
|
434
387
|
properties.push(property);
|
|
435
388
|
}
|
|
436
|
-
return [...this.sortProperties(ctx, schema, properties), ...appendedProperties];
|
|
389
|
+
return [...this.sortProperties(ctx, { schema, properties }), ...appendedProperties];
|
|
437
390
|
}
|
|
438
|
-
sortProperties(ctx,
|
|
439
|
-
return [...properties].sort((a, b) => classify(a) - classify(b));
|
|
391
|
+
sortProperties(ctx, args) {
|
|
392
|
+
return [...args.properties].sort((a, b) => classify(a) - classify(b));
|
|
440
393
|
function classify(p) {
|
|
441
394
|
if (p.schema.default !== undefined)
|
|
442
395
|
return 1;
|
|
443
|
-
if (schema.required.has(p.name))
|
|
396
|
+
if (args.schema.required.has(p.name))
|
|
444
397
|
return 0;
|
|
445
398
|
return 2;
|
|
446
399
|
}
|
|
447
400
|
}
|
|
448
|
-
normalizeSchema(ctx,
|
|
401
|
+
normalizeSchema(ctx, args) {
|
|
402
|
+
let { schema } = args;
|
|
449
403
|
if (schema.kind === 'oneOf') {
|
|
450
404
|
schema =
|
|
451
405
|
ctx.config.oneOfBehavior === 'treat-as-any-of'
|
|
@@ -460,10 +414,11 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
460
414
|
}
|
|
461
415
|
return schema;
|
|
462
416
|
}
|
|
463
|
-
hasProperty(ctx,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
('
|
|
417
|
+
hasProperty(ctx, args) {
|
|
418
|
+
const { schema, propertyName } = args;
|
|
419
|
+
return (('properties' in schema && schema.properties.has(propertyName)) ||
|
|
420
|
+
('anyOf' in schema && schema.anyOf.some((schema) => this.hasProperty(ctx, { schema, propertyName }))) ||
|
|
421
|
+
('allOf' in schema && schema.allOf.some((schema) => this.hasProperty(ctx, { schema, propertyName }))));
|
|
467
422
|
}
|
|
468
423
|
}
|
|
469
424
|
exports.DefaultKotlinModelGenerator = DefaultKotlinModelGenerator;
|