@goast/kotlin 0.1.7 → 0.1.9

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.
@@ -149,7 +149,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
149
149
  .append((builder) => this.generateDocumentation(ctx, builder, schema))
150
150
  .append('data class ')
151
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 })
152
+ .parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(this.sortProperties(ctx, schema, params), (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true })
153
153
  .if(inheritedSchemas.length > 0, (builder) => builder
154
154
  .append(' : ')
155
155
  .forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
@@ -420,18 +420,18 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
420
420
  .filter((item, index, self) => self.indexOf(item) === index);
421
421
  }
422
422
  classifyClassProperties(ctx, schema) {
423
- var _a, _b;
423
+ var _a;
424
424
  const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
425
425
  const result = { params: [], properties: [] };
426
426
  for (const property of schema.properties.values()) {
427
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
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) {
429
+ const schemaMappings = Object.entries(discriminator.mapping).filter(([_, v]) => v.id === schema.id);
430
+ if (schemaMappings.length === 1) {
431
431
  const p = (0, core_1.createOverwriteProxy)(property);
432
432
  const s = (0, core_1.createOverwriteProxy)(p.schema);
433
433
  p.schema = s;
434
- s.default = value;
434
+ s.default = schemaMappings[0][0];
435
435
  result.properties.push(p);
436
436
  continue;
437
437
  }
@@ -441,11 +441,14 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
441
441
  return result;
442
442
  }
443
443
  sortProperties(ctx, schema, properties) {
444
- return [...properties].sort((a, b) => {
445
- const aRequired = schema.required.has(a.name) || schema.default ? 1 : 0;
446
- const bRequired = schema.required.has(b.name) || schema.default ? 1 : 0;
447
- return bRequired - aRequired;
448
- });
444
+ return [...properties].sort((a, b) => classify(a) - classify(b));
445
+ function classify(p) {
446
+ if (p.schema.default !== undefined)
447
+ return 1;
448
+ if (schema.required.has(p.name))
449
+ return 0;
450
+ return 2;
451
+ }
449
452
  }
450
453
  normalizeSchema(ctx, schema) {
451
454
  if (schema.kind === 'oneOf') {
@@ -146,7 +146,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
146
146
  .append((builder) => this.generateDocumentation(ctx, builder, schema))
147
147
  .append('data class ')
148
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 })
149
+ .parenthesizeIf(schema.properties.size > 0, '()', (builder) => builder.forEach(this.sortProperties(ctx, schema, params), (builder, property) => this.generateObjectDataClassProperty(ctx, builder, schema, inheritedSchemas, property), { separator: ',\n' }), { multiline: true })
150
150
  .if(inheritedSchemas.length > 0, (builder) => builder
151
151
  .append(' : ')
152
152
  .forEach(inheritedSchemas, (builder, schema) => this.generateTypeUsage(ctx, builder, schema), {
@@ -417,18 +417,18 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
417
417
  .filter((item, index, self) => self.indexOf(item) === index);
418
418
  }
419
419
  classifyClassProperties(ctx, schema) {
420
- var _a, _b;
420
+ var _a;
421
421
  const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
422
422
  const result = { params: [], properties: [] };
423
423
  for (const property of schema.properties.values()) {
424
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
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) {
426
+ const schemaMappings = Object.entries(discriminator.mapping).filter(([_, v]) => v.id === schema.id);
427
+ if (schemaMappings.length === 1) {
428
428
  const p = createOverwriteProxy(property);
429
429
  const s = createOverwriteProxy(p.schema);
430
430
  p.schema = s;
431
- s.default = value;
431
+ s.default = schemaMappings[0][0];
432
432
  result.properties.push(p);
433
433
  continue;
434
434
  }
@@ -438,11 +438,14 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
438
438
  return result;
439
439
  }
440
440
  sortProperties(ctx, schema, properties) {
441
- return [...properties].sort((a, b) => {
442
- const aRequired = schema.required.has(a.name) || schema.default ? 1 : 0;
443
- const bRequired = schema.required.has(b.name) || schema.default ? 1 : 0;
444
- return bRequired - aRequired;
445
- });
441
+ return [...properties].sort((a, b) => classify(a) - classify(b));
442
+ function classify(p) {
443
+ if (p.schema.default !== undefined)
444
+ return 1;
445
+ if (schema.required.has(p.name))
446
+ return 0;
447
+ return 2;
448
+ }
446
449
  }
447
450
  normalizeSchema(ctx, schema) {
448
451
  if (schema.kind === 'oneOf') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/kotlin",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",