Skip to content

Commit e735ff2

Browse files
pimveldhuisenPim Veldhuisen
and
Pim Veldhuisen
authored
Stop trimming whitespace other than linebreaks in string values (#1269)
* add failing unit test for enums with whitespace * stop trimming whitespace when escaping strings * add changeset --------- Co-authored-by: Pim Veldhuisen <[email protected]>
1 parent 8570947 commit e735ff2

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

.changeset/new-rats-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Stop trimming whitespace other than linebreaks in string values

packages/openapi-typescript/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export function tsUnionOf(...types: (string | number | boolean)[]): string {
273273
/** escape string value */
274274
export function escStr(input: any): string {
275275
if (typeof input !== "string") return JSON.stringify(input);
276-
return `"${input.trim().replace(DOUBLE_QUOTE_RE, '\\"')}"`;
276+
return `"${input.replace(LB_RE, "").replace(DOUBLE_QUOTE_RE, '\\"')}"`;
277277
}
278278

279279
/** surround a JS object key with quotes, if needed */

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

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ describe("Schema Object", () => {
5757
status?: "complete" | "incomplete";
5858
}`);
5959
});
60+
61+
test("enum (whitespace)", () => {
62+
const schema: SchemaObject = { type: "string", enum: [" blue", "green ", " ", ""] };
63+
const generated = transformSchemaObject(schema, options);
64+
expect(generated).toBe('" blue" | "green " | " " | ""');
65+
});
6066
});
6167

6268
describe("number", () => {

0 commit comments

Comments
 (0)