Skip to content

Commit cf275f3

Browse files
committed
Do not create duplicate required properties.
1 parent ca34f19 commit cf275f3

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Diff for: test/programs/type-intersection/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
interface Type1 {
22
value1: string;
3+
value2: number;
34
}
45
interface Type2 {
56
value2: number;
7+
value3: boolean;
68
}
79

810
interface MyObject {

Diff for: test/programs/type-intersection/schema.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
},
1111
"value2": {
1212
"type": "number"
13+
},
14+
"value3": {
15+
"type": "boolean"
1316
}
1417
},
1518
"required": [
1619
"value1",
17-
"value2"
20+
"value2",
21+
"value3"
1822
],
1923
"type": "object"
2024
}

Diff for: typescript-json-schema.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ function extend(target: any, ..._: any[]) {
9999
return to;
100100
}
101101

102+
function unique(arr: string[]): string[] {
103+
const temp = {};
104+
for (const e of arr) {
105+
temp[e] = true;
106+
}
107+
const r: string[] = [];
108+
for (const k in temp) {
109+
if (temp.hasOwnProperty(k)) {
110+
r.push(k);
111+
}
112+
}
113+
return r;
114+
}
115+
102116
export class JsonSchemaGenerator {
103117
/**
104118
* JSDoc keywords that should be used to annotate the JSON schema.
@@ -564,7 +578,7 @@ export class JsonSchemaGenerator {
564578
}, []);
565579

566580
if (requiredProps.length > 0) {
567-
definition.required = requiredProps.sort();
581+
definition.required = unique(requiredProps).sort();
568582
}
569583
}
570584
}
@@ -703,7 +717,7 @@ export class JsonSchemaGenerator {
703717
definition.default = extend(definition.default || {}, other.default);
704718
}
705719
if (other.required) {
706-
definition.required = (definition.required || []).concat(other.required);
720+
definition.required = unique((definition.required || []).concat(other.required)).sort();
707721
}
708722
}
709723
} else if (isRawType) {

0 commit comments

Comments
 (0)