function convertResourceUpdateOperation()

in packages/extensions/openapi-to-typespec/src/transforms/transform-arm-resources.ts [416:517]


function convertResourceUpdateOperation(
  resourceMetadata: ArmResource,
  operations: Record<string, Operation>,
): TspArmResourceOperation[] {
  if (resourceMetadata.UpdateOperation) {
    const operation = resourceMetadata.UpdateOperation;
    if (!resourceMetadata.CreateOperation || resourceMetadata.CreateOperation.OperationID !== operation.OperationID) {
      const swaggerOperation = operations[operation.OperationID];
      const isLongRunning = swaggerOperation.extensions?.["x-ms-long-running-operation"] ?? false;
      const armOperation = buildNewArmOperation(
        resourceMetadata,
        operation,
        swaggerOperation,
        isLongRunning ? "ArmCustomPatchAsync" : "ArmCustomPatchSync",
      );

      const finalStateVia =
        swaggerOperation.extensions?.["x-ms-long-running-operation-options"]?.["final-state-via"] ?? "location";
      armOperation.lroHeaders =
        isLongRunning && finalStateVia === "azure-async-operation" ? "Azure-AsyncOperation" : undefined;

      const bodyParam = swaggerOperation.requests?.[0].parameters?.find((p) => p.protocol.http?.in === "body");
      if (!bodyParam) {
        armOperation.fixMe = [
          "// FIXME: (ArmResourcePatch): ArmResourcePatchSync/ArmResourcePatchAsync should have a body parameter with either properties property or tag property",
        ];
        armOperation.patchModel = "{}";
      } else {
        armOperation.patchModel = bodyParam.schema.language.default.name;
      }

      buildBodyDecorator(
        bodyParam,
        armOperation,
        resourceMetadata,
        "properties",
        "The resource properties to be updated.",
      );

      const asyncNames: NamesOfResponseTemplate = {
        _200Name: "ArmResponse",
        _200NameNoBody: "OkResponse",
        _201Name: "ArmResourceCreatedResponse",
        _201NameNoBody: "CreatedResponse",
        _202Name: "ArmAcceptedLroResponse",
        _202NameNoBody: "ArmAcceptedLroResponse",
        _204Name: "ArmNoContentResponse",
      };
      const syncNames: NamesOfResponseTemplate = {
        _200Name: "ArmResponse",
        _200NameNoBody: "OkResponse",
        _201Name: "ArmResourceCreatedSyncResponse",
        _201NameNoBody: "CreatedResponse",
        _202Name: "AcceptedResponse",
        _202NameNoBody: "AcceptedResponse",
        _204Name: "ArmNoContentResponse",
      };
      let responses = isLongRunning
        ? getTemplateResponses(swaggerOperation, asyncNames)
        : getTemplateResponses(swaggerOperation, syncNames);
      const templateAsyncResponses: TypespecTemplateModel[] = [
        {
          kind: "template",
          name: asyncNames._200Name,
          arguments: [{ kind: "object", name: resourceMetadata.SwaggerModelName }],
        },
        {
          kind: "template",
          name: asyncNames._202NameNoBody,
        },
      ];
      const templateSyncResponses: TypespecTemplateModel[] = [
        {
          kind: "template",
          name: syncNames._200Name,
          arguments: [{ kind: "object", name: resourceMetadata.SwaggerModelName }],
        },
      ];
      if (isLongRunning && isSameResponses(responses, templateAsyncResponses)) responses = [];
      if (!isLongRunning && isSameResponses(responses, templateSyncResponses)) responses = [];
      if (responses.length > 0) armOperation.response = responses;
      if (armOperation.lroHeaders && responses) {
        const _202response = responses.find(
          (r) => r.name === asyncNames._202NameNoBody || r.name === asyncNames._202Name,
        );
        if (_202response) {
          _202response.arguments = [
            { kind: "object", name: "ArmAsyncOperationHeader & Azure.Core.Foundations.RetryAfterHeader" },
          ]; //TO-DO: do it in a better way
          armOperation.lroHeaders = undefined;
        }
      }

      buildSuppressionsForArmOperation(armOperation, asyncNames, syncNames);
      armOperation.decorators = [
        { name: "patch", arguments: [{ value: "#{ implicitOptionality: false }", options: { unwrap: true } }] },
      ];
      return [armOperation as TspArmResourceLifeCycleOperation];
    }
  }
  return [];
}