Skip to content

Commit 1638186

Browse files
authored
fix: schema with properties and oneOf properties (#491)
1 parent 494fa56 commit 1638186

File tree

5 files changed

+12785
-5386
lines changed

5 files changed

+12785
-5386
lines changed

src/transform/schema.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ export function transformSchemaObj(node: any): string {
7777
break;
7878
}
7979
case "object": {
80+
const isAnyOfOrOneOfOrAllOf = "anyOf" in node || "oneOf" in node || "allOf" in node;
81+
8082
// if empty object, then return generic map type
81-
if ((!node.properties || !Object.keys(node.properties).length) && !node.allOf && !node.additionalProperties) {
83+
if (
84+
!isAnyOfOrOneOfOrAllOf &&
85+
(!node.properties || !Object.keys(node.properties).length) &&
86+
!node.additionalProperties
87+
) {
8288
output += `{ [key: string]: any }`;
8389
break;
8490
}
@@ -104,10 +110,14 @@ export function transformSchemaObj(node: any): string {
104110
}
105111

106112
output += tsIntersectionOf([
107-
...(node.allOf ? (node.allOf as any[]).map(transformSchemaObj) : []), // append allOf first
113+
// append allOf/anyOf/oneOf first
114+
...(node.allOf ? (node.allOf as any[]).map(transformSchemaObj) : []),
115+
...(node.anyOf ? [transformAnyOf(node.anyOf)] : []),
116+
...(node.oneOf ? [transformOneOf(node.oneOf)] : []),
108117
...(properties ? [`{\n${properties}\n}`] : []), // then properties (line breaks are important!)
109118
...(additionalProperties ? [additionalProperties] : []), // then additional properties
110119
]);
120+
111121
break;
112122
}
113123

@@ -119,16 +129,6 @@ export function transformSchemaObj(node: any): string {
119129
}
120130
break;
121131
}
122-
123-
case "anyOf": {
124-
output += transformAnyOf(node.anyOf);
125-
break;
126-
}
127-
128-
case "oneOf": {
129-
output += transformOneOf(node.oneOf);
130-
break;
131-
}
132132
}
133133

134134
// close nullable

src/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
5555
return "number";
5656
}
5757

58-
// anyOf
59-
if (Array.isArray(obj.anyOf)) {
60-
return "anyOf";
61-
}
62-
63-
// oneOf
64-
if (Array.isArray(obj.oneOf)) {
65-
return "oneOf";
66-
}
67-
6858
// array
6959
if (obj.type === "array" || obj.items) {
7060
return "array";

tests/schema.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,57 @@ describe("SchemaObject", () => {
213213
})
214214
).toBe(`{ [key: string]: (string) | (number) | (boolean); }`);
215215
});
216+
217+
// https://www.jsonschemavalidator.net/s/fOyR2UtQ
218+
it("properties + oneOf", () => {
219+
expect(
220+
transform({
221+
properties: {
222+
a: {
223+
type: "string",
224+
},
225+
},
226+
oneOf: [
227+
{ properties: { b: { type: "string" } }, required: ["b"] },
228+
{ properties: { c: { type: "string" } }, required: ["c"] },
229+
],
230+
})
231+
).toBe(`(({
232+
"b": string;
233+
234+
}) | ({
235+
"c": string;
236+
237+
})) & ({
238+
"a"?: string;
239+
240+
})`);
241+
});
242+
243+
it("properties + anyOf", () => {
244+
expect(
245+
transform({
246+
properties: {
247+
a: {
248+
type: "string",
249+
},
250+
},
251+
anyOf: [
252+
{ properties: { b: { type: "string" } }, required: ["b"] },
253+
{ properties: { c: { type: "string" } }, required: ["c"] },
254+
],
255+
})
256+
).toBe(`((Partial<{
257+
"b": string;
258+
259+
}>) & (Partial<{
260+
"c": string;
261+
262+
}>)) & ({
263+
"a"?: string;
264+
265+
})`);
266+
});
216267
});
217268

218269
describe("comments", () => {

0 commit comments

Comments
 (0)