Support single generation rule (#921)

* Support single generation rule

* change generateRule to a private property

* update the name of generationRule

* update the name of generationRule

Co-authored-by: Ray Chen <raychen@microsoft.com>
This commit is contained in:
Ke Yu 2023-01-11 11:36:49 +08:00 коммит произвёл GitHub
Родитель 5246068acc
Коммит 122f255d36
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 42 добавлений и 10 удалений

Просмотреть файл

@ -9,6 +9,7 @@
## 12/01/2022 3.2.4
- Generate Examples - Support specified generation rule
- API Scenario
- Support file type in formdata and body
- Change the prefix length to 8 from 10 in generated API Scenario

Просмотреть файл

@ -34,6 +34,18 @@ export const builder: yargs.CommandBuilder = {
describe: "the readme tag name.",
string: true,
},
max: {
alias: "maximumSet",
describe: "generate examples by rule of MaximumSet.",
boolean: true,
default: false,
},
min: {
alias: "minimumSet",
describe: "generate examples by rule of MinimumSet.",
boolean: true,
default: false,
},
};
export async function handler(argv: yargs.Arguments): Promise<void> {
@ -44,12 +56,19 @@ export async function handler(argv: yargs.Arguments): Promise<void> {
consoleLogLevel: argv.logLevel,
logFilepath: argv.f,
};
let generationRule: "Max" | "Min" | undefined;
if (argv.max && argv.min) {
generationRule = undefined;
} else {
generationRule = argv.max ? "Max" : argv.min ? "Min" : undefined;
}
await validate.generateExamples(
specPath,
argv.payload,
argv.o,
argv.config,
argv.tag,
generationRule,
vOptions
);
return 0;

Просмотреть файл

@ -34,9 +34,11 @@ export default class Generator {
private shouldMock: boolean;
private mockerCache: MockerCache;
private payloadCache: PayloadCache;
private generationRule?: "Max" | "Min";
public readonly transformContext: TransformContext;
public constructor(specFilePath: string, payloadDir?: string) {
public constructor(specFilePath: string, payloadDir?: string, generationRule?: "Max" | "Min") {
this.generationRule = generationRule;
this.shouldMock = payloadDir ? false : true;
this.specFilePath = specFilePath;
this.payloadDir = payloadDir;
@ -240,14 +242,23 @@ export default class Generator {
}
}
const ruleSet: RuleSet = [];
ruleSet.push({
exampleNamePostfix: "MaximumSet",
ruleName: "MaximumSet",
});
ruleSet.push({
exampleNamePostfix: "MinimumSet",
ruleName: "MinimumSet",
});
if (this.generationRule) {
ruleSet.push({
exampleNamePostfix: `${this.generationRule}imumSet`,
ruleName: `${this.generationRule}imumSet`,
});
} else {
ruleSet.push(
{
exampleNamePostfix: "MaximumSet",
ruleName: "MaximumSet",
},
{
exampleNamePostfix: "MinimumSet",
ruleName: "MinimumSet",
}
);
}
for (const rule of ruleSet) {
const error = await this.generateExample(operationId, specItem, rule);
if (error.length) {

Просмотреть файл

@ -277,6 +277,7 @@ export async function generateExamples(
operationIds?: string,
readme?: string,
tag?: string,
generationRule?: "Max" | "Min",
options?: Options
): Promise<any> {
if (!options) {
@ -304,7 +305,7 @@ export async function generateExamples(
log.consoleLogLevel = options.consoleLogLevel || log.consoleLogLevel;
log.filepath = options.logFilepath || log.filepath;
for (const file of wholeInputFiles) {
const generator = new ExampleGenerator(file, payloadDir);
const generator = new ExampleGenerator(file, payloadDir, generationRule);
if (operationIds) {
const operationIdArray = operationIds.trim().split(",");
for (const operationId of operationIdArray) {