Skip to content

Commit d32ce71

Browse files
committed
fix(openapi-typescript): missing jsdocs comment anyof nested schema
1 parent 82e98b4 commit d32ce71

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface AnnotatedSchemaObject {
2929
deprecated?: boolean; // jsdoc without value
3030
description?: string; // jsdoc with value
3131
enum?: unknown[]; // jsdoc without value
32+
anyOf?: unknown[]; // jsdoc without value
3233
example?: string; // jsdoc with value
3334
format?: string; // not jsdoc
3435
nullable?: boolean; // Node information
@@ -80,6 +81,16 @@ export function addJSDocComment(schemaObject: AnnotatedSchemaObject, node: ts.Pr
8081
output.push(`@${field} ${String(serialized).replace(LB_RE, "\n * ")}`);
8182
}
8283

84+
// anyOf
85+
if (!schemaObject.description && Array.isArray(schemaObject.anyOf)) {
86+
const anyOfDescriptions = schemaObject.anyOf
87+
.map((subSchema: any) => subSchema.description)
88+
.filter((description: string | undefined) => typeof description === "string" && description.trim() !== "");
89+
if (anyOfDescriptions.length > 0) {
90+
output.push(`@description ${anyOfDescriptions.join(" | ")}`);
91+
}
92+
}
93+
8394
// JSDoc 'Constant' without value
8495
if ("const" in schemaObject) {
8596
output.push("@constant");

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

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ describe("addJSDocComment", () => {
5252
expect(astToString(ts.factory.createTypeLiteralNode([property])).trim()).toBe(`{
5353
/** This is a comment with \`/* an example comment *\\/\` within */
5454
comment: boolean;
55+
}`);
56+
});
57+
58+
test("anyOf", () => {
59+
const property = ts.factory.createPropertySignature(undefined, "comment", undefined, tsUnion([BOOLEAN, BOOLEAN]));
60+
addJSDocComment(
61+
{ anyOf: [{ description: "This is the comment 1" }, { description: "This is the comment 2" }] },
62+
property,
63+
);
64+
expect(astToString(ts.factory.createTypeLiteralNode([property])).trim()).toBe(`{
65+
/** @description This is the comment 1 | This is the comment 2 */
66+
comment: boolean;
5567
}`);
5668
});
5769
});

0 commit comments

Comments
 (0)