diff --git a/.changeset/new-rats-yawn.md b/.changeset/new-rats-yawn.md new file mode 100644 index 000000000..e7b32dd33 --- /dev/null +++ b/.changeset/new-rats-yawn.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +Stop trimming whitespace other than linebreaks in string values diff --git a/packages/openapi-typescript/src/utils.ts b/packages/openapi-typescript/src/utils.ts index 3c907dc6e..a1262911e 100644 --- a/packages/openapi-typescript/src/utils.ts +++ b/packages/openapi-typescript/src/utils.ts @@ -273,7 +273,7 @@ export function tsUnionOf(...types: (string | number | boolean)[]): string { /** escape string value */ export function escStr(input: any): string { if (typeof input !== "string") return JSON.stringify(input); - return `"${input.trim().replace(DOUBLE_QUOTE_RE, '\\"')}"`; + return `"${input.replace(LB_RE, "").replace(DOUBLE_QUOTE_RE, '\\"')}"`; } /** surround a JS object key with quotes, if needed */ diff --git a/packages/openapi-typescript/test/schema-object.test.ts b/packages/openapi-typescript/test/schema-object.test.ts index eab63e9b1..32edfca2f 100644 --- a/packages/openapi-typescript/test/schema-object.test.ts +++ b/packages/openapi-typescript/test/schema-object.test.ts @@ -57,6 +57,12 @@ describe("Schema Object", () => { status?: "complete" | "incomplete"; }`); }); + + test("enum (whitespace)", () => { + const schema: SchemaObject = { type: "string", enum: [" blue", "green ", " ", ""] }; + const generated = transformSchemaObject(schema, options); + expect(generated).toBe('" blue" | "green " | " " | ""'); + }); }); describe("number", () => {