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

perf(spec-parser): treat required + default parameter as optional parameter #10009

Merged
merged 3 commits into from
Sep 21, 2023
Merged
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
14 changes: 9 additions & 5 deletions packages/fx-core/src/common/spec-parser/manifestUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function generateParametersFromSchema(
title: updateFirstLetter(name),
description: schema.description ?? "",
};
if (isRequired) {
if (isRequired && schema.default === undefined) {
requiredParams.push(parameter);
} else {
optionalParams.push(parameter);
Expand Down Expand Up @@ -123,10 +123,14 @@ export async function generateCommands(
title: updateFirstLetter(param.name),
description: param.description ?? "",
};
if (param.required) {
requiredParams.push(parameter);
} else {
optionalParams.push(parameter);

const schema = param.schema as OpenAPIV3.SchemaObject;
if (param.in !== "header" && param.in !== "cookie") {
if (param.required && schema?.default === undefined) {
requiredParams.push(parameter);
} else {
optionalParams.push(parameter);
}
}
});
}
Expand Down
16 changes: 10 additions & 6 deletions packages/fx-core/src/common/spec-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ export function checkParameters(paramObject: OpenAPIV3.ParameterObject[]): Check

for (let i = 0; i < paramObject.length; i++) {
const param = paramObject[i];
const schema = param.schema as OpenAPIV3.SchemaObject;
const isRequiredWithoutDefault = param.required && schema.default === undefined;

if (param.in === "header" || param.in === "cookie") {
if (param.required) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
continue;
}

const schema = param.schema as OpenAPIV3.SchemaObject;
if (
schema.type !== "boolean" &&
schema.type !== "string" &&
schema.type !== "number" &&
schema.type !== "integer"
) {
if (param.required) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
continue;
}

if (param.in === "query" || param.in === "path") {
if (param.required) {
if (isRequiredWithoutDefault) {
paramResult.requiredNum = paramResult.requiredNum + 1;
} else {
paramResult.optionalNum = paramResult.optionalNum + 1;
Expand All @@ -89,13 +91,15 @@ export function checkPostBody(
return paramResult;
}

const isRequiredWithoutDefault = isRequired && schema.default === undefined;

if (
schema.type === "string" ||
schema.type === "integer" ||
schema.type === "boolean" ||
schema.type === "number"
) {
if (isRequired) {
if (isRequiredWithoutDefault) {
paramResult.requiredNum = paramResult.requiredNum + 1;
} else {
paramResult.optionalNum = paramResult.optionalNum + 1;
Expand All @@ -113,7 +117,7 @@ export function checkPostBody(
paramResult.isValid = paramResult.isValid && result.isValid;
}
} else {
if (isRequired) {
if (isRequiredWithoutDefault) {
paramResult.isValid = false;
}
}
Expand Down
106 changes: 106 additions & 0 deletions packages/fx-core/tests/common/spec-parser/manifestUpdater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,56 @@ describe("generateCommands", () => {
]);
});

it("should treat POST operation required parameter with default value as optional parameter", async () => {
const spec: any = {
paths: {
"/pets": {
post: {
operationId: "createPet",
summary: "Create a pet",
requestBody: {
content: {
"application/json": {
schema: {
type: "object",
required: ["name"],
properties: {
name: {
type: "string",
description: "Name of the pet",
default: "value",
},
},
},
},
},
},
},
},
},
};
sinon.stub(fs, "pathExists").resolves(true);

const [result, warnings] = await generateCommands(spec, adaptiveCardFolder, manifestPath);
expect(result).to.deep.equal([
{
apiResponseRenderingTemplateFile: "adaptiveCards/createPet.json",
context: ["compose"],
id: "createPet",
parameters: [
{
description: "Name of the pet",
name: "name",
title: "Name",
},
],
title: "Create a pet",
type: "query",
},
]);
expect(warnings).to.deep.equal([]);
});

it("should not show warning for each GET/POST operation in the spec if only contains 1 optional parameters", async () => {
const spec: any = {
paths: {
Expand Down Expand Up @@ -648,6 +698,62 @@ describe("generateCommands", () => {
expect(warnings).to.deep.equal([]);
});

it("should treat required parameter with default value as optional parameter", async () => {
const spec: any = {
paths: {
"/pets": {
get: {
operationId: "getPets",
summary: "Get all pets",
parameters: [
{
name: "limit",
in: "query",
description: "Maximum number of pets to return",
required: false,
},
{
name: "id",
in: "query",
description: "ID of the pet",
required: true,
schema: {
type: "string",
default: "value",
},
},
],
},
},
},
};
sinon.stub(fs, "pathExists").resolves(true);

const expectedCommands = [
{
context: ["compose"],
type: "query",
title: "Get all pets",
id: "getPets",
parameters: [
{ name: "limit", title: "Limit", description: "Maximum number of pets to return" },
],
apiResponseRenderingTemplateFile: "adaptiveCards/getPets.json",
},
];

const [result, warnings] = await generateCommands(spec, adaptiveCardFolder, manifestPath);

expect(result).to.deep.equal(expectedCommands);
expect(warnings).to.deep.equal([
{
type: WarningType.OperationOnlyContainsOptionalParam,
content: format(ConstantString.OperationOnlyContainsOptionalParam, "getPets"),
data: "getPets",
},
]);
});

it("should generate commands for POST operation with string schema", async () => {
const spec: any = {
paths: {
Expand Down
Loading
Loading