Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

importer - Add support for x-ms-list extension to add `@list` decorator to operations
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export function getExtensions(element: Extensions): TypeSpecDecorator[] {

for (const key of Object.keys(element)) {
if (isExtensionKey(key)) {
// Handle x-ms-list extension specially
if (key === "x-ms-list" && element[key] === true) {
decorators.push({
name: "list",
args: [],
});
}

decorators.push({
name: extensionDecoratorName,
args: [key, normalizeObjectValue(element[key])],
Expand Down
143 changes: 143 additions & 0 deletions packages/openapi3/test/tsp-openapi3/x-ms-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { describe, expect, it } from "vitest";
import { OpenAPI3Response } from "../../src/types.js";
import { renderTypeSpecForOpenAPI3, validateTsp } from "./utils/tsp-for-openapi3.js";

const response: OpenAPI3Response = {
description: "test response",
content: {
"application/json": {
schema: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
name: { type: "string" },
},
},
},
},
},
};

describe("x-ms-list extension", () => {
it("adds @list decorator when x-ms-list is true", async () => {
const tsp = await renderTypeSpecForOpenAPI3({
paths: {
"/widgets": {
get: {
operationId: "Widgets_list",
"x-ms-list": true,
parameters: [],
responses: {
"200": response,
},
},
},
},
});

expect(tsp).toContain("@list");
expect(tsp).toContain('@extension("x-ms-list", true)');

// Note: We don't validate the TypeSpec here because the generated code
// may not be semantically correct without proper pagination properties.
// The x-ms-list extension just indicates intent to mark the operation as a list operation.
});

it("does not add @list decorator when x-ms-list is false", async () => {
const tsp = await renderTypeSpecForOpenAPI3({
paths: {
"/widgets": {
get: {
operationId: "Widgets_list",
"x-ms-list": false,
parameters: [],
responses: {
"200": response,
},
},
},
},
});

expect(tsp).not.toContain("@list");
expect(tsp).toContain('@extension("x-ms-list", false)');

await validateTsp(tsp);
});

it("does not add @list decorator when x-ms-list is not present", async () => {
const tsp = await renderTypeSpecForOpenAPI3({
paths: {
"/widgets": {
get: {
operationId: "Widgets_list",
parameters: [],
responses: {
"200": response,
},
},
},
},
});

expect(tsp).not.toContain("@list");
expect(tsp).not.toContain("x-ms-list");

await validateTsp(tsp);
});

it("handles x-ms-list in complete example with other decorators", async () => {
const tsp = await renderTypeSpecForOpenAPI3({
paths: {
"/widgets": {
get: {
operationId: "Widgets_list",
description: "List widgets",
summary: "List all widgets",
"x-ms-list": true,
parameters: [],
responses: {
"200": {
description: "The request has succeeded.",
content: {
"application/json": {
schema: {
type: "array",
items: {
$ref: "#/components/schemas/Widget",
},
},
},
},
},
},
tags: ["Widgets"],
},
},
},
schemas: {
Widget: {
type: "object",
required: ["id", "name"],
properties: {
id: { type: "string" },
name: { type: "string" },
},
},
},
});

// Check that all expected decorators are present
expect(tsp).toContain("@list");
expect(tsp).toContain('@extension("x-ms-list", true)');
expect(tsp).toContain('@summary("List all widgets")');
expect(tsp).toContain('@tag("Widgets")');
expect(tsp).toContain("List widgets");

// Note: We don't validate the TypeSpec here because the generated code
// may not be semantically correct without proper pagination properties.
// The x-ms-list extension just indicates intent to mark the operation as a list operation.
});
});
Loading