From bba1e8c7539f0e72d759a117d268f583804acc26 Mon Sep 17 00:00:00 2001 From: Pan Shao <97225342+pshao25@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:06:04 +0800 Subject: [PATCH] Fix several issues in converter (#5050) 1. `ResourceListResult` should be only replaced in arm. 2. Properties in TrackedResource/ProxyResource should not be in resource model. --------- Co-authored-by: Pan Shao --- .../openapi-to-typespec/2025-01-06-07-05.json | 10 +++++++ .../src/generate/generate-arm-resource.ts | 3 +- .../src/transforms/transform-arm-resources.ts | 17 +++++------ .../src/transforms/transform-operations.ts | 3 +- .../tsp-output/DeletedServiceContract.tsp | 6 ++++ .../arm-compute/tsp-output/CloudService.tsp | 29 ------------------- .../tsp-output/CloudServiceRole.tsp | 12 -------- .../arm-compute/tsp-output/DedicatedHost.tsp | 3 +- .../test/arm-compute/tsp-output/OSFamily.tsp | 12 -------- .../test/arm-compute/tsp-output/OSVersion.tsp | 12 -------- .../tsp-output/PrivateEndpointConnection.tsp | 12 -------- .../arm-compute/tsp-output/RoleInstance.tsp | 25 ---------------- .../VirtualMachineScaleSetExtension.tsp | 6 ---- .../VirtualMachineScaleSetVMExtension.tsp | 6 ---- .../test/keyvault/tsp-output/routes.tsp | 2 +- 15 files changed, 29 insertions(+), 129 deletions(-) create mode 100644 common/changes/@autorest/openapi-to-typespec/2025-01-06-07-05.json diff --git a/common/changes/@autorest/openapi-to-typespec/2025-01-06-07-05.json b/common/changes/@autorest/openapi-to-typespec/2025-01-06-07-05.json new file mode 100644 index 0000000000..b737fe91ec --- /dev/null +++ b/common/changes/@autorest/openapi-to-typespec/2025-01-06-07-05.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@autorest/openapi-to-typespec", + "comment": "Fix the ResourceListResult issue in data plane.", + "type": "patch" + } + ], + "packageName": "@autorest/openapi-to-typespec" +} \ No newline at end of file diff --git a/packages/extensions/openapi-to-typespec/src/generate/generate-arm-resource.ts b/packages/extensions/openapi-to-typespec/src/generate/generate-arm-resource.ts index aacbaa4906..464cbaa4c2 100644 --- a/packages/extensions/openapi-to-typespec/src/generate/generate-arm-resource.ts +++ b/packages/extensions/openapi-to-typespec/src/generate/generate-arm-resource.ts @@ -4,7 +4,6 @@ import pluralize from "pluralize"; import { TypespecOperation, TspArmResource, - isArmResourceActionOperation, TypespecTemplateModel, TypespecVoidType, TspLroHeaders, @@ -129,7 +128,7 @@ function generateArmResourceOperation(resource: TspArmResource): string { definitions.push( `${operation.name} is ${operation.kind}<${operation.resource}, ${generateArmRequest( operation.request, - )}, ${generateArmResponse(operation.response)}${operation.baseParameters![0]}${ + )}, ${generateArmResponse(operation.response)}, BaseParameters = ${operation.baseParameters![0]}${ operation.parameters ? `, Parameters = { ${generateParameters(operation.parameters)} }` : "" }>`, ); diff --git a/packages/extensions/openapi-to-typespec/src/transforms/transform-arm-resources.ts b/packages/extensions/openapi-to-typespec/src/transforms/transform-arm-resources.ts index d65c82c4bc..89645eb924 100644 --- a/packages/extensions/openapi-to-typespec/src/transforms/transform-arm-resources.ts +++ b/packages/extensions/openapi-to-typespec/src/transforms/transform-arm-resources.ts @@ -28,7 +28,6 @@ import { TspArmResourceLifeCycleOperation, TspArmResourceListOperation, isArmResourceActionOperation, - TypespecDataType, } from "../interfaces"; import { getOptions, updateOptions } from "../options"; import { createClientNameDecorator, createCSharpNameDecorator } from "../pretransforms/rename-pretransform"; @@ -42,14 +41,8 @@ import { getResourceOperations, isResourceSchema, } from "../utils/resource-discovery"; -import { isArraySchema, isResponseSchema } from "../utils/schemas"; import { getSuppresssionWithCode } from "../utils/suppressions"; -import { - getFullyQualifiedName, - getTemplateResponses, - isResourceListResult, - NamesOfResponseTemplate, -} from "../utils/type-mapping"; +import { getFullyQualifiedName, getTemplateResponses, NamesOfResponseTemplate } from "../utils/type-mapping"; import { getTypespecType, isTypespecType, transformObjectProperty } from "./transform-object"; import { transformParameter } from "./transform-operations"; @@ -790,9 +783,13 @@ function buildBodyDecorator( } function getOtherProperties(schema: ArmResourceSchema, noCommonTypes: boolean): TypespecObjectProperty[] { - const knownProperties = ["properties", "name"]; + const knownProperties = ["properties", "name", "id", "type", "systemData"]; + const resourceKind = getResourceKind(schema); + if (resourceKind === "TrackedResource") { + knownProperties.push(...["location", "tags"]); + } if (!noCommonTypes) { - knownProperties.push(...["id", "type", "systemData", "location", "tags", "identity", "sku", "eTag", "plan"]); + knownProperties.push(...["identity", "sku", "eTag", "plan"]); } const otherProperties: TypespecObjectProperty[] = []; for (const property of schema.properties ?? []) { diff --git a/packages/extensions/openapi-to-typespec/src/transforms/transform-operations.ts b/packages/extensions/openapi-to-typespec/src/transforms/transform-operations.ts index 666e1f90a0..ea9cce9339 100644 --- a/packages/extensions/openapi-to-typespec/src/transforms/transform-operations.ts +++ b/packages/extensions/openapi-to-typespec/src/transforms/transform-operations.ts @@ -61,6 +61,7 @@ function transformVerb(protocol?: Protocols) { } function transformResponse(response: Response): [string, string] { + const { isArm } = getOptions(); const statusCode = response.protocol.http?.statusCodes[0] as string; const codeModel = getSession().model; const dataTypes = getDataTypes(codeModel); @@ -68,7 +69,7 @@ function transformResponse(response: Response): [string, string] { return [statusCode, "void"]; } - if (isResourceListResult(response)) { + if (isResourceListResult(response) && isArm) { const valueSchema = ((response as SchemaResponse).schema as ObjectSchema).properties?.find( (p) => p.language.default.name === "value", ); diff --git a/packages/extensions/openapi-to-typespec/test/arm-apimanagement/tsp-output/DeletedServiceContract.tsp b/packages/extensions/openapi-to-typespec/test/arm-apimanagement/tsp-output/DeletedServiceContract.tsp index b585246ad3..1ac99eca76 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-apimanagement/tsp-output/DeletedServiceContract.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-apimanagement/tsp-output/DeletedServiceContract.tsp @@ -23,6 +23,12 @@ model DeletedServiceContract SegmentName = "deletedservices", NamePattern = "^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$" >; + + /** + * API Management Service Master Location. + */ + @visibility("read") + location?: string; } @armResourceOperations diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudService.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudService.tsp index bedbd200a0..20b1d75577 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudService.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudService.tsp @@ -22,35 +22,6 @@ model CloudService NamePattern = "" >; - /** - * Resource Id. - */ - @visibility("read") - id?: string; - - /** - * Resource type. - */ - @visibility("read") - type?: string; - - /** - * Resource location. - */ - location: string; - - /** - * Resource tags. - */ - #suppress "@azure-tools/typespec-azure-resource-manager/arm-no-record" "For backward compatibility" - tags?: Record; - - /** - * Metadata pertaining to creation and last modification of the resource. - */ - @visibility("read") - systemData?: SystemData; - /** * List of logical availability zone of the resource. List should contain only 1 zone where cloud service should be provisioned. This field is optional. */ diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudServiceRole.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudServiceRole.tsp index d1b0efaf53..f46a00e051 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudServiceRole.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/CloudServiceRole.tsp @@ -24,18 +24,6 @@ model CloudServiceRole NamePattern = "" >; - /** - * Resource id - */ - @visibility("read") - id?: string; - - /** - * Resource type - */ - @visibility("read") - type?: string; - /** * Resource location */ diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/DedicatedHost.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/DedicatedHost.tsp index 17325a98b7..8fc27c36dd 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/DedicatedHost.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/DedicatedHost.tsp @@ -84,7 +84,8 @@ interface DedicatedHosts { restart is ArmResourceActionAsyncBase< DedicatedHost, void, - OkResponseDefaultBaseParameters + OkResponse, + BaseParameters = DefaultBaseParameters >; /** diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSFamily.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSFamily.tsp index 9932acbc8c..66a52fce3e 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSFamily.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSFamily.tsp @@ -23,18 +23,6 @@ model OSFamily is Azure.ResourceManager.ProxyResource { NamePattern = "" >; - /** - * Resource Id. - */ - @visibility("read") - id?: string; - - /** - * Resource type. - */ - @visibility("read") - type?: string; - /** * Resource location. */ diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSVersion.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSVersion.tsp index 2939864f3a..06e6825b7f 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSVersion.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/OSVersion.tsp @@ -23,18 +23,6 @@ model OSVersion is Azure.ResourceManager.ProxyResource { NamePattern = "" >; - /** - * Resource Id. - */ - @visibility("read") - id?: string; - - /** - * Resource type. - */ - @visibility("read") - type?: string; - /** * Resource location. */ diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/PrivateEndpointConnection.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/PrivateEndpointConnection.tsp index d7409ec28c..f4a9959068 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/PrivateEndpointConnection.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/PrivateEndpointConnection.tsp @@ -23,18 +23,6 @@ model PrivateEndpointConnection SegmentName = "privateEndpointConnections", NamePattern = "" >; - - /** - * private endpoint connection Id - */ - @visibility("read") - id?: string; - - /** - * private endpoint connection type - */ - @visibility("read") - type?: string; } @armResourceOperations diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/RoleInstance.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/RoleInstance.tsp index 9378d8d684..1f0c518385 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/RoleInstance.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/RoleInstance.tsp @@ -24,31 +24,6 @@ model RoleInstance NamePattern = "" >; - /** - * Resource Id - */ - @visibility("read") - id?: string; - - /** - * Resource Type. - */ - @visibility("read") - type?: string; - - /** - * Resource Location. - */ - @visibility("read") - location?: string; - - /** - * Resource tags. - */ - #suppress "@azure-tools/typespec-azure-resource-manager/arm-no-record" "For backward compatibility" - @visibility("read") - tags?: Record; - /** * The role instance SKU. */ diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetExtension.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetExtension.tsp index 7a6f3620df..9024a4fbe9 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetExtension.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetExtension.tsp @@ -23,12 +23,6 @@ model VirtualMachineScaleSetExtension SegmentName = "extensions", NamePattern = "" >; - - /** - * Resource type - */ - @visibility("read") - type?: string; } @armResourceOperations diff --git a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetVMExtension.tsp b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetVMExtension.tsp index 58e5201cc0..f97d83aa70 100644 --- a/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetVMExtension.tsp +++ b/packages/extensions/openapi-to-typespec/test/arm-compute/tsp-output/VirtualMachineScaleSetVMExtension.tsp @@ -24,12 +24,6 @@ model VirtualMachineScaleSetVMExtension NamePattern = "" >; - /** - * Resource type - */ - @visibility("read") - type?: string; - /** * The location of the extension. */ diff --git a/packages/extensions/openapi-to-typespec/test/keyvault/tsp-output/routes.tsp b/packages/extensions/openapi-to-typespec/test/keyvault/tsp-output/routes.tsp index 42d5ee76c7..66a503969a 100644 --- a/packages/extensions/openapi-to-typespec/test/keyvault/tsp-output/routes.tsp +++ b/packages/extensions/openapi-to-typespec/test/keyvault/tsp-output/routes.tsp @@ -2311,7 +2311,7 @@ interface RoleAssignmentsOperations { @query("$filter") $filter?: string; }, - ResourceListResult + RoleAssignmentListResult >; }