Skip to content

Commit

Permalink
fix(spec parser): fix multiple media bug (#11759)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethBWSong authored Jun 3, 2024
1 parent f7c5ee2 commit 7f61fc3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ async function updateAdaptiveCardForCustomApi(

for (const item of specItems) {
const name = item.item.operationId;
const [card] = AdaptiveCardGenerator.generateAdaptiveCard(item.item);
const [card] = AdaptiveCardGenerator.generateAdaptiveCard(item.item, true);
const cardFilePath = path.join(adaptiveCardsFolderPath, `${name!}.json`);
await fs.writeFile(cardFilePath, JSON.stringify(card, null, 2));
}
Expand Down
7 changes: 5 additions & 2 deletions packages/spec-parser/src/adaptiveCardGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import { ConstantString } from "./constants";
import { SpecParserError } from "./specParserError";

export class AdaptiveCardGenerator {
static generateAdaptiveCard(operationItem: OpenAPIV3.OperationObject): [AdaptiveCard, string] {
static generateAdaptiveCard(
operationItem: OpenAPIV3.OperationObject,
allowMultipleMediaType = false
): [AdaptiveCard, string] {
try {
const { json } = Utils.getResponseJson(operationItem);
const { json } = Utils.getResponseJson(operationItem, allowMultipleMediaType);

let cardBody: Array<TextBlockElement | ImageElement | ArrayElement> = [];

Expand Down
9 changes: 7 additions & 2 deletions packages/spec-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export class Utils {
return str.charAt(0).toUpperCase() + str.slice(1);
}

static getResponseJson(operationObject: OpenAPIV3.OperationObject | undefined): {
static getResponseJson(
operationObject: OpenAPIV3.OperationObject | undefined,
allowMultipleMediaType = false
): {
json: OpenAPIV3.MediaTypeObject;
multipleMediaType: boolean;
} {
Expand All @@ -119,7 +122,9 @@ export class Utils {
json = responseObject.content["application/json"];
if (Utils.containMultipleMediaTypes(responseObject)) {
multipleMediaType = true;
json = {};
if (!allowMultipleMediaType) {
json = {};
}
} else {
break;
}
Expand Down
60 changes: 60 additions & 0 deletions packages/spec-parser/test/adaptiveCardGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,66 @@ describe("adaptiveCardGenerator", () => {
expect(actual).to.deep.equal(expected);
expect(jsonPath).to.equal("$");
});

it("should allow multiple media type if allowMultipleMediaType = true", async () => {
const operationItem = {
responses: {
"200": {
description: "OK",
content: {
"application/json": {
schema: {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
},
},
"application/xml": {
schema: {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "number",
},
},
},
},
},
},
},
} as any;
const expected = {
type: "AdaptiveCard",
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
version: "1.5",
body: [
{
type: "TextBlock",
text: "name: ${if(name, name, 'N/A')}",
wrap: true,
},
{
type: "TextBlock",
text: "age: ${if(age, age, 'N/A')}",
wrap: true,
},
],
};

const [actual, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, true);

expect(actual).to.deep.equal(expected);
expect(jsonPath).to.equal("$");
});
});

describe("generateCardFromResponse", () => {
Expand Down

0 comments on commit 7f61fc3

Please sign in to comment.