Skip to content

Commit d423084

Browse files
committed
Factorize parenthesise function and apply it to arrays
1 parent b3c3508 commit d423084

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

packages/openapi-typescript/src/utils.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,22 @@ export function encodeRef(ref: string): string {
168168
return ref.replace(TILDE_RE, "~0").replace(FS_RE, "~1");
169169
}
170170

171+
/** if the type has & or | we should parenthesise it for safety */
172+
function parenthesise(type: string) {
173+
return TS_UNION_INTERSECTION_RE.test(type) ? `(${type})` : type;
174+
}
175+
171176
/** T[] */
172177
export function tsArrayOf(type: string): string {
173-
return `(${type})[]`;
178+
return `${parenthesise(type)}[]`;
174179
}
175180

176181
/** X & Y & Z; */
177182
export function tsIntersectionOf(...types: string[]): string {
178183
types = types.filter((t) => t !== "unknown");
179184
if (types.length === 0) return "unknown";
180185
if (types.length === 1) return String(types[0]); // don’t add parentheses around one thing
181-
return types.map((t) => (TS_UNION_INTERSECTION_RE.test(t) ? `(${t})` : t)).join(" & ");
186+
return types.map((t) => parenthesise(t)).join(" & ");
182187
}
183188

184189
/** NonNullable<T> */
@@ -258,11 +263,7 @@ export function tsUnionOf(...types: (string | number | boolean)[]): string {
258263
let out = "";
259264
for (let i = 0; i < memberArr.length; i++) {
260265
const t = memberArr[i];
261-
262-
// if the type has & or | we should parenthesise it for safety
263-
const escaped = TS_UNION_INTERSECTION_RE.test(t) ? `(${t})` : t;
264-
265-
out += escaped;
266+
out += parenthesise(t);
266267
if (i !== memberArr.length - 1) out += " | ";
267268
}
268269

packages/openapi-typescript/test/schema-object.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe("Schema Object", () => {
9797
test("basic", () => {
9898
const schema: SchemaObject = { type: "array", items: { type: "string" } };
9999
const generated = transformSchemaObject(schema, options);
100-
expect(generated).toBe("(string)[]");
100+
expect(generated).toBe("string[]");
101101
});
102102

103103
test("tuple array", () => {
@@ -109,7 +109,7 @@ describe("Schema Object", () => {
109109
test("ref", () => {
110110
const schema: SchemaObject = { type: "array", items: { $ref: 'components["schemas"]["ArrayItem"]' } };
111111
const generated = transformSchemaObject(schema, options);
112-
expect(generated).toBe('(components["schemas"]["ArrayItem"])[]');
112+
expect(generated).toBe('components["schemas"]["ArrayItem"][]');
113113
});
114114
});
115115

@@ -267,7 +267,7 @@ describe("Schema Object", () => {
267267

268268
test("array", () => {
269269
const generated = transformSchemaObject({ type: "array", items: { type: "string" }, nullable: true }, options);
270-
expect(generated).toBe("(string)[] | null");
270+
expect(generated).toBe("string[] | null");
271271
});
272272

273273
test("object", () => {
@@ -301,7 +301,7 @@ describe("Schema Object", () => {
301301

302302
test("array", () => {
303303
const generated = transformSchemaObject({ type: ["array", "null"], items: { type: "string" } } as any, options);
304-
expect(generated).toBe("(string)[] | null");
304+
expect(generated).toBe("string[] | null");
305305
});
306306

307307
test("object", () => {
@@ -405,7 +405,7 @@ describe("Schema Object", () => {
405405
} | {
406406
number: number;
407407
} | {
408-
array: (string)[];
408+
array: string[];
409409
} | {
410410
object: {
411411
string: string;
@@ -606,10 +606,10 @@ describe("Schema Object", () => {
606606

607607
test("supportArrayLength", () => {
608608
const opts = { ...options, ctx: { ...options.ctx, supportArrayLength: true } };
609-
expect(transformSchemaObject({ type: "array", items: { type: "string" } }, options)).toBe(`(string)[]`);
610-
expect(transformSchemaObject({ type: "array", items: { type: "string" }, minItems: 1 }, opts)).toBe(`[string, ...(string)[]]`);
609+
expect(transformSchemaObject({ type: "array", items: { type: "string" } }, options)).toBe(`string[]`);
610+
expect(transformSchemaObject({ type: "array", items: { type: "string" }, minItems: 1 }, opts)).toBe(`[string, ...string[]]`);
611611
expect(transformSchemaObject({ type: "array", items: { type: "string" }, maxItems: 2 }, opts)).toBe(`[] | [string] | [string, string]`);
612-
expect(transformSchemaObject({ type: "array", items: { type: "string" }, maxItems: 20 }, opts)).toBe(`(string)[]`);
612+
expect(transformSchemaObject({ type: "array", items: { type: "string" }, maxItems: 20 }, opts)).toBe(`string[]`);
613613
});
614614

615615
test("prefixItems", () => {
@@ -642,9 +642,9 @@ describe("Schema Object", () => {
642642
ctx: { ...options.ctx, immutableTypes: true },
643643
});
644644
expect(generated).toBe(`{
645-
readonly array?: readonly ({
645+
readonly array?: readonly {
646646
[key: string]: unknown;
647-
})[] | null;
647+
}[] | null;
648648
}`);
649649
});
650650
});

0 commit comments

Comments
 (0)