Skip to content

Commit

Permalink
Merge pull request #431 from hey-api/fix/template-bugs
Browse files Browse the repository at this point in the history
fix: template bugs
  • Loading branch information
mrlubos authored Apr 19, 2024
2 parents 0e1776b + 0b75192 commit 70222a2
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-pumas-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: properly escape backticks in template literals
5 changes: 5 additions & 0 deletions .changeset/slimy-wolves-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: properly escape expressions in template literals
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 @@ -98,7 +98,7 @@ export const ots = {
(hasNewlines || hasBothQuotes || hasUnescapedBackticks) &&
!isBacktickEscaped
) {
value = `\`${value.replace(/`/g, '\\`')}\``;
value = `\`${value.replace(/(?<!\\)`/g, '\\`').replace(/\${/g, '\\${')}\``;
}
const text = encodeURIComponent(value);
if (value.startsWith('`')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1670,3 +1670,40 @@ export const $ModelWithNumericEnumUnion = {
},
},
} as const;

export const $ModelWithBackticksInDescription = {
description: 'Some description with `back ticks`',
type: 'object',
properties: {
template: {
type: 'string',
description: `The template \`that\` should be used for parsing and importing the contents of the CSV file.
<br/><p>There is one placeholder currently supported:<ul> <li><b>\${x}</b> - refers to the n-th column in the CSV file, e.g. \${1}, \${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
<pre>
[
{
"resourceType": "Asset",
"identifier": {
"name": "\${1}",
"domain": {
"name": "\${2}",
"community": {
"name": "Some Community"
}
}
},
"attributes" : {
"00000000-0000-0000-0000-000000003115" : [ {
"value" : "\${3}"
} ],
"00000000-0000-0000-0000-000000000222" : [ {
"value" : "\${4}"
} ]
}
}
]
</pre>`,
},
},
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ export class ComplexService {
parameterReference,
},
errors: {
400: '400 server error',
400: '400 `server` error',
500: '500 server error',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,42 @@ export type ModelWithNumericEnumUnion = {
value?: 1 | 3 | 6 | 12;
};

/**
* Some description with `back ticks`
*/
export type ModelWithBackticksInDescription = {
/**
* The template `that` should be used for parsing and importing the contents of the CSV file.
*
* <br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
* <pre>
* [
* {
* "resourceType": "Asset",
* "identifier": {
* "name": "${1}",
* "domain": {
* "name": "${2}",
* "community": {
* "name": "Some Community"
* }
* }
* },
* "attributes" : {
* "00000000-0000-0000-0000-000000003115" : [ {
* "value" : "${3}"
* } ],
* "00000000-0000-0000-0000-000000000222" : [ {
* "value" : "${4}"
* } ]
* }
* }
* ]
* </pre>
*/
template?: string;
};

/**
* This is a reusable parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ export class ComplexService {
parameterReference,
},
errors: {
400: '400 server error',
400: '400 `server` error',
500: '500 server error',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,42 @@ export type ModelWithNumericEnumUnion = {
value?: 1 | 3 | 6 | 12;
};

/**
* Some description with `back ticks`
*/
export type ModelWithBackticksInDescription = {
/**
* The template `that` should be used for parsing and importing the contents of the CSV file.
*
* <br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
* <pre>
* [
* {
* "resourceType": "Asset",
* "identifier": {
* "name": "${1}",
* "domain": {
* "name": "${2}",
* "community": {
* "name": "Some Community"
* }
* }
* },
* "attributes" : {
* "00000000-0000-0000-0000-000000003115" : [ {
* "value" : "${3}"
* } ],
* "00000000-0000-0000-0000-000000000222" : [ {
* "value" : "${4}"
* } ]
* }
* }
* ]
* </pre>
*/
template?: string;
};

/**
* This is a reusable parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ export class ComplexService {
parameterReference,
},
errors: {
400: '400 server error',
400: '400 `server` error',
500: '500 server error',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,42 @@ export type ModelWithNumericEnumUnion = {
value?: 1 | 3 | 6 | 12;
};

/**
* Some description with `back ticks`
*/
export type ModelWithBackticksInDescription = {
/**
* The template `that` should be used for parsing and importing the contents of the CSV file.
*
* <br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
* <pre>
* [
* {
* "resourceType": "Asset",
* "identifier": {
* "name": "${1}",
* "domain": {
* "name": "${2}",
* "community": {
* "name": "Some Community"
* }
* }
* },
* "attributes" : {
* "00000000-0000-0000-0000-000000003115" : [ {
* "value" : "${3}"
* } ],
* "00000000-0000-0000-0000-000000000222" : [ {
* "value" : "${4}"
* } ]
* }
* }
* ]
* </pre>
*/
template?: string;
};

/**
* This is a reusable parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ export class ComplexService {
parameterReference,
},
errors: {
400: '400 server error',
400: '400 `server` error',
500: '500 server error',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,42 @@ export type ModelWithNumericEnumUnion = {
value?: 1 | 3 | 6 | 12;
};

/**
* Some description with `back ticks`
*/
export type ModelWithBackticksInDescription = {
/**
* The template `that` should be used for parsing and importing the contents of the CSV file.
*
* <br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
* <pre>
* [
* {
* "resourceType": "Asset",
* "identifier": {
* "name": "${1}",
* "domain": {
* "name": "${2}",
* "community": {
* "name": "Some Community"
* }
* }
* },
* "attributes" : {
* "00000000-0000-0000-0000-000000003115" : [ {
* "value" : "${3}"
* } ],
* "00000000-0000-0000-0000-000000000222" : [ {
* "value" : "${4}"
* } ]
* }
* }
* ]
* </pre>
*/
template?: string;
};

/**
* This is a reusable parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,42 @@ export type ModelWithNumericEnumUnion = {
value?: 1 | 3 | 6 | 12;
};

/**
* Some description with `back ticks`
*/
export type ModelWithBackticksInDescription = {
/**
* The template `that` should be used for parsing and importing the contents of the CSV file.
*
* <br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
* <pre>
* [
* {
* "resourceType": "Asset",
* "identifier": {
* "name": "${1}",
* "domain": {
* "name": "${2}",
* "community": {
* "name": "Some Community"
* }
* }
* },
* "attributes" : {
* "00000000-0000-0000-0000-000000003115" : [ {
* "value" : "${3}"
* } ],
* "00000000-0000-0000-0000-000000000222" : [ {
* "value" : "${4}"
* } ]
* }
* }
* ]
* </pre>
*/
template?: string;
};

/**
* This is a reusable parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1670,3 +1670,40 @@ export const $ModelWithNumericEnumUnion = {
},
},
} as const;

export const $ModelWithBackticksInDescription = {
description: 'Some description with `back ticks`',
type: 'object',
properties: {
template: {
type: 'string',
description: `The template \`that\` should be used for parsing and importing the contents of the CSV file.
<br/><p>There is one placeholder currently supported:<ul> <li><b>\${x}</b> - refers to the n-th column in the CSV file, e.g. \${1}, \${2}, ...)</li></ul><p>Example of a correct JSON template:</p>
<pre>
[
{
"resourceType": "Asset",
"identifier": {
"name": "\${1}",
"domain": {
"name": "\${2}",
"community": {
"name": "Some Community"
}
}
},
"attributes" : {
"00000000-0000-0000-0000-000000003115" : [ {
"value" : "\${3}"
} ],
"00000000-0000-0000-0000-000000000222" : [ {
"value" : "\${4}"
} ]
}
}
]
</pre>`,
},
},
} as const;
12 changes: 11 additions & 1 deletion packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@
}
},
"400": {
"description": "400 server error"
"description": "400 `server` error"
},
"500": {
"description": "500 server error"
Expand Down Expand Up @@ -3183,6 +3183,16 @@
"enum": [1, 3, 6, 12]
}
}
},
"ModelWithBackticksInDescription": {
"description": "Some description with `back ticks`",
"type": "object",
"properties": {
"template": {
"type": "string",
"description": "The template `that` should be used for parsing and importing the contents of the CSV file.\n\n<br/><p>There is one placeholder currently supported:<ul> <li><b>${x}</b> - refers to the n-th column in the CSV file, e.g. ${1}, ${2}, ...)</li></ul><p>Example of a correct JSON template:</p>\n<pre>\n[\n {\n \"resourceType\": \"Asset\",\n \"identifier\": {\n \"name\": \"${1}\",\n \"domain\": {\n \"name\": \"${2}\",\n \"community\": {\n \"name\": \"Some Community\"\n }\n }\n },\n \"attributes\" : {\n \"00000000-0000-0000-0000-000000003115\" : [ {\n \"value\" : \"${3}\" \n } ],\n \"00000000-0000-0000-0000-000000000222\" : [ {\n \"value\" : \"${4}\"\n } ]\n }\n }\n]\n</pre>"
}
}
}
}
}
Expand Down

0 comments on commit 70222a2

Please sign in to comment.