Skip to content

Commit dc06dc9

Browse files
authored
Support Type Arrays for OpenAPI 3.1 (#917)
* recursive types * example * tests for nullable as type array
1 parent 704954e commit dc06dc9

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/transform/schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,15 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
113113
} else {
114114
// transform core type
115115
switch (nodeType(node)) {
116+
case "type-array":
117+
// This is an array of types as of the 3.1 specification - we should recursively evaluate them
118+
output += tsUnionOf((node.type as any[]).map((type) => transformSchemaObj({ ...node, type }, options)));
119+
break;
116120
case "ref": {
117121
output += node.$ref; // these were transformed at load time when remote schemas were resolved; return as-is
118122
break;
119123
}
124+
case "null":
120125
case "string":
121126
case "number":
122127
case "boolean":

src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ type SchemaObjectType =
130130
| "object"
131131
| "oneOf"
132132
| "ref"
133+
| "null"
133134
| "string"
135+
// Special type so the parent function knows to recursively evaluate each entry
136+
| "type-array"
134137
| "unknown";
135138
export function nodeType(obj: any): SchemaObjectType {
136139
if (!obj || typeof obj !== "object") {
@@ -156,11 +159,20 @@ export function nodeType(obj: any): SchemaObjectType {
156159
return "object";
157160
}
158161

162+
// Type array from the 3.1 specification
163+
if (Array.isArray(obj.type)) {
164+
return "type-array";
165+
}
166+
159167
// boolean
160168
if (obj.type === "boolean") {
161169
return "boolean";
162170
}
163171

172+
// null
173+
if (obj.type === "null") {
174+
return "null";
175+
}
164176
// string
165177
if (
166178
obj.type === "string" ||

test/core/schema.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ describe("SchemaObject", () => {
256256
});
257257
});
258258

259+
describe("(3.1) type arrays", () => {
260+
it("nullable type array", () => {
261+
expect(transform({ type: ["string", "null"] }, { ...defaults })).to.equal("(string) | (null)");
262+
});
263+
264+
it("nullable type array within an object", () => {
265+
const objectWithNullableTypeArray = {
266+
type: "object",
267+
properties: {
268+
object: {
269+
properties: { nullableNumber: { type: ["number", "null"] }, string: { type: "string" } },
270+
type: "object",
271+
},
272+
},
273+
};
274+
expect(transform(objectWithNullableTypeArray, { ...defaults })).to.equal(
275+
`{\n"object"?: {\n"nullableNumber"?: (number) | (null);\n"string"?: string;\n\n};\n\n}`
276+
);
277+
});
278+
});
279+
259280
describe("advanced", () => {
260281
it("additionalProperties", () => {
261282
// boolean

0 commit comments

Comments
 (0)