Skip to content

Commit f291a32

Browse files
fabien0102atrakh
andauthored
fix: Fix comment generation (#58)
* Deal with */ in JSDoc values * Update plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts Co-authored-by: Arnold Trakhtenberg <[email protected]> Co-authored-by: Arnold Trakhtenberg <[email protected]>
1 parent ec3d4a8 commit f291a32

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,40 @@ describe("schemaToTypeAliasDeclaration", () => {
7878
expect(printSchema(schema)).toBe(`export type Test = 1 | 2 | 3;`);
7979
});
8080

81+
it("should skip example which contains `*/` to avoid confusion", () => {
82+
const schema: SchemaObject = {
83+
title: "CronTimingCreate",
84+
required: ["type", "cron_expression"],
85+
type: "object",
86+
properties: {
87+
cron_expression: {
88+
title: "Cron Expression",
89+
type: "string",
90+
description: "The string representing the timing's cron expression.",
91+
format: "cron-string",
92+
example: "*/5 * * * *", // `*/` is conflicting the multiline comment syntax
93+
},
94+
},
95+
additionalProperties: false,
96+
description: "Cron timing schema for create requests.",
97+
};
98+
99+
expect(printSchema(schema)).toMatchInlineSnapshot(`
100+
"/**
101+
* Cron timing schema for create requests.
102+
*/
103+
export type Test = {
104+
/*
105+
* The string representing the timing's cron expression.
106+
*
107+
* @format cron-string
108+
* @example [see original specs]
109+
*/
110+
cron_expression: string;
111+
};"
112+
`);
113+
});
114+
81115
it("should generate top-level documentation", () => {
82116
const schema: SchemaObject = {
83117
type: "null",

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,29 @@ const getJSDocComment = (
544544
}, schema)
545545
: schema;
546546

547+
const getJsDocIdentifier = (value: unknown) => {
548+
const multilineEndChar = "*/";
549+
550+
if (typeof value === "string" && !value.includes(multilineEndChar)) {
551+
return f.createIdentifier(value);
552+
}
553+
554+
if (
555+
typeof value === "object" &&
556+
!JSON.stringify(value).includes(multilineEndChar)
557+
) {
558+
return f.createIdentifier(JSON.stringify(value));
559+
}
560+
561+
if (typeof value === "boolean" || typeof value === "number") {
562+
return f.createIdentifier(value.toString());
563+
}
564+
565+
// Value is not stringifiable
566+
// See https://github.com/fabien0102/openapi-codegen/issues/36, https://github.com/fabien0102/openapi-codegen/issues/57
567+
return f.createIdentifier("[see original specs]");
568+
};
569+
547570
const propertyTags: ts.JSDocPropertyTag[] = [];
548571
Object.entries(schemaWithAllOfResolved)
549572
.filter(
@@ -557,7 +580,7 @@ const getJSDocComment = (
557580
propertyTags.push(
558581
f.createJSDocPropertyTag(
559582
f.createIdentifier(singular(key)),
560-
f.createIdentifier(v.toString()),
583+
getJsDocIdentifier(v),
561584
false
562585
)
563586
)
@@ -566,11 +589,7 @@ const getJSDocComment = (
566589
propertyTags.push(
567590
f.createJSDocPropertyTag(
568591
f.createIdentifier(key),
569-
f.createIdentifier(
570-
typeof value === "object"
571-
? JSON.stringify(value)
572-
: value.toString()
573-
),
592+
getJsDocIdentifier(value),
574593
false
575594
)
576595
);

0 commit comments

Comments
 (0)