Skip to content

Commit

Permalink
perf(spec-parser): fix AC gen issue due to special chars in operation…
Browse files Browse the repository at this point in the history
…Id (#11819)

Co-authored-by: rentu <[email protected]>
  • Loading branch information
SLdragon and SLdragon authored Jun 13, 2024
1 parent 90d9b49 commit 3031916
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions packages/fx-core/src/component/generator/copilotPlugin/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ async function updateAdaptiveCardForCustomApi(
await fs.ensureDir(adaptiveCardsFolderPath);

for (const item of specItems) {
const name = item.item.operationId;
const name = item.item.operationId!.replace(/[^a-zA-Z0-9]/g, "_");
const [card] = AdaptiveCardGenerator.generateAdaptiveCard(item.item, true);
const cardFilePath = path.join(adaptiveCardsFolderPath, `${name!}.json`);
const cardFilePath = path.join(adaptiveCardsFolderPath, `${name}.json`);
await fs.writeFile(cardFilePath, JSON.stringify(card, null, 2));
}
}
Expand Down Expand Up @@ -874,7 +874,8 @@ app.ai.action("{{operationId}}", async (context, state, parameter) => {
const result = await path.{{method}}(parameter.path, parameter.body, {
params: parameter.query,
});
const card = generateAdaptiveCard("../adaptiveCards/{{operationId}}.json", result);
const cardName = "{{operationId}}".replace(/[^a-zA-Z0-9]/g, "_");
const card = generateAdaptiveCard("../adaptiveCards/" + cardName + ".json", result);
await context.sendActivity({ attachments: [card] });
} else {
await context.sendActivity("no result");
Expand All @@ -891,7 +892,8 @@ app.ai.action("{{operationId}}", async (context: TurnContext, state: Application
const result = await path.{{method}}(parameter.path, parameter.body, {
params: parameter.query,
});
const card = generateAdaptiveCard("../adaptiveCards/{{operationId}}.json", result);
const cardName = "{{operationId}}".replace(/[^a-zA-Z0-9]/g, "_");
const card = generateAdaptiveCard("../adaptiveCards/" + cardName + ".json", result);
await context.sendActivity({ attachments: [card] });
} else {
await context.sendActivity("no result");
Expand Down
5 changes: 3 additions & 2 deletions packages/spec-parser/src/specParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,13 @@ export class SpecParser {
const operation = (newSpec.paths[url] as any)[method] as OpenAPIV3.OperationObject;
try {
const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operation);
const fileName = path.join(adaptiveCardFolder, `${operation.operationId!}.json`);
const safeAdaptiveCardName = operation.operationId!.replace(/[^a-zA-Z0-9]/g, "_");
const fileName = path.join(adaptiveCardFolder, `${safeAdaptiveCardName}.json`);
const wrappedCard = wrapAdaptiveCard(card, jsonPath);
await fs.outputJSON(fileName, wrappedCard, { spaces: 2 });
const dataFileName = path.join(
adaptiveCardFolder,
`${operation.operationId!}.data.json`
`${safeAdaptiveCardName}.data.json`
);
await fs.outputJSON(dataFileName, {}, { spaces: 2 });
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions packages/spec-parser/test/specParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ describe("SpecParser", () => {
paths: {
"/hello": {
get: {
operationId: "helloApi",
responses: {
200: {
content: {
Expand Down Expand Up @@ -1395,6 +1396,7 @@ describe("SpecParser", () => {
"/hello": {
description: "additional description",
get: {
operationId: "helloApi",
responses: {
200: {
content: {
Expand Down

0 comments on commit 3031916

Please sign in to comment.