From d32ce7182e9c0ab5ee708ebc24934d6ec1aa11be Mon Sep 17 00:00:00 2001 From: alewin Date: Thu, 6 Feb 2025 16:24:34 +0100 Subject: [PATCH] fix(openapi-typescript): missing jsdocs comment anyof nested schema --- packages/openapi-typescript/src/lib/ts.ts | 11 +++++++++++ packages/openapi-typescript/test/lib/ts.test.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/openapi-typescript/src/lib/ts.ts b/packages/openapi-typescript/src/lib/ts.ts index 3f795c60c..0b201a19b 100644 --- a/packages/openapi-typescript/src/lib/ts.ts +++ b/packages/openapi-typescript/src/lib/ts.ts @@ -29,6 +29,7 @@ export interface AnnotatedSchemaObject { deprecated?: boolean; // jsdoc without value description?: string; // jsdoc with value enum?: unknown[]; // jsdoc without value + anyOf?: unknown[]; // jsdoc without value example?: string; // jsdoc with value format?: string; // not jsdoc nullable?: boolean; // Node information @@ -80,6 +81,16 @@ export function addJSDocComment(schemaObject: AnnotatedSchemaObject, node: ts.Pr output.push(`@${field} ${String(serialized).replace(LB_RE, "\n * ")}`); } + // anyOf + if (!schemaObject.description && Array.isArray(schemaObject.anyOf)) { + const anyOfDescriptions = schemaObject.anyOf + .map((subSchema: any) => subSchema.description) + .filter((description: string | undefined) => typeof description === "string" && description.trim() !== ""); + if (anyOfDescriptions.length > 0) { + output.push(`@description ${anyOfDescriptions.join(" | ")}`); + } + } + // JSDoc 'Constant' without value if ("const" in schemaObject) { output.push("@constant"); diff --git a/packages/openapi-typescript/test/lib/ts.test.ts b/packages/openapi-typescript/test/lib/ts.test.ts index e7a1fd992..1cef46a1b 100644 --- a/packages/openapi-typescript/test/lib/ts.test.ts +++ b/packages/openapi-typescript/test/lib/ts.test.ts @@ -52,6 +52,18 @@ describe("addJSDocComment", () => { expect(astToString(ts.factory.createTypeLiteralNode([property])).trim()).toBe(`{ /** This is a comment with \`/* an example comment *\\/\` within */ comment: boolean; +}`); + }); + + test("anyOf", () => { + const property = ts.factory.createPropertySignature(undefined, "comment", undefined, tsUnion([BOOLEAN, BOOLEAN])); + addJSDocComment( + { anyOf: [{ description: "This is the comment 1" }, { description: "This is the comment 2" }] }, + property, + ); + expect(astToString(ts.factory.createTypeLiteralNode([property])).trim()).toBe(`{ + /** @description This is the comment 1 | This is the comment 2 */ + comment: boolean; }`); }); });