-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript generator: Use string type as the discriminator property t…
…ype in specialized interfaces (#1718) * improve TypeScript interface generation * TypeScript generator: Use string type as the discriminator property type in specialized interfaces * fix tests
- Loading branch information
Showing
8 changed files
with
322 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...criptDiscriminatorTests.When_generating_interface_contract_add_discriminator.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export interface Base { | ||
Type: EBase; | ||
} | ||
|
||
export enum EBase { | ||
OneChild = "OneChild", | ||
SecondChild = "SecondChild", | ||
} | ||
|
||
export interface OneChild extends Base { | ||
A: string; | ||
Type: EBase.OneChild; | ||
} | ||
|
||
export interface SecondChild extends Base { | ||
B: string; | ||
Type: EBase.SecondChild; | ||
} | ||
|
||
export interface MyClass { | ||
Child: Base; | ||
Children: Base[]; | ||
} |
31 changes: 31 additions & 0 deletions
31
...torTests.When_generating_interface_contract_add_discriminator_string_literal.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export interface Base { | ||
Type: EBase; | ||
} | ||
|
||
export type EBase = "OneChild" | "SecondChild"; | ||
|
||
export interface OneChild extends Base { | ||
A: string; | ||
Type: 'OneChild'; | ||
} | ||
|
||
export interface SecondChild extends Base { | ||
B: string; | ||
Type: 'SecondChild'; | ||
} | ||
|
||
export interface MyClass { | ||
Child: Base; | ||
Children: Base[]; | ||
} |
181 changes: 181 additions & 0 deletions
181
...criptDiscriminatorTests.When_parameter_is_abstract_then_generate_union_class.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export abstract class Base implements IBase { | ||
|
||
protected _discriminator: string; | ||
|
||
constructor(data?: IBase) { | ||
if (data) { | ||
for (var property in data) { | ||
if (data.hasOwnProperty(property)) | ||
(<any>this)[property] = (<any>data)[property]; | ||
} | ||
} | ||
this._discriminator = "Base"; | ||
} | ||
|
||
init(_data?: any) { | ||
} | ||
|
||
static fromJS(data: any): Base { | ||
data = typeof data === 'object' ? data : {}; | ||
if (data["Type"] === "OneChild") { | ||
let result = new OneChild(); | ||
result.init(data); | ||
return result; | ||
} | ||
if (data["Type"] === "SecondChild") { | ||
let result = new SecondChild(); | ||
result.init(data); | ||
return result; | ||
} | ||
throw new Error("The abstract class 'Base' cannot be instantiated."); | ||
} | ||
|
||
toJSON(data?: any) { | ||
data = typeof data === 'object' ? data : {}; | ||
data["Type"] = this._discriminator; | ||
return data; | ||
} | ||
} | ||
|
||
export interface IBase { | ||
} | ||
|
||
export class OneChild extends Base implements IOneChild { | ||
a: string; | ||
type: EBase; | ||
|
||
constructor(data?: IOneChild) { | ||
super(data); | ||
this._discriminator = "OneChild"; | ||
} | ||
|
||
init(_data?: any) { | ||
super.init(_data); | ||
if (_data) { | ||
this.a = _data["A"]; | ||
this.type = _data["Type"]; | ||
} | ||
} | ||
|
||
static fromJS(data: any): OneChild { | ||
data = typeof data === 'object' ? data : {}; | ||
let result = new OneChild(); | ||
result.init(data); | ||
return result; | ||
} | ||
|
||
toJSON(data?: any) { | ||
data = typeof data === 'object' ? data : {}; | ||
data["A"] = this.a; | ||
data["Type"] = this.type; | ||
super.toJSON(data); | ||
return data; | ||
} | ||
} | ||
|
||
export interface IOneChild extends IBase { | ||
a: string; | ||
type: EBase; | ||
} | ||
|
||
export enum EBase { | ||
OneChild = "OneChild", | ||
SecondChild = "SecondChild", | ||
} | ||
|
||
export class SecondChild extends Base implements ISecondChild { | ||
b: string; | ||
type: EBase; | ||
|
||
constructor(data?: ISecondChild) { | ||
super(data); | ||
this._discriminator = "SecondChild"; | ||
} | ||
|
||
init(_data?: any) { | ||
super.init(_data); | ||
if (_data) { | ||
this.b = _data["B"]; | ||
this.type = _data["Type"]; | ||
} | ||
} | ||
|
||
static fromJS(data: any): SecondChild { | ||
data = typeof data === 'object' ? data : {}; | ||
let result = new SecondChild(); | ||
result.init(data); | ||
return result; | ||
} | ||
|
||
toJSON(data?: any) { | ||
data = typeof data === 'object' ? data : {}; | ||
data["B"] = this.b; | ||
data["Type"] = this.type; | ||
super.toJSON(data); | ||
return data; | ||
} | ||
} | ||
|
||
export interface ISecondChild extends IBase { | ||
b: string; | ||
type: EBase; | ||
} | ||
|
||
export class MyClass implements IMyClass { | ||
child: OneChild | SecondChild; | ||
children: (OneChild | SecondChild)[]; | ||
|
||
constructor(data?: IMyClass) { | ||
if (data) { | ||
for (var property in data) { | ||
if (data.hasOwnProperty(property)) | ||
(<any>this)[property] = (<any>data)[property]; | ||
} | ||
} | ||
} | ||
|
||
init(_data?: any) { | ||
if (_data) { | ||
this.child = _data["Child"] ? OneChild | SecondChild.fromJS(_data["Child"]) : <any>undefined; | ||
if (Array.isArray(_data["Children"])) { | ||
this.children = [] as any; | ||
for (let item of _data["Children"]) | ||
this.children.push(OneChild | SecondChild.fromJS(item)); | ||
} | ||
} | ||
} | ||
|
||
static fromJS(data: any): MyClass { | ||
data = typeof data === 'object' ? data : {}; | ||
let result = new MyClass(); | ||
result.init(data); | ||
return result; | ||
} | ||
|
||
toJSON(data?: any) { | ||
data = typeof data === 'object' ? data : {}; | ||
data["Child"] = this.child ? this.child.toJSON() : <any>undefined; | ||
if (Array.isArray(this.children)) { | ||
data["Children"] = []; | ||
for (let item of this.children) | ||
data["Children"].push(item.toJSON()); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
export interface IMyClass { | ||
child: OneChild | SecondChild; | ||
children: (OneChild | SecondChild)[]; | ||
} |
34 changes: 34 additions & 0 deletions
34
...tDiscriminatorTests.When_parameter_is_abstract_then_generate_union_interface.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
export interface Base { | ||
Type: string; | ||
} | ||
|
||
export interface OneChild extends Base { | ||
A: string; | ||
Type: EBase.OneChild; | ||
} | ||
|
||
export enum EBase { | ||
OneChild = "OneChild", | ||
SecondChild = "SecondChild", | ||
} | ||
|
||
export interface SecondChild extends Base { | ||
B: string; | ||
Type: EBase.SecondChild; | ||
} | ||
|
||
export interface MyClass { | ||
Child: OneChild | SecondChild; | ||
Children: (OneChild | SecondChild)[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.