@goast/kotlin 0.4.18-springwebclient1 → 0.4.19

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