Skip to content

Commit cbb6bed

Browse files
authored
fix: typescript should never render duplicate types (#354)
1 parent 253462b commit cbb6bed

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

src/generators/typescript/TypeScriptRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export abstract class TypeScriptRenderer extends AbstractRenderer<TypeScriptOpti
5959
return this.nameType(model.$ref);
6060
}
6161
if (Array.isArray(model.type)) {
62-
return model.type.map(t => this.toTsType(t, model)).join(' | ');
62+
return [... new Set(model.type.map(t => this.toTsType(t, model)))].join(' | ');
6363
}
6464
return this.toTsType(model.type, model);
6565
}

test/generators/typescript/TypeScriptGenerator.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ describe('TypeScriptGenerator', () => {
7575
private _marriage?: boolean;
7676
private _members?: string | number | boolean;
7777
private _tupleType?: [string, number];
78-
private _tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null | number)[]];
78+
private _tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null)[]];
7979
private _arrayType: Array<string>;
80-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
80+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
8181
private _sTestPatternProperties?: Map<String, string>;
8282
8383
constructor(input: {
@@ -88,7 +88,7 @@ describe('TypeScriptGenerator', () => {
8888
marriage?: boolean,
8989
members?: string | number | boolean,
9090
tupleType?: [string, number],
91-
tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null | number)[]],
91+
tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null)[]],
9292
arrayType: Array<string>,
9393
}) {
9494
this._streetName = input.streetName;
@@ -123,14 +123,14 @@ describe('TypeScriptGenerator', () => {
123123
get tupleType(): [string, number] | undefined { return this._tupleType; }
124124
set tupleType(tupleType: [string, number] | undefined) { this._tupleType = tupleType; }
125125
126-
get tupleTypeWithAdditionalItems(): [string, number, ...(object | string | number | Array<unknown> | boolean | null | number)[]] | undefined { return this._tupleTypeWithAdditionalItems; }
127-
set tupleTypeWithAdditionalItems(tupleTypeWithAdditionalItems: [string, number, ...(object | string | number | Array<unknown> | boolean | null | number)[]] | undefined) { this._tupleTypeWithAdditionalItems = tupleTypeWithAdditionalItems; }
126+
get tupleTypeWithAdditionalItems(): [string, number, ...(object | string | number | Array<unknown> | boolean | null)[]] | undefined { return this._tupleTypeWithAdditionalItems; }
127+
set tupleTypeWithAdditionalItems(tupleTypeWithAdditionalItems: [string, number, ...(object | string | number | Array<unknown> | boolean | null)[]] | undefined) { this._tupleTypeWithAdditionalItems = tupleTypeWithAdditionalItems; }
128128
129129
get arrayType(): Array<string> { return this._arrayType; }
130130
set arrayType(arrayType: Array<string>) { this._arrayType = arrayType; }
131131
132-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
133-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
132+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
133+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
134134
135135
get sTestPatternProperties(): Map<String, string> | undefined { return this._sTestPatternProperties; }
136136
set sTestPatternProperties(sTestPatternProperties: Map<String, string> | undefined) { this._sTestPatternProperties = sTestPatternProperties; }
@@ -160,7 +160,7 @@ describe('TypeScriptGenerator', () => {
160160
@JsonProperty("property")
161161
private _property?: string;
162162
@JsonProperty("additionalProperties")
163-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
163+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
164164
165165
constructor(input: {
166166
property?: string,
@@ -171,8 +171,8 @@ describe('TypeScriptGenerator', () => {
171171
get property(): string | undefined { return this._property; }
172172
set property(property: string | undefined) { this._property = property; }
173173
174-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
175-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
174+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
175+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
176176
}`;
177177

178178
generator = new TypeScriptGenerator({ presets: [
@@ -224,9 +224,9 @@ ${content}`;
224224
marriage?: boolean;
225225
members?: string | number | boolean;
226226
tupleType?: [string, number];
227-
tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null | number)[]];
227+
tupleTypeWithAdditionalItems?: [string, number, ...(object | string | number | Array<unknown> | boolean | null)[]];
228228
arrayType: Array<string>;
229-
additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
229+
additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
230230
sTestPatternProperties?: Map<String, string>;
231231
}`;
232232

@@ -249,7 +249,7 @@ ${content}`;
249249
};
250250
const expected = `export interface CustomInterface {
251251
property?: string;
252-
additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
252+
additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
253253
}`;
254254

255255
generator = new TypeScriptGenerator({ presets: [

test/generators/typescript/TypeScriptRenderer.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ describe('TypeScriptRenderer', () => {
4141
});
4242

4343
describe('toTsType()', () => {
44+
test('should not render duplicate types', () => {
45+
expect(renderer.toTsType(undefined, new CommonModel())).toEqual('any');
46+
});
4447
test('Should render unknown type', () => {
4548
expect(renderer.toTsType(undefined, new CommonModel())).toEqual('any');
4649
});
4750
test('Should render number type', () => {
48-
expect(renderer.toTsType('integer', new CommonModel())).toEqual('number');
4951
expect(renderer.toTsType('number', new CommonModel())).toEqual('number');
5052
});
5153
test('Should render array type', () => {
@@ -88,5 +90,10 @@ describe('TypeScriptRenderer', () => {
8890
model.enum = ['enum1', 'enum2', 9];
8991
expect(renderer.renderType(model)).toEqual('"enum1" | "enum2" | 9');
9092
});
93+
test('should not render duplicate types', () => {
94+
const model = new CommonModel();
95+
model.type = ['integer', 'number'];
96+
expect(renderer.renderType(model)).toEqual('number');
97+
});
9198
});
9299
});

test/generators/typescript/preset/MarshallingPreset.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Marshalling preset', () => {
5656
}
5757
class NestedTest {
5858
private _stringProp?: string;
59-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
59+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
6060

6161
constructor(input: {
6262
stringProp?: string,
@@ -67,8 +67,8 @@ describe('Marshalling preset', () => {
6767
get stringProp(): string | undefined { return this._stringProp; }
6868
set stringProp(stringProp: string | undefined) { this._stringProp = stringProp; }
6969

70-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
71-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
70+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
71+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
7272

7373
public marshal() : string {
7474
let json = '{'

test/generators/typescript/preset/__snapshots__/ExamplePreset.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`Example function generation should render example function for model 1`
55
private _stringProp: string;
66
private _numberProp?: number;
77
private _objectProp?: NestedTest;
8-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
8+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
99
private _sTestPatternProperties?: Map<String, string>;
1010
1111
constructor(input: {
@@ -27,8 +27,8 @@ exports[`Example function generation should render example function for model 1`
2727
get objectProp(): NestedTest | undefined { return this._objectProp; }
2828
set objectProp(objectProp: NestedTest | undefined) { this._objectProp = objectProp; }
2929
30-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
31-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
30+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
31+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
3232
3333
get sTestPatternProperties(): Map<String, string> | undefined { return this._sTestPatternProperties; }
3434
set sTestPatternProperties(sTestPatternProperties: Map<String, string> | undefined) { this._sTestPatternProperties = sTestPatternProperties; }
@@ -46,7 +46,7 @@ exports[`Example function generation should render example function for model 1`
4646
exports[`Example function generation should render example function for model 2`] = `
4747
"export class NestedTest {
4848
private _stringProp?: string;
49-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
49+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
5050
5151
constructor(input: {
5252
stringProp?: string,
@@ -57,8 +57,8 @@ exports[`Example function generation should render example function for model 2`
5757
get stringProp(): string | undefined { return this._stringProp; }
5858
set stringProp(stringProp: string | undefined) { this._stringProp = stringProp; }
5959
60-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
61-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
60+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
61+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
6262
6363
public static example(): NestedTest {
6464
const instance = new NestedTest({} as any);

test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
124124
exports[`Marshalling preset should render un/marshal code 2`] = `
125125
"export class NestedTest {
126126
private _stringProp?: string;
127-
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
127+
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;
128128
129129
constructor(input: {
130130
stringProp?: string,
@@ -135,8 +135,8 @@ exports[`Marshalling preset should render un/marshal code 2`] = `
135135
get stringProp(): string | undefined { return this._stringProp; }
136136
set stringProp(stringProp: string | undefined) { this._stringProp = stringProp; }
137137
138-
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
139-
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
138+
get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
139+
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }
140140
141141
public marshal() : string {
142142
let json = '{'

0 commit comments

Comments
 (0)