Skip to content

Commit

Permalink
perf(typeb): show unsupported reason when list APIs (#12061)
Browse files Browse the repository at this point in the history
* perf(typeb): show unsupported reason when list APIs

* perf: update test case

* perf: add test case

---------

Co-authored-by: rentu <rentu>
  • Loading branch information
SLdragon authored Jul 29, 2024
1 parent 6a64c8a commit d707c2d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/fx-core/resource/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
"core.copilotPlugin.validate.apiSpec.summary": "Teams Toolkit has checked your OpenAPI description document:\n\nSummary:\n%s.\n%s\n%s",
"core.copilotPlugin.validate.summary.validate.failed": "%s failed",
"core.copilotPlugin.validate.summary.validate.warning": "%s warning",
"core.copilotPlugin.list.unsupportedBecause": "is unsupported because:",
"core.copilotPlugin.scaffold.summary": "We have detected the following issues for your OpenAPI description document:\n%s",
"core.copilotPlugin.scaffold.summary.warning.operationId": "%s Mitigation: Not required, operationId has been automatically generated and added into \"%s\" file.",
"core.copilotPlugin.scaffold.summary.warning.swaggerVersion": "The OpenAPI description document is on Swagger version 2.0. Mitigation: Not required. The content has been converted to OpenAPI 3.0 and saved in \"%s\".",
Expand Down Expand Up @@ -372,6 +373,7 @@
"core.createProjectQuestion.apiSpec.operation.title": "Select Operation(s) Teams Can Interact with",
"core.createProjectQuestion.apiSpec.copilotOperation.title": "Select Operation(s) Copilot Can Interact with",
"core.createProjectQuestion.apiSpec.operation.apikey.placeholder": "GET/POST methods with at most 5 required parameter and API key are listed",
"core.createProjectQuestion.apiSpec.operation.plugin.placeholder": "Unsupported APIs are not listed, check the output channel for reasons",
"core.createProjectQuestion.apiSpec.operation.invalidMessage": "%s API(s) selected. You can select at least one and at most %s APIs.",
"core.createProjectQuestion.apiSpec.operation.multipleAuth": "Your selected APIs have multiple authorizations %s which are not supported.",
"core.createProjectQuestion.apiSpec.operation.multipleServer": "Your selected APIs have multiple server URLs %s which are not supported.",
Expand Down
9 changes: 9 additions & 0 deletions packages/fx-core/src/component/generator/apiSpec/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export async function listOperations(

const listResult: ListAPIResult = await specParser.list();

const invalidAPIs = listResult.APIs.filter((value) => !value.isValid);
for (const invalidAPI of invalidAPIs) {
context.logProvider.warning(
`${invalidAPI.api} ${getLocalizedString(
"core.copilotPlugin.list.unsupportedBecause"
)} ${invalidAPI.reason.map(mapInvalidReasonToMessage).join(", ")}`
);
}

const bearerTokenAuthAPIs = listResult.APIs.filter(
(api) => api.auth && Utils.isBearerTokenAuth(api.auth.authScheme)
);
Expand Down
4 changes: 3 additions & 1 deletion packages/fx-core/src/question/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,9 @@ export function apiOperationQuestion(
"core.createProjectQuestion.apiSpec.operation.placeholder.skipExisting"
);
} else if (isPlugin) {
placeholder = ""; // TODO: add placeholder for api plugin
placeholder = getLocalizedString(
"core.createProjectQuestion.apiSpec.operation.plugin.placeholder"
);
} else {
placeholder = getLocalizedString(
"core.createProjectQuestion.apiSpec.operation.apikey.placeholder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,45 @@ describe("listOperations", async () => {
expect(res.isOk()).to.be.true;
});

it("will show invalid api reasons", async () => {
const inputs = {
"custom-copilot-rag": "custom-copilot-rag-customApi",
platform: Platform.VSCode,
};
sandbox.stub(CopilotPluginHelper, "formatValidationErrors").resolves([]);
sandbox.stub(CopilotPluginHelper, "logValidationResults").resolves();
sandbox.stub(SpecParser.prototype, "validate").resolves({
status: ValidationStatus.Valid,
warnings: [],
errors: [],
});
sandbox.stub(SpecParser.prototype, "list").resolves({
APIs: [
{
api: "1",
server: "https://test",
operationId: "id1",
isValid: false,
reason: [ErrorType.NoParameter],
},
{
api: "2",
server: "https://test",
operationId: "id2",
isValid: true,
reason: [],
},
],
allAPICount: 2,
validAPICount: 1,
});
const warningSpy = sandbox.spy(context.logProvider, "warning");

const res = await CopilotPluginHelper.listOperations(context, "", inputs, true, false, "");
expect(res.isOk()).to.be.true;
expect(warningSpy.calledOnce).to.be.true;
});

it("should throw error if list api not from original OpenAPI spec", async () => {
const inputs = {
platform: Platform.VSCode,
Expand Down
5 changes: 4 additions & 1 deletion packages/fx-core/tests/question/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,10 @@ describe("scaffold question", () => {
assert.isTrue(options.length === 2);
assert.isTrue(options[0].id === "operation1");
assert.isTrue(options[1].id === "operation2");
assert.equal(placeholder, "");
assert.equal(
placeholder,
getLocalizedString("core.createProjectQuestion.apiSpec.operation.plugin.placeholder")
);
assert.equal(
title,
getLocalizedString("core.createProjectQuestion.apiSpec.copilotOperation.title")
Expand Down

0 comments on commit d707c2d

Please sign in to comment.