diff --git a/.changeset/two-elephants-heal.md b/.changeset/two-elephants-heal.md new file mode 100644 index 000000000..0528f2b1f --- /dev/null +++ b/.changeset/two-elephants-heal.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: ensure strings with both single/double quotes and backticks are escaped properly ([#310](https://github.com/hey-api/openapi-ts/pull/310)) diff --git a/packages/openapi-ts/src/compiler/utils.ts b/packages/openapi-ts/src/compiler/utils.ts index 6fe406801..171018aba 100644 --- a/packages/openapi-ts/src/compiler/utils.ts +++ b/packages/openapi-ts/src/compiler/utils.ts @@ -70,7 +70,7 @@ export const ots = { !value.startsWith('`') && !value.endsWith('`') ) { - value = `\`${value}\``; + value = `\`${value.replace(/`/g, '\\`')}\``; } const text = encodeURIComponent(value); if (value.startsWith('`')) { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/models.ts.snap index 5465d863d..6edef1890 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v2/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v2/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v2/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v2/schemas.ts.snap index e0613ec52..bdfad9078 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v2/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v2/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/models.ts.snap index 8d9e97a96..2db63d8ff 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.ts.snap index d4f2992e7..00cc75699 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.ts.snap index 41a479dec..df440f550 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.ts.snap index d4f2992e7..00cc75699 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.ts.snap index 39cbcebac..3ff3c2f71 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.ts.snap index d4f2992e7..00cc75699 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.ts.snap index 0a9710da5..c8058f86e 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.ts.snap index d4f2992e7..00cc75699 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/models.ts.snap index 22cff0e13..cc389e200 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/schemas.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/schemas.ts.snap index d4f2992e7..00cc75699 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/schemas.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/schemas.ts.snap @@ -11,6 +11,11 @@ export const $CommentWithBackticks = { type: 'integer', } as const; +export const $CommentWithBackticksAndQuotes = { + description: `Testing backticks and quotes in string: \`backticks\`, 'quotes', "double quotes" and \`\`\`multiple backticks\`\`\` should work`, + type: 'integer', +} as const; + export const $CommentWithSlashes = { description: 'Testing slashes in string: \backwards\\ and /forwards/// should work', type: 'integer', diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.ts.snap index 41a479dec..df440f550 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.ts.snap @@ -11,6 +11,11 @@ export type CommentWithBreaks = number; */ export type CommentWithBackticks = number; +/** + * Testing backticks and quotes in string: `backticks`, 'quotes', "double quotes" and ```multiple backticks``` should work + */ +export type CommentWithBackticksAndQuotes = number; + /** * Testing slashes in string: \backwards\\\ and /forwards/// should work */ diff --git a/packages/openapi-ts/test/spec/v2.json b/packages/openapi-ts/test/spec/v2.json index 7f11e0f98..6eadd859f 100644 --- a/packages/openapi-ts/test/spec/v2.json +++ b/packages/openapi-ts/test/spec/v2.json @@ -967,6 +967,10 @@ "description": "Testing backticks in string: `backticks` and ```multiple backticks``` should work", "type": "integer" }, + "CommentWithBackticksAndQuotes": { + "description": "Testing backticks and quotes in string: `backticks`, 'quotes', \"double quotes\" and ```multiple backticks``` should work", + "type": "integer" + }, "CommentWithSlashes": { "description": "Testing slashes in string: \\backwards\\\\\\ and /forwards/// should work", "type": "integer" diff --git a/packages/openapi-ts/test/spec/v3.json b/packages/openapi-ts/test/spec/v3.json index 43274d9fc..3aa165e06 100644 --- a/packages/openapi-ts/test/spec/v3.json +++ b/packages/openapi-ts/test/spec/v3.json @@ -1733,6 +1733,10 @@ "description": "Testing backticks in string: `backticks` and ```multiple backticks``` should work", "type": "integer" }, + "CommentWithBackticksAndQuotes": { + "description": "Testing backticks and quotes in string: `backticks`, 'quotes', \"double quotes\" and ```multiple backticks``` should work", + "type": "integer" + }, "CommentWithSlashes": { "description": "Testing slashes in string: \\backwards\\\\\\ and /forwards/// should work", "type": "integer"