@goast/kotlin 0.1.1 → 0.1.3

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.
@@ -50,19 +50,7 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
50
50
  }
51
51
  }
52
52
  generateFileContent(ctx, builder) {
53
- let schema = ctx.schema;
54
- if (schema.kind === 'oneOf') {
55
- schema =
56
- ctx.config.oneOfBehavior === 'treat-as-any-of'
57
- ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
58
- ctx.schema = schema;
59
- }
60
- if (schema.kind === 'object' || schema.kind === 'combined') {
61
- const mergedSchema = (0, core_1.resolveAnyOfAndAllOf)(schema, true);
62
- if (mergedSchema) {
63
- schema = mergedSchema;
64
- }
65
- }
53
+ const schema = this.normalizeSchema(ctx, ctx.schema);
66
54
  if (schema.kind === 'object') {
67
55
  this.generateObjectType(ctx, builder, schema);
68
56
  }
@@ -369,6 +357,14 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
369
357
  schema.kind !== 'oneOf') {
370
358
  return false;
371
359
  }
360
+ // Too complex types cannot be represented in Kotlin, so they fallback to Any
361
+ if (schema.kind === 'multi-type') {
362
+ return false;
363
+ }
364
+ schema = this.normalizeSchema(ctx, schema);
365
+ if (schema.kind === 'combined' || schema.kind === 'oneOf') {
366
+ return false;
367
+ }
372
368
  // Only object types with properties should have its own type declaration
373
369
  if (schema.kind === 'object' &&
374
370
  schema.properties.size === 0 &&
@@ -388,6 +384,21 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
388
384
  return bRequired - aRequired;
389
385
  });
390
386
  }
387
+ normalizeSchema(ctx, schema) {
388
+ if (schema.kind === 'oneOf') {
389
+ schema =
390
+ ctx.config.oneOfBehavior === 'treat-as-any-of'
391
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
392
+ ctx.schema = schema;
393
+ }
394
+ if (schema.kind === 'object' || schema.kind === 'combined') {
395
+ const mergedSchema = (0, core_1.resolveAnyOfAndAllOf)(schema, true);
396
+ if (mergedSchema) {
397
+ schema = mergedSchema;
398
+ }
399
+ }
400
+ return schema;
401
+ }
391
402
  hasProperty(ctx, schema, name) {
392
403
  return (('properties' in schema && schema.properties.has(name)) ||
393
404
  ('anyOf' in schema && schema.anyOf.some((x) => this.hasProperty(ctx, x, name))) ||
@@ -47,19 +47,7 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
47
47
  }
48
48
  }
49
49
  generateFileContent(ctx, builder) {
50
- let schema = ctx.schema;
51
- if (schema.kind === 'oneOf') {
52
- schema =
53
- ctx.config.oneOfBehavior === 'treat-as-any-of'
54
- ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
55
- ctx.schema = schema;
56
- }
57
- if (schema.kind === 'object' || schema.kind === 'combined') {
58
- const mergedSchema = resolveAnyOfAndAllOf(schema, true);
59
- if (mergedSchema) {
60
- schema = mergedSchema;
61
- }
62
- }
50
+ const schema = this.normalizeSchema(ctx, ctx.schema);
63
51
  if (schema.kind === 'object') {
64
52
  this.generateObjectType(ctx, builder, schema);
65
53
  }
@@ -366,6 +354,14 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
366
354
  schema.kind !== 'oneOf') {
367
355
  return false;
368
356
  }
357
+ // Too complex types cannot be represented in Kotlin, so they fallback to Any
358
+ if (schema.kind === 'multi-type') {
359
+ return false;
360
+ }
361
+ schema = this.normalizeSchema(ctx, schema);
362
+ if (schema.kind === 'combined' || schema.kind === 'oneOf') {
363
+ return false;
364
+ }
369
365
  // Only object types with properties should have its own type declaration
370
366
  if (schema.kind === 'object' &&
371
367
  schema.properties.size === 0 &&
@@ -385,6 +381,21 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
385
381
  return bRequired - aRequired;
386
382
  });
387
383
  }
384
+ normalizeSchema(ctx, schema) {
385
+ if (schema.kind === 'oneOf') {
386
+ schema =
387
+ ctx.config.oneOfBehavior === 'treat-as-any-of'
388
+ ? Object.assign(Object.assign({}, schema), { kind: 'combined', anyOf: schema.oneOf, allOf: [], oneOf: undefined }) : Object.assign(Object.assign({}, schema), { kind: 'combined', allOf: schema.oneOf, anyOf: [], oneOf: undefined });
389
+ ctx.schema = schema;
390
+ }
391
+ if (schema.kind === 'object' || schema.kind === 'combined') {
392
+ const mergedSchema = resolveAnyOfAndAllOf(schema, true);
393
+ if (mergedSchema) {
394
+ schema = mergedSchema;
395
+ }
396
+ }
397
+ return schema;
398
+ }
388
399
  hasProperty(ctx, schema, name) {
389
400
  return (('properties' in schema && schema.properties.has(name)) ||
390
401
  ('anyOf' in schema && schema.anyOf.some((x) => this.hasProperty(ctx, x, name))) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/kotlin",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -33,6 +33,7 @@ export declare class DefaultKotlinModelGenerator extends KotlinFileGenerator<Con
33
33
  protected shouldGenerateTypeDeclaration(ctx: Context, schema: ApiSchema): boolean;
34
34
  protected getDeclarationTypeName(ctx: Context, schema: ApiSchema): string;
35
35
  protected sortProperties(ctx: Context, schema: ApiSchema, properties: Iterable<ApiSchemaProperty>): ApiSchemaProperty[];
36
+ private normalizeSchema;
36
37
  private hasProperty;
37
38
  }
38
39
  export {};