Skip to content

Commit

Permalink
fix(compiler): ensure backticked strings are escaped properly
Browse files Browse the repository at this point in the history
Fixes #309.
  • Loading branch information
icedream committed Apr 9, 2024
1 parent 399a0ca commit a8e9e33
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/two-elephants-heal.md
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const ots = {
!value.startsWith('`') &&
!value.endsWith('`')
) {
value = `\`${value}\``;
value = `\`${value.replace(/`/g, '\\`')}\``;

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Check warning on line 73 in packages/openapi-ts/src/compiler/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/utils.ts#L73

Added line #L73 was not covered by tests
}
const text = encodeURIComponent(value);
if (value.startsWith('`')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-ts/test/spec/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a8e9e33

Please sign in to comment.