@goast/kotlin 0.1.6 → 0.1.7
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.
|
@@ -132,29 +132,32 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
132
132
|
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
133
133
|
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')));
|
|
134
134
|
}
|
|
135
|
-
|
|
136
|
-
const inheritedSchemas = schema.inheritedSchemas
|
|
137
|
-
.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated)
|
|
138
|
-
.filter((item, index, self) => self.indexOf(item) === index);
|
|
135
|
+
generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property) {
|
|
139
136
|
builder
|
|
140
|
-
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
141
|
-
.append('data class ')
|
|
142
|
-
.append(this.getDeclarationTypeName(ctx, schema))
|
|
143
|
-
.parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(this.sortProperties(ctx, schema, schema.properties.values()), (builder, property) => builder
|
|
144
137
|
.ensurePreviousLineEmpty()
|
|
145
138
|
.append((builder) => this.generateObjectDataClassParameterAnnotations(ctx, builder, schema, property))
|
|
146
139
|
.appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
|
|
147
140
|
.append(`val ${(0, core_1.toCasing)(property.name, 'camel')}: `)
|
|
148
141
|
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
149
142
|
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?'))
|
|
150
|
-
.appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema))
|
|
143
|
+
.appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema));
|
|
144
|
+
}
|
|
145
|
+
generateObjectDataClass(ctx, builder, schema) {
|
|
146
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
|
|
147
|
+
const { params, properties } = this.classifyClassProperties(ctx, schema);
|
|
148
|
+
builder
|
|
149
|
+
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
150
|
+
.append('data class ')
|
|
151
|
+
.append(this.getDeclarationTypeName(ctx, schema))
|
|
152
|
+
.parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(params, (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true })
|
|
151
153
|
.if(inheritedSchemas.length > 0, (builder) => builder
|
|
152
154
|
.append(' : ')
|
|
153
155
|
.forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
|
|
154
156
|
separator: ', ',
|
|
155
157
|
}))
|
|
156
158
|
.append(' ')
|
|
157
|
-
.parenthesizeIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false
|
|
159
|
+
.parenthesizeIf((schema.additionalProperties !== undefined && schema.additionalProperties !== false) || properties.length > 0, '{}', (builder) => builder
|
|
160
|
+
.if(schema.additionalProperties !== undefined && schema.additionalProperties !== false, (builder) => builder
|
|
158
161
|
.if(ctx.config.addJacksonAnnotations, (builder) => builder.appendLine('@JsonIgnore').addImport('JsonIgnore', 'com.fasterxml.jackson.annotation'))
|
|
159
162
|
.append('val additionalProperties: Mutable')
|
|
160
163
|
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
@@ -171,7 +174,8 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
171
174
|
.append('fun getMap(): ')
|
|
172
175
|
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
173
176
|
.append(' ')
|
|
174
|
-
.parenthesize('{}', 'return this.additionalProperties', { multiline: true }))
|
|
177
|
+
.parenthesize('{}', 'return this.additionalProperties', { multiline: true }))
|
|
178
|
+
.forEach(properties, (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true });
|
|
175
179
|
}
|
|
176
180
|
generateDefaultValue(ctx, builder, schema) {
|
|
177
181
|
if (schema.default === null || schema.default === undefined) {
|
|
@@ -410,10 +414,36 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
|
|
|
410
414
|
getDeclarationTypeName(ctx, schema) {
|
|
411
415
|
return (0, core_1.toCasing)(schema.name, 'pascal');
|
|
412
416
|
}
|
|
417
|
+
getInheritedSchemas(ctx, schema) {
|
|
418
|
+
return schema.inheritedSchemas
|
|
419
|
+
.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated)
|
|
420
|
+
.filter((item, index, self) => self.indexOf(item) === index);
|
|
421
|
+
}
|
|
422
|
+
classifyClassProperties(ctx, schema) {
|
|
423
|
+
var _a, _b;
|
|
424
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
|
|
425
|
+
const result = { params: [], properties: [] };
|
|
426
|
+
for (const property of schema.properties.values()) {
|
|
427
|
+
const discriminator = (_a = inheritedSchemas.find((x) => { var _a; return ((_a = x.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName) === property.name; })) === null || _a === void 0 ? void 0 : _a.discriminator;
|
|
428
|
+
if (discriminator) {
|
|
429
|
+
const value = (_b = Object.entries(discriminator.mapping).find(([_, v]) => v.id === schema.id)) === null || _b === void 0 ? void 0 : _b[0];
|
|
430
|
+
if (value) {
|
|
431
|
+
const p = (0, core_1.createOverwriteProxy)(property);
|
|
432
|
+
const s = (0, core_1.createOverwriteProxy)(p.schema);
|
|
433
|
+
p.schema = s;
|
|
434
|
+
s.default = value;
|
|
435
|
+
result.properties.push(p);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
result.params.push(property);
|
|
440
|
+
}
|
|
441
|
+
return result;
|
|
442
|
+
}
|
|
413
443
|
sortProperties(ctx, schema, properties) {
|
|
414
444
|
return [...properties].sort((a, b) => {
|
|
415
|
-
const aRequired = schema.required.has(a.name) ? 1 : 0;
|
|
416
|
-
const bRequired = schema.required.has(b.name) ? 1 : 0;
|
|
445
|
+
const aRequired = schema.required.has(a.name) || schema.default ? 1 : 0;
|
|
446
|
+
const bRequired = schema.required.has(b.name) || schema.default ? 1 : 0;
|
|
417
447
|
return bRequired - aRequired;
|
|
418
448
|
});
|
|
419
449
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { dirname } from 'path';
|
|
2
2
|
import { ensureDirSync, writeFileSync } from 'fs-extra';
|
|
3
|
-
import { getSchemaReference, resolveAnyOfAndAllOf, toCasing, } from '@goast/core';
|
|
3
|
+
import { createOverwriteProxy, getSchemaReference, resolveAnyOfAndAllOf, toCasing, } from '@goast/core';
|
|
4
4
|
import { KotlinFileBuilder } from '../../file-builder';
|
|
5
5
|
import { KotlinFileGenerator } from '../file-generator';
|
|
6
6
|
export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
@@ -129,29 +129,32 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
129
129
|
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
130
130
|
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?').append(' = null')));
|
|
131
131
|
}
|
|
132
|
-
|
|
133
|
-
const inheritedSchemas = schema.inheritedSchemas
|
|
134
|
-
.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated)
|
|
135
|
-
.filter((item, index, self) => self.indexOf(item) === index);
|
|
132
|
+
generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property) {
|
|
136
133
|
builder
|
|
137
|
-
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
138
|
-
.append('data class ')
|
|
139
|
-
.append(this.getDeclarationTypeName(ctx, schema))
|
|
140
|
-
.parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(this.sortProperties(ctx, schema, schema.properties.values()), (builder, property) => builder
|
|
141
134
|
.ensurePreviousLineEmpty()
|
|
142
135
|
.append((builder) => this.generateObjectDataClassParameterAnnotations(ctx, builder, schema, property))
|
|
143
136
|
.appendIf(inheritedSchemas.some((x) => this.hasProperty(ctx, x, property.name)), 'override ')
|
|
144
137
|
.append(`val ${toCasing(property.name, 'camel')}: `)
|
|
145
138
|
.append((builder) => this.generateTypeUsage(ctx, builder, property.schema))
|
|
146
139
|
.if(!schema.required.has(property.name), (builder) => builder.appendIf(!property.schema.nullable, '?'))
|
|
147
|
-
.appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema))
|
|
140
|
+
.appendIf(property.schema.default !== undefined || !schema.required.has(property.name), ' = ', (builder) => this.generateDefaultValue(ctx, builder, property.schema));
|
|
141
|
+
}
|
|
142
|
+
generateObjectDataClass(ctx, builder, schema) {
|
|
143
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
|
|
144
|
+
const { params, properties } = this.classifyClassProperties(ctx, schema);
|
|
145
|
+
builder
|
|
146
|
+
.append((builder) => this.generateDocumentation(ctx, builder, schema))
|
|
147
|
+
.append('data class ')
|
|
148
|
+
.append(this.getDeclarationTypeName(ctx, schema))
|
|
149
|
+
.parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(params, (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true })
|
|
148
150
|
.if(inheritedSchemas.length > 0, (builder) => builder
|
|
149
151
|
.append(' : ')
|
|
150
152
|
.forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
|
|
151
153
|
separator: ', ',
|
|
152
154
|
}))
|
|
153
155
|
.append(' ')
|
|
154
|
-
.parenthesizeIf(schema.additionalProperties !== undefined && schema.additionalProperties !== false
|
|
156
|
+
.parenthesizeIf((schema.additionalProperties !== undefined && schema.additionalProperties !== false) || properties.length > 0, '{}', (builder) => builder
|
|
157
|
+
.if(schema.additionalProperties !== undefined && schema.additionalProperties !== false, (builder) => builder
|
|
155
158
|
.if(ctx.config.addJacksonAnnotations, (builder) => builder.appendLine('@JsonIgnore').addImport('JsonIgnore', 'com.fasterxml.jackson.annotation'))
|
|
156
159
|
.append('val additionalProperties: Mutable')
|
|
157
160
|
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
@@ -168,7 +171,8 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
168
171
|
.append('fun getMap(): ')
|
|
169
172
|
.append((builder) => this.generateMapType(ctx, builder, schema))
|
|
170
173
|
.append(' ')
|
|
171
|
-
.parenthesize('{}', 'return this.additionalProperties', { multiline: true }))
|
|
174
|
+
.parenthesize('{}', 'return this.additionalProperties', { multiline: true }))
|
|
175
|
+
.forEach(properties, (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true });
|
|
172
176
|
}
|
|
173
177
|
generateDefaultValue(ctx, builder, schema) {
|
|
174
178
|
if (schema.default === null || schema.default === undefined) {
|
|
@@ -407,10 +411,36 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
|
|
|
407
411
|
getDeclarationTypeName(ctx, schema) {
|
|
408
412
|
return toCasing(schema.name, 'pascal');
|
|
409
413
|
}
|
|
414
|
+
getInheritedSchemas(ctx, schema) {
|
|
415
|
+
return schema.inheritedSchemas
|
|
416
|
+
.filter((x) => this.shouldGenerateTypeDeclaration(ctx, x) && !x.isNameGenerated)
|
|
417
|
+
.filter((item, index, self) => self.indexOf(item) === index);
|
|
418
|
+
}
|
|
419
|
+
classifyClassProperties(ctx, schema) {
|
|
420
|
+
var _a, _b;
|
|
421
|
+
const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
|
|
422
|
+
const result = { params: [], properties: [] };
|
|
423
|
+
for (const property of schema.properties.values()) {
|
|
424
|
+
const discriminator = (_a = inheritedSchemas.find((x) => { var _a; return ((_a = x.discriminator) === null || _a === void 0 ? void 0 : _a.propertyName) === property.name; })) === null || _a === void 0 ? void 0 : _a.discriminator;
|
|
425
|
+
if (discriminator) {
|
|
426
|
+
const value = (_b = Object.entries(discriminator.mapping).find(([_, v]) => v.id === schema.id)) === null || _b === void 0 ? void 0 : _b[0];
|
|
427
|
+
if (value) {
|
|
428
|
+
const p = createOverwriteProxy(property);
|
|
429
|
+
const s = createOverwriteProxy(p.schema);
|
|
430
|
+
p.schema = s;
|
|
431
|
+
s.default = value;
|
|
432
|
+
result.properties.push(p);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
result.params.push(property);
|
|
437
|
+
}
|
|
438
|
+
return result;
|
|
439
|
+
}
|
|
410
440
|
sortProperties(ctx, schema, properties) {
|
|
411
441
|
return [...properties].sort((a, b) => {
|
|
412
|
-
const aRequired = schema.required.has(a.name) ? 1 : 0;
|
|
413
|
-
const bRequired = schema.required.has(b.name) ? 1 : 0;
|
|
442
|
+
const aRequired = schema.required.has(a.name) || schema.default ? 1 : 0;
|
|
443
|
+
const bRequired = schema.required.has(b.name) || schema.default ? 1 : 0;
|
|
414
444
|
return bRequired - aRequired;
|
|
415
445
|
});
|
|
416
446
|
}
|
package/package.json
CHANGED
|
@@ -18,6 +18,7 @@ export declare class DefaultKotlinModelGenerator extends KotlinFileGenerator<Con
|
|
|
18
18
|
protected generateObjectInterfaceAnnotations(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
|
|
19
19
|
protected generateObjectInterfaceSignature(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
|
|
20
20
|
protected generateObjectInterfaceMembers(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
|
|
21
|
+
protected generateObjectDataClassProperty(ctx: Context, builder: Builder, schema: ApiSchema<'object'>, inheritedSchemas: ApiSchema[], property: ApiSchemaProperty): void;
|
|
21
22
|
protected generateObjectDataClass(ctx: Context, builder: Builder, schema: ApiSchema<'object'>): void;
|
|
22
23
|
protected generateDefaultValue(ctx: Context, builder: Builder, schema: ApiSchema): void;
|
|
23
24
|
protected generateObjectDataClassParameterAnnotations(ctx: Context, builder: Builder, schema: ApiSchema, property: ApiSchemaProperty): void;
|
|
@@ -33,6 +34,11 @@ export declare class DefaultKotlinModelGenerator extends KotlinFileGenerator<Con
|
|
|
33
34
|
protected generateDocumentation(ctx: Context, builder: Builder, schema: ApiSchema): void;
|
|
34
35
|
protected shouldGenerateTypeDeclaration(ctx: Context, schema: ApiSchema): boolean;
|
|
35
36
|
protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
|
|
37
|
+
protected getInheritedSchemas(ctx: KotlinModelGeneratorContext, schema: ApiSchema): ApiSchema<import("@goast/core").ApiSchemaKind>[];
|
|
38
|
+
protected classifyClassProperties(ctx: Context, schema: ApiSchema<'object'>): {
|
|
39
|
+
params: ApiSchemaProperty[];
|
|
40
|
+
properties: ApiSchemaProperty[];
|
|
41
|
+
};
|
|
36
42
|
protected sortProperties(ctx: Context, schema: ApiSchema, properties: Iterable<ApiSchemaProperty>): ApiSchemaProperty[];
|
|
37
43
|
private normalizeSchema;
|
|
38
44
|
private hasProperty;
|