Skip to content

Commit 7a1694f

Browse files
authored
Merge pull request #1510 from drwpow/fix/utf-8
Fix UTF-8 chars
2 parents a7e3968 + d1a686d commit 7a1694f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/openapi-typescript/src/lib/ts.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ export function tsIsPrimitive(type: ts.TypeNode): boolean {
324324
/** Create a literal type */
325325
export function tsLiteral(value: unknown): ts.TypeNode {
326326
if (typeof value === "string") {
327-
return ts.factory.createLiteralTypeNode(
328-
ts.factory.createStringLiteral(value),
329-
);
327+
// workaround for UTF-8: https://github.com/microsoft/TypeScript/issues/36174
328+
return ts.factory.createIdentifier(
329+
JSON.stringify(value),
330+
) as unknown as ts.TypeNode;
330331
}
331332
if (typeof value === "number") {
332333
return ts.factory.createLiteralTypeNode(

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ describe("transformSchemaObject > string", () => {
4545
// options: DEFAULT_OPTIONS,
4646
},
4747
],
48+
[
49+
"enum (UTF-8)",
50+
{
51+
given: { type: "string", enum: ["赤", "青", "緑"] },
52+
want: `"赤" | "青" | "緑"`,
53+
// options: DEFAULT_OPTIONS
54+
},
55+
],
56+
[
57+
"enum (quotes)",
58+
{
59+
// prettier-ignore
60+
// eslint-disable-next-line no-useless-escape
61+
given: { type: "string", enum: ['"', "'", '\"', "`"] },
62+
want: `"\\"" | "'" | "\\"" | "\`"`,
63+
},
64+
],
4865
[
4966
"nullable",
5067
{

0 commit comments

Comments
 (0)