Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openapi-typescript): missing jsdocs comment anyof nested schema #2143

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(" | ")}`);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this will still fail for generating jsdoc descriptions for the other compositions allOf and oneOf.

At least in the case of allOf you could re-use this logic and intersect (&) the entries of instead of union-ing (|) them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, makes sense to me. I’ll try to take AllOf and OneOf into account in this PR then


// JSDoc 'Constant' without value
if ("const" in schemaObject) {
output.push("@constant");
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-typescript/test/lib/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}`);
});
});
Expand Down
Loading