Skip to content

Stop trimming whitespace other than linebreaks in string values #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-rats-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Stop trimming whitespace other than linebreaks in string values
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\"')}"`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this is a smarter way to handle it

}

/** surround a JS object key with quotes, if needed */
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-typescript/test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down