@goast/kotlin 0.5.1-beta.1 → 0.5.1

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.
Files changed (94) hide show
  1. package/LICENSE +21 -21
  2. package/assets/client/okhttp3/ApiAbstractions.kt +30 -30
  3. package/assets/client/okhttp3/ApiClient.kt +253 -253
  4. package/assets/client/okhttp3/ApiResponse.kt +43 -43
  5. package/assets/client/okhttp3/Errors.kt +21 -21
  6. package/assets/client/okhttp3/PartConfig.kt +11 -11
  7. package/assets/client/okhttp3/RequestConfig.kt +18 -18
  8. package/assets/client/okhttp3/RequestMethod.kt +8 -8
  9. package/assets/client/okhttp3/ResponseExtensions.kt +24 -24
  10. package/assets/client/spring-reactive-web-clients/ApiRequestFile.kt +33 -33
  11. package/esm/src/generators/models/model-generator.d.ts.map +1 -1
  12. package/esm/src/generators/models/model-generator.js +1 -0
  13. package/package.json +2 -2
  14. package/script/src/generators/models/model-generator.d.ts.map +1 -1
  15. package/script/src/generators/models/model-generator.js +1 -0
  16. package/src/mod.ts +8 -0
  17. package/src/src/assets.ts +9 -0
  18. package/src/src/ast/_index.ts +66 -0
  19. package/src/src/ast/common.ts +1 -0
  20. package/src/src/ast/index.ts +1 -0
  21. package/src/src/ast/node.ts +10 -0
  22. package/src/src/ast/nodes/annotation.ts +79 -0
  23. package/src/src/ast/nodes/argument.ts +62 -0
  24. package/src/src/ast/nodes/call.ts +75 -0
  25. package/src/src/ast/nodes/class.ts +178 -0
  26. package/src/src/ast/nodes/collection-literal.ts +49 -0
  27. package/src/src/ast/nodes/constructor.ts +126 -0
  28. package/src/src/ast/nodes/doc-tag.ts +138 -0
  29. package/src/src/ast/nodes/doc.ts +111 -0
  30. package/src/src/ast/nodes/enum-value.ts +100 -0
  31. package/src/src/ast/nodes/enum.ts +163 -0
  32. package/src/src/ast/nodes/function.ts +178 -0
  33. package/src/src/ast/nodes/generic-parameter.ts +54 -0
  34. package/src/src/ast/nodes/init-block.ts +38 -0
  35. package/src/src/ast/nodes/interface.ts +133 -0
  36. package/src/src/ast/nodes/lambda-type.ts +73 -0
  37. package/src/src/ast/nodes/lambda.ts +74 -0
  38. package/src/src/ast/nodes/object.ts +102 -0
  39. package/src/src/ast/nodes/parameter.ts +118 -0
  40. package/src/src/ast/nodes/property.ts +225 -0
  41. package/src/src/ast/nodes/reference.ts +178 -0
  42. package/src/src/ast/nodes/string.ts +114 -0
  43. package/src/src/ast/nodes/types.ts +23 -0
  44. package/src/src/ast/references/index.ts +10 -0
  45. package/src/src/ast/references/jackson.ts +44 -0
  46. package/src/src/ast/references/jakarta.ts +14 -0
  47. package/src/src/ast/references/java.ts +20 -0
  48. package/src/src/ast/references/kotlin.ts +41 -0
  49. package/src/src/ast/references/kotlinx.ts +14 -0
  50. package/src/src/ast/references/okhttp3.ts +5 -0
  51. package/src/src/ast/references/reactor.ts +5 -0
  52. package/src/src/ast/references/spring-reactive.ts +33 -0
  53. package/src/src/ast/references/spring.ts +86 -0
  54. package/src/src/ast/references/swagger.ts +23 -0
  55. package/src/src/ast/utils/get-kotlin-builder-options.ts +19 -0
  56. package/src/src/ast/utils/to-kt-node.ts +31 -0
  57. package/src/src/ast/utils/write-kt-annotations.ts +15 -0
  58. package/src/src/ast/utils/write-kt-arguments.ts +45 -0
  59. package/src/src/ast/utils/write-kt-enum-values.ts +27 -0
  60. package/src/src/ast/utils/write-kt-generic-parameters.ts +12 -0
  61. package/src/src/ast/utils/write-kt-members.ts +25 -0
  62. package/src/src/ast/utils/write-kt-node.ts +37 -0
  63. package/src/src/ast/utils/write-kt-parameters.ts +25 -0
  64. package/src/src/common-results.ts +4 -0
  65. package/src/src/config.ts +41 -0
  66. package/src/src/file-builder.ts +112 -0
  67. package/src/src/generators/file-generator.ts +29 -0
  68. package/src/src/generators/index.ts +5 -0
  69. package/src/src/generators/models/args.ts +132 -0
  70. package/src/src/generators/models/index.ts +4 -0
  71. package/src/src/generators/models/model-generator.ts +703 -0
  72. package/src/src/generators/models/models-generator.ts +65 -0
  73. package/src/src/generators/models/models.ts +95 -0
  74. package/src/src/generators/services/okhttp3-clients/args.ts +88 -0
  75. package/src/src/generators/services/okhttp3-clients/index.ts +4 -0
  76. package/src/src/generators/services/okhttp3-clients/models.ts +73 -0
  77. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +597 -0
  78. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +169 -0
  79. package/src/src/generators/services/okhttp3-clients/refs.ts +59 -0
  80. package/src/src/generators/services/spring-controllers/args.ts +93 -0
  81. package/src/src/generators/services/spring-controllers/index.ts +4 -0
  82. package/src/src/generators/services/spring-controllers/models.ts +76 -0
  83. package/src/src/generators/services/spring-controllers/refs.ts +17 -0
  84. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +1084 -0
  85. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +140 -0
  86. package/src/src/generators/services/spring-reactive-web-clients/args.ts +101 -0
  87. package/src/src/generators/services/spring-reactive-web-clients/index.ts +4 -0
  88. package/src/src/generators/services/spring-reactive-web-clients/models.ts +62 -0
  89. package/src/src/generators/services/spring-reactive-web-clients/refs.ts +11 -0
  90. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-client-generator.ts +571 -0
  91. package/src/src/generators/services/spring-reactive-web-clients/spring-reactive-web-clients-generator.ts +125 -0
  92. package/src/src/import-collection.ts +98 -0
  93. package/src/src/types.ts +3 -0
  94. package/src/src/utils.ts +39 -0
@@ -0,0 +1,703 @@
1
+ import {
2
+ type ApiSchema,
3
+ type ApiSchemaKind,
4
+ type ApiSchemaProperty,
5
+ type AppendValue,
6
+ type AppendValueGroup,
7
+ appendValueGroup,
8
+ builderTemplate as s,
9
+ createOverwriteProxy,
10
+ DEFAULT_IGNORED_SCHEMA_PROPERTIES,
11
+ getSchemaReference,
12
+ getSourceDisplayName,
13
+ type MaybePromise,
14
+ modify,
15
+ modifyEach,
16
+ notNullish,
17
+ resolveAnyOfAndAllOf,
18
+ type SourceBuilder,
19
+ toCasing,
20
+ } from '@goast/core';
21
+
22
+ import { kt } from '../../ast/index.js';
23
+ import { KotlinFileBuilder } from '../../file-builder.js';
24
+ import { KotlinFileGenerator } from '../file-generator.js';
25
+ import type { DefaultKotlinModelGeneratorArgs as Args } from './index.js';
26
+ import type { KotlinModelGeneratorContext, KotlinModelGeneratorOutput } from './models.js';
27
+
28
+ type Context = KotlinModelGeneratorContext;
29
+ type Output = KotlinModelGeneratorOutput;
30
+ type Builder = KotlinFileBuilder;
31
+
32
+ export interface KotlinModelGenerator<TOutput extends Output = Output> {
33
+ generate(ctx: Context): MaybePromise<TOutput>;
34
+ }
35
+
36
+ export class DefaultKotlinModelGenerator extends KotlinFileGenerator<Context, Output> implements KotlinModelGenerator {
37
+ public generate(ctx: Context): MaybePromise<KotlinModelGeneratorOutput> {
38
+ if (/\/(anyOf|allOf)(\/[0-9]+)?$/.test(ctx.schema.$src.path)) {
39
+ // Do not generate types that are only used for anyOf and/or allOf
40
+ return { type: kt.refs.any({ nullable: true }) };
41
+ }
42
+
43
+ const schemaReference = getSchemaReference(ctx.schema, DEFAULT_IGNORED_SCHEMA_PROPERTIES);
44
+ if (schemaReference !== ctx.schema) {
45
+ const typeName = this.getDeclarationTypeName(ctx, { schema: schemaReference });
46
+ const packageName = this.getPackageName(ctx, { schema: schemaReference });
47
+ return { type: kt.reference(typeName, packageName) };
48
+ }
49
+
50
+ if (this.shouldGenerateTypeDeclaration(ctx, { schema: ctx.schema })) {
51
+ const typeName = this.getDeclarationTypeName(ctx, { schema: ctx.schema });
52
+ const packageName = this.getPackageName(ctx, { schema: ctx.schema });
53
+ const filePath = `${ctx.config.outputDir}/${packageName.replace(/\./g, '/')}/${typeName}.kt`;
54
+
55
+ console.log(
56
+ `Generating model ${getSourceDisplayName(ctx.data, ctx.schema)} [${packageName}.${typeName}] to ${filePath}...`,
57
+ );
58
+ const builder = new KotlinFileBuilder(packageName, ctx.config);
59
+ builder.append(this.getFileContent(ctx, {}));
60
+ builder.writeToFile(filePath);
61
+
62
+ return { type: kt.reference(typeName, packageName) };
63
+ } else {
64
+ return { type: this.getType(ctx, { schema: ctx.schema }) };
65
+ }
66
+ }
67
+
68
+ protected getFileContent(ctx: Context, _args: Args.GetFileContent): AppendValueGroup<Builder> {
69
+ return appendValueGroup<Builder>(
70
+ [this.getSchemaDeclaration(ctx, { schema: this.normalizeSchema(ctx, { schema: ctx.schema }) })],
71
+ '\n\n',
72
+ );
73
+ }
74
+
75
+ protected getSchemaDeclaration(ctx: Context, args: Args.GetSchemaDeclaration): AppendValue<Builder> {
76
+ const { schema } = args;
77
+
78
+ if (schema.kind === 'object') {
79
+ return schema.discriminator ? this.getInterface(ctx, { schema }) : this.getClass(ctx, { schema });
80
+ } else if (schema.enum !== undefined && schema.enum.length > 0) {
81
+ return this.getEnum(ctx, { schema });
82
+ }
83
+
84
+ 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.';
85
+ }
86
+
87
+ protected getClass(ctx: Context, args: Args.GetClass): kt.Class<Builder> {
88
+ const { schema } = args;
89
+ const inheritedSchemas = this.getInheritedSchemas(ctx, { schema });
90
+ const parameters = this.getClassProperties(ctx, { schema });
91
+
92
+ return kt.class(this.getDeclarationTypeName(ctx, { schema }), {
93
+ doc: kt.doc(schema.description?.trim()),
94
+ annotations: [
95
+ this.getJacksonJsonClassDescriptionAnnotation(ctx, { schema }),
96
+ schema.deprecated ? kt.annotation(kt.refs.deprecated(), [kt.argument(kt.string(''))]) : null,
97
+ ],
98
+ classKind: parameters.length === 0 ? null : 'data',
99
+ implements: inheritedSchemas.map((schema) => this.getType(ctx, { schema, nullable: false })),
100
+ primaryConstructor: kt.constructor(
101
+ parameters.map((property) => this.getClassParameter(ctx, { ...args, inheritedSchemas, parameters, property })),
102
+ ),
103
+ members: [
104
+ ...(schema.additionalProperties !== undefined && schema.additionalProperties !== false
105
+ ? [
106
+ this.getAdditionalPropertiesProperty(ctx, { schema }),
107
+ this.getAdditionalPropertiesSetter(ctx, { schema }),
108
+ this.getAdditionalPropertiesGetter(ctx, { schema }),
109
+ ]
110
+ : []),
111
+ ],
112
+ });
113
+ }
114
+
115
+ protected getInterface(ctx: Context, args: Args.GetInterface): kt.Interface<Builder> {
116
+ const { schema } = args;
117
+
118
+ return kt.interface(this.getDeclarationTypeName(ctx, { schema }), {
119
+ doc: kt.doc(schema.description?.trim()),
120
+ annotations: [
121
+ this.getJacksonJsonTypeInfoAnnotation(ctx, { schema }),
122
+ this.getJacksonJsonSubTypesAnnotation(ctx, { schema }),
123
+ this.getJacksonJsonClassDescriptionAnnotation(ctx, { schema }),
124
+ schema.deprecated ? kt.annotation(kt.refs.deprecated(), [kt.argument(kt.string(''))]) : null,
125
+ ].filter(notNullish),
126
+ members: this.sortProperties(ctx, { schema, properties: schema.properties.values() }).map((property) =>
127
+ this.getInterfaceProperty(ctx, { schema, property })
128
+ ),
129
+ });
130
+ }
131
+
132
+ protected getEnum(ctx: Context, args: Args.GetEnum): kt.Enum<Builder> {
133
+ const { schema } = args;
134
+
135
+ const name = this.getDeclarationTypeName(ctx, { schema });
136
+ return kt.enum(
137
+ name,
138
+ schema.enum?.map((x) =>
139
+ kt.enumValue(toCasing(String(x), ctx.config.enumValueNameCasing), {
140
+ annotations: [kt.annotation(kt.refs.jackson.jsonProperty(), [kt.argument(kt.string(String(x)))])],
141
+ arguments: [kt.argument(kt.string(String(x)))],
142
+ })
143
+ ) ?? [],
144
+ {
145
+ doc: kt.doc(schema.description?.trim()),
146
+ annotations: [schema.deprecated ? kt.annotation(kt.refs.deprecated(), [kt.argument(kt.string(''))]) : null],
147
+ primaryConstructor: kt.constructor([
148
+ kt.parameter.class(toCasing('value', ctx.config.propertyNameCasing), kt.refs.string(), {
149
+ property: 'readonly',
150
+ }),
151
+ ]),
152
+ companionObject: kt.object({
153
+ members: [
154
+ kt.function('fromValue', {
155
+ parameters: [kt.parameter('value', kt.refs.string())],
156
+ returnType: kt.reference(name, null, { nullable: true }),
157
+ singleExpression: true,
158
+ body: !schema.enum?.length ? 'null' : s`\nwhen(value) {${s.indent`${
159
+ appendValueGroup([
160
+ schema.enum.map((x) =>
161
+ s`\n${kt.string(String(x))} -> ${toCasing(String(x), ctx.config.enumValueNameCasing)}`
162
+ ),
163
+ ])
164
+ }
165
+ else -> null`}
166
+ }`,
167
+ }),
168
+ ],
169
+ }),
170
+ },
171
+ );
172
+ }
173
+
174
+ protected getType(ctx: Context, args: Args.GetType): kt.Reference<SourceBuilder> {
175
+ let { schema, nullable } = args;
176
+
177
+ if (schema.kind === 'oneOf' && schema.oneOf.length === 2 && schema.oneOf.some((x) => x.kind === 'null')) {
178
+ const nonNullSchema = schema.oneOf.find((x) => x.kind !== 'null')!;
179
+ if (nonNullSchema) {
180
+ schema = nonNullSchema;
181
+ nullable ??= true;
182
+ }
183
+ }
184
+
185
+ const generatedType = this.getGeneratedType(ctx, { schema, nullable });
186
+ if (generatedType) {
187
+ return generatedType;
188
+ }
189
+
190
+ nullable ??= schema.nullable;
191
+ switch (schema.kind) {
192
+ case 'boolean':
193
+ return kt.refs.boolean({ nullable });
194
+ case 'integer':
195
+ case 'number':
196
+ switch (schema.format) {
197
+ case 'int32':
198
+ return kt.refs.int({ nullable });
199
+ case 'int64':
200
+ return kt.refs.long({ nullable });
201
+ case 'float':
202
+ return kt.refs.float({ nullable });
203
+ case 'double':
204
+ return kt.refs.double({ nullable });
205
+ default:
206
+ return schema.kind === 'integer' ? kt.refs.int({ nullable }) : kt.refs.double({ nullable });
207
+ }
208
+ case 'string':
209
+ switch (schema.format) {
210
+ case 'date-time':
211
+ return kt.refs.java.offsetDateTime({ nullable });
212
+ default:
213
+ return kt.refs.string({ nullable });
214
+ }
215
+ case 'null':
216
+ return kt.refs.nothing({ nullable });
217
+ case 'unknown':
218
+ return kt.refs.any({ nullable });
219
+ case 'array':
220
+ return kt.refs.list(
221
+ [schema.items ? this.getType(ctx, { schema: schema.items }) : kt.refs.any({ nullable: true })],
222
+ { nullable },
223
+ );
224
+ case 'object':
225
+ return schema.properties.size === 0 && schema.additionalProperties
226
+ ? kt.refs.map([kt.refs.string(), this.getAdditionalPropertiesType(ctx, { schema })], { nullable })
227
+ : kt.refs.any({ nullable });
228
+ default:
229
+ return kt.refs.any({ nullable });
230
+ }
231
+ }
232
+
233
+ protected getGeneratedType(ctx: Context, args: Args.GetGeneratedType): kt.Reference<SourceBuilder> | null {
234
+ const schema = getSchemaReference(args.schema, DEFAULT_IGNORED_SCHEMA_PROPERTIES);
235
+ if (this.shouldGenerateTypeDeclaration(ctx, { schema })) {
236
+ return kt.reference(this.getDeclarationTypeName(ctx, { schema }), this.getPackageName(ctx, { schema }), {
237
+ nullable: args.nullable ?? schema.nullable,
238
+ });
239
+ }
240
+ return null;
241
+ }
242
+
243
+ protected getAdditionalPropertiesType(
244
+ ctx: Context,
245
+ args: Args.GetAdditionalPropertiesType,
246
+ ): kt.Reference<SourceBuilder> {
247
+ const { schema } = args;
248
+
249
+ return typeof schema.additionalProperties === 'object'
250
+ ? this.getType(ctx, { schema: schema.additionalProperties })
251
+ : kt.refs.any({ nullable: true });
252
+ }
253
+
254
+ protected getDefaultValue(ctx: Context, args: Args.GetDefaultValue): kt.Value<Builder> {
255
+ const { schema } = args;
256
+
257
+ if (schema.default === null || schema.default === undefined) {
258
+ return 'null';
259
+ } else {
260
+ switch (schema.kind) {
261
+ case 'boolean':
262
+ return Boolean(schema.default) || String(schema.default).toLowerCase() === 'true' ? 'true' : 'false';
263
+ case 'integer':
264
+ case 'number':
265
+ return String(schema.default);
266
+ case 'string':
267
+ return schema.enum && schema.enum.length > 0
268
+ ? kt.call([this.getType(ctx, { schema }), toCasing(String(schema.default), ctx.config.enumValueNameCasing)])
269
+ : kt.string(String(schema.default));
270
+ case 'array':
271
+ return kt.call(
272
+ kt.refs.listOf.infer(),
273
+ Array.isArray(schema.default) ? schema.default.map((x) => kt.toNode(x)) : [],
274
+ );
275
+ default:
276
+ return 'null';
277
+ }
278
+ }
279
+ }
280
+
281
+ // #region Members
282
+ protected getClassParameter(ctx: Context, args: Args.GetClassParameter): kt.Parameter<Builder> {
283
+ const { schema, inheritedSchemas, property } = args;
284
+
285
+ return kt.parameter.class(
286
+ toCasing(property.name, ctx.config.propertyNameCasing),
287
+ this.getType(ctx, { schema: property.schema, nullable: schema.required.has(property.name) ? undefined : true }),
288
+ {
289
+ description: property.schema.description?.trim(),
290
+ annotations: [
291
+ ...this.getJakartaValidationAnnotations(ctx, { schema, property }),
292
+ this.getSwaggerSchemaAnnotation(ctx, { schema, property }),
293
+ modify(this.getJacksonJsonPropertyAnnotation(ctx, { schema, property }), (x) => (x.target = 'param')),
294
+ ...modifyEach(
295
+ [
296
+ this.getJacksonJsonPropertyAnnotation(ctx, { schema, property }),
297
+ this.getJacksonJsonPropertyDescriptionAnnotation(ctx, { schema, property }),
298
+ this.getJacksonJsonIncludeAnnotation(ctx, { schema, property }),
299
+ ].filter(notNullish),
300
+ (x) => (x.target = 'get'),
301
+ ),
302
+ property.schema.deprecated ? kt.annotation(kt.refs.deprecated(), [kt.argument(kt.string(''))]) : null,
303
+ ].filter(notNullish),
304
+ override: inheritedSchemas.some((schema) => this.hasProperty(ctx, { schema, propertyName: property.name })),
305
+ property: 'readonly',
306
+ default: property.schema.default !== undefined || !schema.required.has(property.name)
307
+ ? this.getDefaultValue(ctx, { schema: property.schema })
308
+ : null,
309
+ },
310
+ );
311
+ }
312
+
313
+ protected getInterfaceProperty(ctx: Context, args: Args.GetInterfaceProperty): kt.Property<Builder> {
314
+ const { schema, property } = args;
315
+
316
+ return kt.property(toCasing(property.name, ctx.config.propertyNameCasing), {
317
+ doc: kt.doc(property.schema.description?.trim()),
318
+ annotations: modifyEach(
319
+ [
320
+ this.getJacksonJsonPropertyAnnotation(ctx, { schema, property }),
321
+ this.getJacksonJsonPropertyDescriptionAnnotation(ctx, { schema, property }),
322
+ this.getJacksonJsonIncludeAnnotation(ctx, { schema, property }),
323
+ property.schema.deprecated ? kt.annotation(kt.refs.deprecated(), [kt.argument(kt.string(''))]) : null,
324
+ ].filter(notNullish),
325
+ (x) => (x.target = 'get'),
326
+ ),
327
+ type: this.getType(ctx, {
328
+ schema: property.schema,
329
+ nullable: schema.required.has(property.name) ? undefined : true,
330
+ }),
331
+ });
332
+ }
333
+
334
+ protected getAdditionalPropertiesProperty(
335
+ ctx: Context,
336
+ args: Args.GetAdditionalPropertiesProperty,
337
+ ): kt.Property<Builder> {
338
+ const { schema } = args;
339
+
340
+ return kt.property(toCasing('additionalProperties', ctx.config.propertyNameCasing), {
341
+ annotations: [kt.annotation(kt.refs.jackson.jsonIgnore())],
342
+ type: kt.reference('MutableMap', null, {
343
+ generics: ['String', this.getAdditionalPropertiesType(ctx, { schema })],
344
+ }),
345
+ default: 'mutableMapOf()',
346
+ });
347
+ }
348
+
349
+ protected getAdditionalPropertiesSetter(
350
+ ctx: Context,
351
+ args: Args.GetAdditionalPropertiesSetter,
352
+ ): kt.Function<Builder> {
353
+ const { schema } = args;
354
+
355
+ return kt.function(toCasing('set', ctx.config.functionNameCasing), {
356
+ annotations: [kt.annotation(kt.refs.jackson.jsonAnySetter())],
357
+ parameters: [
358
+ kt.parameter(toCasing('name', ctx.config.parameterNameCasing), 'String'),
359
+ kt.parameter(
360
+ toCasing('value', ctx.config.parameterNameCasing),
361
+ this.getAdditionalPropertiesType(ctx, { schema }),
362
+ ),
363
+ ],
364
+ body: `this.${toCasing('additionalProperties', ctx.config.propertyNameCasing)}[name] = value`,
365
+ });
366
+ }
367
+
368
+ protected getAdditionalPropertiesGetter(
369
+ ctx: Context,
370
+ args: Args.GetAdditionalPropertiesGetter,
371
+ ): kt.Function<Builder> {
372
+ const { schema } = args;
373
+
374
+ return kt.function(toCasing('getMap', ctx.config.functionNameCasing), {
375
+ annotations: [kt.annotation(kt.refs.jackson.jsonAnyGetter())],
376
+ returnType: kt.refs.map([kt.refs.string(), this.getAdditionalPropertiesType(ctx, { schema })]),
377
+ body: `return this.${toCasing('additionalProperties', ctx.config.propertyNameCasing)}`,
378
+ });
379
+ }
380
+ // #endregion
381
+
382
+ // #region Annotations
383
+ protected getJacksonJsonTypeInfoAnnotation(
384
+ ctx: Context,
385
+ args: Args.GetJacksonJsonTypeInfoAnnotation,
386
+ ): kt.Annotation<Builder> | null {
387
+ const { schema } = args;
388
+
389
+ return ctx.config.addJacksonAnnotations && schema.discriminator
390
+ ? kt.annotation(kt.refs.jackson.jsonTypeInfo(), [
391
+ kt.argument.named('use', 'JsonTypeInfo.Id.NAME'),
392
+ kt.argument.named(
393
+ 'include',
394
+ 'properties' in schema && schema.properties.has(schema.discriminator.propertyName)
395
+ ? 'JsonTypeInfo.As.EXISTING_PROPERTY'
396
+ : 'JsonTypeInfo.As.PROPERTY',
397
+ ),
398
+ kt.argument.named('property', kt.string(schema.discriminator.propertyName)),
399
+ kt.argument.named('visible', 'true'),
400
+ ])
401
+ : null;
402
+ }
403
+
404
+ protected getJacksonJsonSubTypesAnnotation(
405
+ ctx: Context,
406
+ args: Args.GetJacksonJsonSubTypesAnnotation,
407
+ ): kt.Annotation<Builder> | null {
408
+ const { schema } = args;
409
+
410
+ if (!ctx.config.addJacksonAnnotations || !schema.discriminator) return null;
411
+ const entries = Object.entries(schema.discriminator.mapping);
412
+ return entries.length > 0
413
+ ? kt.annotation(
414
+ kt.refs.jackson.jsonSubTypes(),
415
+ entries.map(([value, schema]) =>
416
+ kt.argument(
417
+ kt.call(
418
+ [kt.refs.jackson.jsonSubTypes(), 'Type'],
419
+ [
420
+ kt.argument.named(
421
+ 'value',
422
+ modify(this.getType(ctx, { schema }), (x) => (x.classReference = true)),
423
+ ),
424
+ kt.argument.named('name', kt.string(value)),
425
+ ],
426
+ ),
427
+ )
428
+ ),
429
+ )
430
+ : null;
431
+ }
432
+
433
+ protected getJacksonJsonClassDescriptionAnnotation(
434
+ ctx: Context,
435
+ args: Args.GetJacksonJsonClassDescriptionAnnotation,
436
+ ): kt.Annotation<Builder> | null {
437
+ const { schema } = args;
438
+
439
+ return ctx.config.addJacksonAnnotations && schema.description
440
+ ? kt.annotation(kt.refs.jackson.jsonClassDescription(), [
441
+ kt.argument(kt.string(schema.description)),
442
+ ])
443
+ : null;
444
+ }
445
+
446
+ protected getJacksonJsonPropertyAnnotation(
447
+ ctx: Context,
448
+ args: Args.GetJacksonJsonPropertyAnnotation,
449
+ ): kt.Annotation<Builder> | null {
450
+ const { schema, property } = args;
451
+
452
+ return ctx.config.addJacksonAnnotations
453
+ ? kt.annotation(kt.refs.jackson.jsonProperty(), [
454
+ kt.argument(kt.string(property.name)),
455
+ schema.required.has(property.name) ? kt.argument.named('required', 'true') : null,
456
+ ])
457
+ : null;
458
+ }
459
+
460
+ protected getJacksonJsonPropertyDescriptionAnnotation(
461
+ ctx: Context,
462
+ args: Args.GetJacksonJsonPropertyDescriptionAnnotation,
463
+ ): kt.Annotation<Builder> | null {
464
+ const { property } = args;
465
+
466
+ return ctx.config.addJacksonAnnotations && property.schema.description
467
+ ? kt.annotation(kt.refs.jackson.jsonPropertyDescription(), [
468
+ kt.argument(kt.string(property.schema.description)),
469
+ ])
470
+ : null;
471
+ }
472
+
473
+ protected getJacksonJsonIncludeAnnotation(
474
+ ctx: Context,
475
+ args: Args.GetJacksonJsonIncludeAnnotation,
476
+ ): kt.Annotation<Builder> | null {
477
+ const { property } = args;
478
+
479
+ return ctx.config.addJacksonAnnotations && property.schema.custom['exclude-when-null'] === true
480
+ ? kt.annotation(kt.refs.jackson.jsonInclude(), [
481
+ kt.argument(kt.call([kt.refs.jackson.jsonInclude(), 'Include', 'NON_NULL'])),
482
+ ])
483
+ : null;
484
+ }
485
+
486
+ protected getJakartaValidationAnnotations(
487
+ ctx: Context,
488
+ args: Args.GetJakartaValidationAnnotations,
489
+ ): kt.Annotation<Builder>[] {
490
+ const { property } = args;
491
+ const annotations: kt.Annotation<Builder>[] = [];
492
+ if (!ctx.config.addJakartaValidationAnnotations) {
493
+ return annotations;
494
+ }
495
+
496
+ if (property.schema.kind === 'string') {
497
+ if (property.schema.pattern) {
498
+ annotations.push(
499
+ kt.annotation(kt.refs.jakarta.pattern(), [kt.argument.named('regexp', kt.string(property.schema.pattern))], {
500
+ target: 'get',
501
+ }),
502
+ );
503
+ }
504
+ if (property.schema.minLength === 1 && property.schema.maxLength === undefined) {
505
+ annotations.push(kt.annotation(kt.refs.jakarta.notEmpty(), [], { target: 'get' }));
506
+ } else if (property.schema.minLength !== undefined || property.schema.maxLength !== undefined) {
507
+ annotations.push(kt.annotation(kt.refs.jakarta.size(), [
508
+ property.schema.minLength !== undefined ? kt.argument.named('min', property.schema.minLength) : null,
509
+ property.schema.maxLength !== undefined ? kt.argument.named('max', property.schema.maxLength) : null,
510
+ ], { target: 'get' }));
511
+ }
512
+ } else if (property.schema.kind === 'number' || property.schema.kind === 'integer') {
513
+ if (property.schema.minimum !== undefined) {
514
+ annotations.push(
515
+ kt.annotation(kt.refs.jakarta.min(), [kt.argument.named('value', property.schema.minimum)], {
516
+ target: 'get',
517
+ }),
518
+ );
519
+ }
520
+ if (property.schema.maximum !== undefined) {
521
+ annotations.push(
522
+ kt.annotation(kt.refs.jakarta.max(), [kt.argument.named('value', property.schema.maximum)], {
523
+ target: 'get',
524
+ }),
525
+ );
526
+ }
527
+ } else if (property.schema.kind === 'array') {
528
+ if (property.schema.minItems !== undefined || property.schema.maxItems !== undefined) {
529
+ annotations.push(kt.annotation(kt.refs.jakarta.size(), [
530
+ property.schema.minItems !== undefined ? kt.argument.named('min', property.schema.minItems) : null,
531
+ property.schema.maxItems !== undefined ? kt.argument.named('max', property.schema.maxItems) : null,
532
+ ], { target: 'get' }));
533
+ }
534
+ }
535
+
536
+ if (this.shouldGenerateTypeDeclaration(ctx, { schema: property.schema })) {
537
+ annotations.push(kt.annotation(kt.refs.jakarta.valid(), [], { target: 'field' }));
538
+ }
539
+
540
+ return annotations;
541
+ }
542
+
543
+ protected getSwaggerSchemaAnnotation(
544
+ ctx: Context,
545
+ args: Args.GetSwaggerSchemaAnnotation,
546
+ ): kt.Annotation<Builder> | null {
547
+ const { schema, property } = args;
548
+
549
+ return ctx.config.addSwaggerAnnotations
550
+ ? kt.annotation(kt.refs.swagger.schema(), [
551
+ property.schema.example !== undefined
552
+ ? kt.argument.named('example', kt.string(String(property.schema.example)))
553
+ : null,
554
+ schema.required.has(property.name) ? kt.argument.named('required', 'true') : null,
555
+ property.schema.description !== undefined
556
+ ? kt.argument.named('description', kt.string(property.schema.description))
557
+ : null,
558
+ property.schema.deprecated ? kt.argument.named('deprecated', kt.toNode(property.schema.deprecated)) : null,
559
+ ])
560
+ : null;
561
+ }
562
+ // #endregion
563
+
564
+ protected getPackageName(ctx: Context, args: Args.GetPackageName): string {
565
+ const { schema } = args;
566
+
567
+ const packageSuffix = typeof ctx.config.packageSuffix === 'string'
568
+ ? ctx.config.packageSuffix
569
+ : ctx.config.packageSuffix(schema);
570
+ return ctx.config.packageName + packageSuffix;
571
+ }
572
+
573
+ protected shouldGenerateTypeDeclaration(ctx: Context, args: Args.ShouldGenerateTypeDeclaration): boolean {
574
+ let { schema } = args;
575
+
576
+ // All enum types should have its own type declaration
577
+ if (schema.enum !== undefined && schema.enum.length > 0) {
578
+ return true;
579
+ }
580
+
581
+ // All primitive types already exist and do not need its own type declaration
582
+ if (
583
+ schema.kind !== 'combined' &&
584
+ schema.kind !== 'multi-type' &&
585
+ schema.kind !== 'object' &&
586
+ schema.kind !== 'oneOf'
587
+ ) {
588
+ return false;
589
+ }
590
+
591
+ // Too complex types cannot be represented in Kotlin, so they fallback to Any
592
+ if (schema.kind === 'multi-type') {
593
+ return false;
594
+ }
595
+ schema = this.normalizeSchema(ctx, { schema });
596
+ if (schema.kind === 'combined' || schema.kind === 'oneOf') {
597
+ return false;
598
+ }
599
+
600
+ // Schemas representable by a simple Map type do not need its own type declaration
601
+ if (schema.kind === 'object' && schema.properties.size === 0 && schema.additionalProperties) {
602
+ return false;
603
+ }
604
+
605
+ if (schema.kind === 'object' && ctx.config.emptyObjectTypeBehavior === 'use-any' && schema.properties.size === 0) {
606
+ return false;
607
+ }
608
+
609
+ // Dynamically generated schemas do not have its own type declaration
610
+ if (!ctx.data.schemas.some((x) => x.id === schema.id)) {
611
+ return false;
612
+ }
613
+
614
+ // multipart schemas should not have its own type declaration
615
+ if (schema.$src.path.endsWith('/requestBody/content/multipart/form-data/schema')) {
616
+ return false;
617
+ }
618
+
619
+ return true;
620
+ }
621
+
622
+ protected getDeclarationTypeName(ctx: Context, args: Args.GetDeclarationTypeName): string {
623
+ return toCasing(args.schema.name, ctx.config.typeNameCasing);
624
+ }
625
+
626
+ protected getInheritedSchemas(
627
+ ctx: Context,
628
+ args: Args.GetInheritedSchemas,
629
+ ): (ApiSchema<ApiSchemaKind> & { discriminator: NonNullable<ApiSchema['discriminator']> })[] {
630
+ return args.schema.inheritedSchemas
631
+ .filter((schema) => this.shouldGenerateTypeDeclaration(ctx, { schema }) && !schema.isNameGenerated)
632
+ .filter((item, index, self) => self.indexOf(item) === index);
633
+ }
634
+
635
+ protected getClassProperties(ctx: Context, args: Args.GetClassProperties): ApiSchemaProperty[] {
636
+ const { schema } = args;
637
+
638
+ const inheritedSchemas = this.getInheritedSchemas(ctx, { schema });
639
+ const properties: ApiSchemaProperty[] = [];
640
+ const appendedProperties: ApiSchemaProperty[] = [];
641
+ for (const property of schema.properties.values()) {
642
+ const discriminator = inheritedSchemas.find(
643
+ (x) => x.discriminator?.propertyName === property.name,
644
+ )?.discriminator;
645
+ if (discriminator) {
646
+ const schemaMappings = Object.entries(discriminator.mapping).filter(([_, v]) => v.id === schema.id);
647
+ if (schemaMappings.length === 1) {
648
+ const p = createOverwriteProxy(property);
649
+ const s = createOverwriteProxy(p.schema);
650
+ p.schema = s;
651
+ s.default = schemaMappings[0][0];
652
+ appendedProperties.push(p);
653
+ continue;
654
+ }
655
+ }
656
+
657
+ properties.push(property);
658
+ }
659
+
660
+ return [...this.sortProperties(ctx, { schema, properties }), ...appendedProperties];
661
+ }
662
+
663
+ protected sortProperties(_ctx: Context, args: Args.SortProperties): ApiSchemaProperty[] {
664
+ return [...args.properties].sort((a, b) => classify(a) - classify(b));
665
+
666
+ function classify(p: ApiSchemaProperty) {
667
+ if (p.schema.default !== undefined) return 1;
668
+ if (args.schema.required.has(p.name)) return 0;
669
+ return 2;
670
+ }
671
+ }
672
+
673
+ protected normalizeSchema(ctx: Context, args: Args.NormalizeSchema): ApiSchema {
674
+ let { schema } = args;
675
+
676
+ if (schema.kind === 'oneOf') {
677
+ schema = ctx.config.oneOfBehavior === 'treat-as-any-of'
678
+ // deno-lint-ignore no-explicit-any
679
+ ? { ...(schema as any), kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }
680
+ // deno-lint-ignore no-explicit-any
681
+ : { ...(schema as any), kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined };
682
+ ctx.schema = schema;
683
+ }
684
+ if (schema.kind === 'object' || schema.kind === 'combined') {
685
+ const mergedSchema = resolveAnyOfAndAllOf(schema, true);
686
+ if (mergedSchema) {
687
+ schema = mergedSchema;
688
+ }
689
+ }
690
+
691
+ return schema;
692
+ }
693
+
694
+ protected hasProperty(ctx: Context, args: Args.HasProperty): boolean {
695
+ const { schema, propertyName } = args;
696
+
697
+ return (
698
+ ('properties' in schema && schema.properties.has(propertyName)) ||
699
+ ('anyOf' in schema && schema.anyOf.some((schema) => this.hasProperty(ctx, { schema, propertyName }))) ||
700
+ ('allOf' in schema && schema.allOf.some((schema) => this.hasProperty(ctx, { schema, propertyName })))
701
+ );
702
+ }
703
+ }