Skip to content

Commit

Permalink
fix(plugin): support arrays as examples (comments introspection)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Dec 17, 2020
1 parent c1eaaae commit 9a38cca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/plugin/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ export function getMainCommentAndExamplesOfNode(
commentResult.push(oneComment);
}
if (includeExamples) {
const regexOfExample = /@example *((['"]?(?<exampleWithoutSpace>[^ ]+?)['"]?)|(['"](?<exampleWithSpaceAndQuotes>.+?)['"])) *$/gim;
const regexOfExample = /@example *((['"](?<exampleAsString>.+?)['"])|(?<exampleAsBooleanOrNumber>[^ ]+?)|(?<exampleAsArray>(\[.+?\]))) *$/gim;
let execResult: RegExpExecArray;
while (
(execResult = regexOfExample.exec(commentSource)) &&
execResult.length > 1
) {
const example =
execResult.groups?.exampleWithSpaceAndQuotes ??
execResult.groups?.exampleWithoutSpace;
execResult.groups?.exampleAsString ??
execResult.groups?.exampleAsBooleanOrNumber ??
(execResult.groups?.exampleAsArray &&
execResult.groups.exampleAsArray.replace(/'/g, '"'));

const type = typeChecker.getTypeAtLocation(node);
if (type && isString(type)) {
Expand Down
12 changes: 10 additions & 2 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,17 +473,25 @@ export class ModelClassVisitor extends AbstractFileVisitor {
if (examples.length === 1) {
const examplePropertyAssignment = ts.createPropertyAssignment(
'example',
ts.createLiteral(examples[0])
this.createLiteralFromAnyValue(examples[0])
);
propertyAssignments.push(examplePropertyAssignment);
} else {
const examplesPropertyAssignment = ts.createPropertyAssignment(
'examples',
ts.createArrayLiteral(examples.map((e) => ts.createLiteral(e)))
this.createLiteralFromAnyValue(examples)
);
propertyAssignments.push(examplesPropertyAssignment);
}
}
return propertyAssignments;
}

private createLiteralFromAnyValue(item: any) {
return Array.isArray(item)
? ts.createArrayLiteral(
item.map((item) => this.createLiteralFromAnyValue(item))
)
: ts.createLiteral(item);
}
}
26 changes: 25 additions & 1 deletion test/plugin/fixtures/create-cat-alt2.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ export abstract class Audit {
*/
testVersion;
/**
* testVersionArray
*
* @example ['0.0.1', '0.0.2']
* @memberof Audit
*/
testVersionArray: string[];
/**
* testVersionArray
*
* @example ['version 123', 'version 321']
* @memberof Audit
*/
testVersionArray2: string[];
/**
* testVersionArray
*
* @example [123, 321]
* @memberof Audit
*/
testVersionArray3: number[];
/**
* testBoolean
*
Expand All @@ -52,7 +76,7 @@ export abstract class Audit {
export const createCatDtoTextAlt2Transpiled = `import { CreateDateColumn, UpdateDateColumn, VersionColumn } from 'typeorm';
export class Audit {
static _OPENAPI_METADATA_FACTORY() {
return { createdAt: { required: true, type: () => Object, description: "test on createdAt" }, updatedAt: { required: true, type: () => Object }, version: { required: true, type: () => String, description: "test\\nversion", example: "version 123" }, testVersion: { required: true, type: () => Object, description: "testVersion", examples: ["0.0.1", "0.0.2"] }, testBoolean: { required: true, type: () => Boolean, description: "testBoolean", example: true }, testNumber: { required: true, type: () => Number, description: "testNumber", examples: [1, 5] } };
return { createdAt: { required: true, type: () => Object, description: "test on createdAt" }, updatedAt: { required: true, type: () => Object }, version: { required: true, type: () => String, description: "test\\nversion", example: "version 123" }, testVersion: { required: true, type: () => Object, description: "testVersion", examples: ["0.0.1", "0.0.2"] }, testVersionArray: { required: true, type: () => [String], description: "testVersionArray", example: ["0.0.1", "0.0.2"] }, testVersionArray2: { required: true, type: () => [String], description: "testVersionArray", example: ["version 123", "version 321"] }, testVersionArray3: { required: true, type: () => [Number], description: "testVersionArray", example: [123, 321] }, testBoolean: { required: true, type: () => Boolean, description: "testBoolean", example: true }, testNumber: { required: true, type: () => Number, description: "testNumber", examples: [1, 5] } };
}
}
__decorate([
Expand Down

0 comments on commit 9a38cca

Please sign in to comment.