@goast/typescript 0.2.8 → 0.2.10

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.
@@ -64,18 +64,39 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
64
64
  }
65
65
  getTypeAlias(ctx, schema) {
66
66
  var _a;
67
- const isDiscriminated = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
68
- const result = ast_1.ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (isDiscriminated ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, { skipSchemas: true }))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
67
+ const hasDiscriminator = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
68
+ const result = ast_1.ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (hasDiscriminator ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, { skipSchemas: true }))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
69
69
  export: true,
70
70
  doc: ast_1.ts.doc({ description: schema.description }),
71
71
  });
72
- if (isDiscriminated) {
72
+ if (hasDiscriminator) {
73
73
  const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
74
74
  result.generics.push(ast_1.ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
75
75
  constraint: propertyValue,
76
76
  default: propertyValue,
77
77
  }));
78
78
  }
79
+ const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
80
+ if (inheritedSchemas.length > 0) {
81
+ const fixedProperties = inheritedSchemas.reduce((p, c) => {
82
+ const mappingValues = Object.entries(c.discriminator.mapping)
83
+ .filter(([_, value]) => value.id === schema.id)
84
+ .map(([key]) => ast_1.ts.string(key));
85
+ if (c.discriminator.propertyName in p) {
86
+ p[c.discriminator.propertyName].types.push(...mappingValues);
87
+ }
88
+ else {
89
+ p[c.discriminator.propertyName] = ast_1.ts.unionType(mappingValues);
90
+ }
91
+ return p;
92
+ }, {});
93
+ result.type = ast_1.ts.intersectionType([
94
+ ast_1.ts.refs.omit([result.type, ast_1.ts.unionType(Object.keys(fixedProperties).map((x) => ast_1.ts.string(x)))]),
95
+ ast_1.ts.objectType({
96
+ members: Object.entries(fixedProperties).map(([key, value]) => ast_1.ts.property(key, { type: value, readonly: ctx.config.immutableTypes })),
97
+ }),
98
+ ]);
99
+ }
79
100
  return result;
80
101
  }
81
102
  getIndexer(ctx, schema) {
@@ -61,18 +61,39 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
61
61
  }
62
62
  getTypeAlias(ctx, schema) {
63
63
  var _a;
64
- const isDiscriminated = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
65
- const result = ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (isDiscriminated ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, { skipSchemas: true }))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
64
+ const hasDiscriminator = schema.discriminator && Object.keys(schema.discriminator.mapping).length > 0;
65
+ const result = ts.typeAlias(this.getDeclarationTypeName(ctx, schema), (_a = (hasDiscriminator ? this.getDiscriminatorType(ctx, schema) : this.getType(ctx, schema, { skipSchemas: true }))) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
66
66
  export: true,
67
67
  doc: ts.doc({ description: schema.description }),
68
68
  });
69
- if (isDiscriminated) {
69
+ if (hasDiscriminator) {
70
70
  const propertyValue = this.getDiscriminatorTypeName(ctx, ctx.schema);
71
71
  result.generics.push(ts.genericParameter(this.getDiscriminatorGenericParamName(ctx, schema), {
72
72
  constraint: propertyValue,
73
73
  default: propertyValue,
74
74
  }));
75
75
  }
76
+ const inheritedSchemas = this.getInheritedSchemas(ctx, schema);
77
+ if (inheritedSchemas.length > 0) {
78
+ const fixedProperties = inheritedSchemas.reduce((p, c) => {
79
+ const mappingValues = Object.entries(c.discriminator.mapping)
80
+ .filter(([_, value]) => value.id === schema.id)
81
+ .map(([key]) => ts.string(key));
82
+ if (c.discriminator.propertyName in p) {
83
+ p[c.discriminator.propertyName].types.push(...mappingValues);
84
+ }
85
+ else {
86
+ p[c.discriminator.propertyName] = ts.unionType(mappingValues);
87
+ }
88
+ return p;
89
+ }, {});
90
+ result.type = ts.intersectionType([
91
+ ts.refs.omit([result.type, ts.unionType(Object.keys(fixedProperties).map((x) => ts.string(x)))]),
92
+ ts.objectType({
93
+ members: Object.entries(fixedProperties).map(([key, value]) => ts.property(key, { type: value, readonly: ctx.config.immutableTypes })),
94
+ }),
95
+ ]);
96
+ }
76
97
  return result;
77
98
  }
78
99
  getIndexer(ctx, schema) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",