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