Skip to content

Commit

Permalink
Fix operation list issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Pan Shao committed Oct 8, 2024
1 parent 96f9850 commit be87786
Show file tree
Hide file tree
Showing 19 changed files with 555 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export async function emitMain(
metadata: Metadata | undefined,
): Promise<void> {
const { isArm } = getOptions();
const content = `${getHeaders()}\n${
isArm ? getArmServiceInformation(program, metadata!) : getServiceInformation(program)
}`;
const content = `${getHeaders()}\n${isArm ? getArmServiceInformation(program, metadata!) : getServiceInformation(program)
}${isArm && program.containsListOperation ? "\n\ninterface Operations extends Azure.ResourceManager.Operations {}"
: ""}`;
const session = getSession();
session.writeFile({ filename: filePath, content: await formatTypespecFile(content, filePath) });
}
Expand Down
19 changes: 3 additions & 16 deletions packages/extensions/openapi-to-typespec/src/emiters/emit-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TypespecEnum, TypespecProgram } from "../interfaces";
import { getOptions } from "../options";
import { formatTypespecFile } from "../utils/format";
import { getModelsImports } from "../utils/imports";
import { getNamespace, getNamespaceStatement } from "../utils/namespace";
import { getNamespaceStatement } from "../utils/namespace";

export async function emitModels(filePath: string, program: TypespecProgram): Promise<void> {
const content = generateModels(program);
Expand All @@ -23,8 +23,6 @@ function generateModels(program: TypespecProgram) {
"\n",
);

const isArm = getOptions().isArm;

const enums = flattenEnums(models.enums).join("");
const objects = models.objects.map(generateObject).join("\n\n");
return [
Expand All @@ -33,9 +31,7 @@ function generateModels(program: TypespecProgram) {
namespaces,
"\n",
getNamespaceStatement(program),
isArm && containsListOperation(program)
? "\ninterface Operations extends Azure.ResourceManager.Operations {} \n"
: "\n",
"\n",
enums,
"\n",
objects,
Expand All @@ -48,13 +44,4 @@ function flattenEnums(enums: TypespecEnum[]) {
}, []);
}

function containsListOperation(program: TypespecProgram): boolean {
const providerNamespace = getNamespace(program);
const listOperationRoute = `/providers/${providerNamespace}/operations`;
return (
program.operationGroups
.flatMap((g) => g.operations)
.map((o) => o.route)
.find((r) => r === listOperationRoute) !== undefined
);
}

39 changes: 20 additions & 19 deletions packages/extensions/openapi-to-typespec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface TypespecProgram {
models: Models;
operationGroups: TypespecOperationGroup[];
serviceInformation: ServiceInformation;
containsListOperation: boolean;
}

export interface TypespecOptions {
Expand Down Expand Up @@ -228,25 +229,25 @@ export type TspArmResourceOperation =

export interface TspArmResourceNonListOperation extends TspArmResourceOperationBase {
kind:
| "ArmResourceRead"
| "ArmResourceCreateOrReplaceSync"
| "ArmResourceCreateOrReplaceAsync"
| "ArmResourcePatchSync"
| "ArmResourcePatchAsync"
| "ArmTagsPatchSync"
| "ArmTagsPatchAsync"
| "ArmCustomPatchSync"
| "ArmCustomPatchAsync"
| "ArmResourceDeleteSync"
| "ArmResourceDeleteAsync"
| "ArmResourceDeleteWithoutOkAsync"
| "ArmResourceActionSync"
| "ArmResourceActionNoContentSync"
| "ArmResourceActionAsync"
| "ArmResourceActionNoResponseContentAsync"
| "checkGlobalNameAvailability"
| "checkLocalNameAvailability"
| "checkNameAvailability";
| "ArmResourceRead"
| "ArmResourceCreateOrReplaceSync"
| "ArmResourceCreateOrReplaceAsync"
| "ArmResourcePatchSync"
| "ArmResourcePatchAsync"
| "ArmTagsPatchSync"
| "ArmTagsPatchAsync"
| "ArmCustomPatchSync"
| "ArmCustomPatchAsync"
| "ArmResourceDeleteSync"
| "ArmResourceDeleteAsync"
| "ArmResourceDeleteWithoutOkAsync"
| "ArmResourceActionSync"
| "ArmResourceActionNoContentSync"
| "ArmResourceActionAsync"
| "ArmResourceActionNoResponseContentAsync"
| "checkGlobalNameAvailability"
| "checkLocalNameAvailability"
| "checkNameAvailability";
}

export interface TspArmResourceListOperation extends TspArmResourceOperationBase {
Expand Down
11 changes: 8 additions & 3 deletions packages/extensions/openapi-to-typespec/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
.filter((c) => c.language.default.name !== "Versions")
.map((c) => transformEnum(c, codeModel));

const { isArm } = getOptions();
const { isArm, namespace } = getOptions();

// objects need to be converted first because they are used in operation convertion
const typespecObjects = (codeModel.schemas.objects ?? []).map((o) => transformObject(o, codeModel));

const armResources = isArm
? codeModel.schemas.objects
?.filter((o) => isResourceSchema(o))
.map((o) => transformTspArmResource(o as ArmResourceSchema)) ?? []
?.filter((o) => isResourceSchema(o))
.map((o) => transformTspArmResource(o as ArmResourceSchema)) ?? []
: [];

const serviceInformation = transformServiceInformation(codeModel);
Expand All @@ -67,6 +67,7 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
}
}

const listOperationRoute = `/providers/${namespace}/operations`;
return {
serviceInformation,
models: {
Expand All @@ -75,5 +76,9 @@ function transformModel(codeModel: CodeModel): TypespecProgram {
armResources,
},
operationGroups: typespecOperationGroups,
containsListOperation: codeModel.operationGroups
.flatMap((g) => g.operations)
.map((o) => o.requests?.[0].protocol.http?.path)
.find((r) => r === listOperationRoute) !== undefined
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2023_11_14_preview: "2023-11-14-preview",
}

interface Operations extends Azure.ResourceManager.Operations {}
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2022_09_04: "2022-09-04",
}

interface Operations extends Azure.ResourceManager.Operations {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
}
},
"tags": [
{
"name": "Operations"
},
{
"name": "DataProducts"
},
Expand All @@ -51,6 +54,37 @@
}
],
"paths": {
"/providers/Microsoft.NetworkAnalytics/operations": {
"get": {
"operationId": "Operations_List",
"tags": [
"Operations"
],
"description": "List the operations for the provider",
"parameters": [
{
"$ref": "../../common-types/resource-management/v3/types.json#/parameters/ApiVersionParameter"
}
],
"responses": {
"200": {
"description": "Azure operation completed successfully.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/OperationListResult"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/ErrorResponse"
}
}
},
"x-ms-pageable": {
"nextLinkName": "nextLink"
}
}
},
"/subscriptions/{subscriptionId}/providers/Microsoft.NetworkAnalytics/dataProducts": {
"get": {
"operationId": "DataProducts_ListBySubscription",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2023_11_15: "2023-11-15",
}

interface Operations extends Azure.ResourceManager.Operations {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
}
},
"tags": [
{
"name": "Operations"
},
{
"name": "Accounts"
},
Expand All @@ -48,6 +51,37 @@
}
],
"paths": {
"/providers/Microsoft.AzurePlaywrightService/operations": {
"get": {
"operationId": "Operations_List",
"tags": [
"Operations"
],
"description": "List the operations for the provider",
"parameters": [
{
"$ref": "../../common-types/resource-management/v3/types.json#/parameters/ApiVersionParameter"
}
],
"responses": {
"200": {
"description": "Azure operation completed successfully.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/OperationListResult"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/ErrorResponse"
}
}
},
"x-ms-pageable": {
"nextLinkName": "nextLink"
}
}
},
"/subscriptions/{subscriptionId}/providers/Microsoft.AzurePlaywrightService/accounts": {
"get": {
"operationId": "Accounts_ListBySubscription",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2023_10_01_preview: "2023-10-01-preview",
}

interface Operations extends Azure.ResourceManager.Operations {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
}
},
"tags": [
{
"name": "Operations"
},
{
"name": "TrafficControllers"
},
Expand All @@ -51,6 +54,37 @@
}
],
"paths": {
"/providers/Microsoft.ServiceNetworking/operations": {
"get": {
"operationId": "Operations_List",
"tags": [
"Operations"
],
"description": "List the operations for the provider",
"parameters": [
{
"$ref": "../../common-types/resource-management/v3/types.json#/parameters/ApiVersionParameter"
}
],
"responses": {
"200": {
"description": "Azure operation completed successfully.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/OperationListResult"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/ErrorResponse"
}
}
},
"x-ms-pageable": {
"nextLinkName": "nextLink"
}
}
},
"/subscriptions/{subscriptionId}/providers/Microsoft.ServiceNetworking/trafficControllers": {
"get": {
"operationId": "TrafficControllers_ListBySubscription",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2023_11_01: "2023-11-01",
}

interface Operations extends Azure.ResourceManager.Operations {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
}
},
"tags": [
{
"name": "Operations"
},
{
"name": "StorageAccounts"
},
Expand Down Expand Up @@ -93,6 +96,37 @@
}
],
"paths": {
"/providers/Microsoft.Storage/operations": {
"get": {
"operationId": "Operations_List",
"tags": [
"Operations"
],
"description": "List the operations for the provider",
"parameters": [
{
"$ref": "../../common-types/resource-management/v3/types.json#/parameters/ApiVersionParameter"
}
],
"responses": {
"200": {
"description": "Azure operation completed successfully.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/OperationListResult"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "../../common-types/resource-management/v3/types.json#/definitions/ErrorResponse"
}
}
},
"x-ms-pageable": {
"nextLinkName": "nextLink"
}
}
},
"/subscriptions/{subscriptionId}/providers/Microsoft.Storage/checkNameAvailability": {
"post": {
"operationId": "StorageAccounts_CheckNameAvailability",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ enum Versions {
@useDependency(Azure.Core.Versions.v1_0_Preview_1)
v2022_09_01: "2022-09-01",
}

interface Operations extends Azure.ResourceManager.Operations {}
Loading

0 comments on commit be87786

Please sign in to comment.