Skip to content

The generated code should use ToSerialString() to construct a request #57303

@ArthurMa1978

Description

@ArthurMa1978

When serializing an enum to a string, the generated code should call ToSerialString.
However, when an enum is used as a path parameter (for example, as shown below):

/**
 * Regenerates either the primary or secondary admin API key. You can only regenerate one key at a time.
 */
#suppress "@azure-tools/typespec-azure-resource-manager/arm-resource-operation" "FIXME: Update justification, follow aka.ms/tsp/conversion-fix for details"
@autoRoute
@post
op privateLinkResourcesRegenerateCustomization(
  ...ApiVersionParameter,
  ...SubscriptionIdParameter,
  ...CommonTypes.ResourceGroupNameParameter,

  /** The name of the Azure AI Search service associated with the specified resource group. */
  @path
  @segment("searchServices")
  @key
  @pattern("^(?=.{2,60}$)[a-z0-9][a-z0-9]+(-[a-z0-9]+)*$")
  searchServiceName: string,

  /**
   * Specifies which key to regenerate. Valid values include 'primary' and 'secondary'.
   */
  @path
  keyKind: AdminKeyKind,

  #suppress "@azure-tools/typespec-azure-core/documentation-required" "FIXME: Update justification, follow aka.ms/tsp/conversion-fix for details"
  body: void,
  #suppress "@azure-tools/typespec-azure-core/documentation-required" "FIXME: Update justification, follow aka.ms/tsp/conversion-fix for details"
  params?: `search-management-request-options`,
): ArmResponse<AdminKeyResult> | CloudError;

The generated code currently uses ToString() instead of ToSerialString() when constructing the request.
For example, the generated code for the definition above is:

        public virtual Response<SearchServiceAdminKeyResult> RegenerateAdminKey(SearchServiceAdminKeyKind keyKind, SearchManagementRequestOptions searchManagementRequestOptions = default, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _adminKeysClientDiagnostics.CreateScope("SearchServiceResource.RegenerateAdminKey");
            scope.Start();
            try
            {
                RequestContext context = new RequestContext
                {
                    CancellationToken = cancellationToken
                };
                HttpMessage message = _adminKeysRestClient.CreateRegenerateAdminKeyRequest(Guid.Parse(Id.SubscriptionId), Id.ResourceGroupName, Id.Name, keyKind.ToString(), default, context);
                Response result = Pipeline.ProcessMessage(message, context);
                Response<SearchServiceAdminKeyResult> response = Response.FromValue(SearchServiceAdminKeyResult.FromResponse(result), result);
                if (response.Value == null)
                {
                    throw new RequestFailedException(response.GetRawResponse());
                }
                return response;
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

The expected behavior is to use ToSerialString() when serializing the enum value for the request. The correct generated code should be:

        public virtual Response<SearchServiceAdminKeyResult> RegenerateAdminKey(SearchServiceAdminKeyKind keyKind, SearchManagementRequestOptions searchManagementRequestOptions = default, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _adminKeysClientDiagnostics.CreateScope("SearchServiceResource.RegenerateAdminKey");
            scope.Start();
            try
            {
                RequestContext context = new RequestContext
                {
                    CancellationToken = cancellationToken
                };
                HttpMessage message = _adminKeysRestClient.CreateRegenerateAdminKeyRequest(Guid.Parse(Id.SubscriptionId), Id.ResourceGroupName, Id.Name, keyKind.ToSerialString(), default, context);
                Response result = Pipeline.ProcessMessage(message, context);
                Response<SearchServiceAdminKeyResult> response = Response.FromValue(SearchServiceAdminKeyResult.FromResponse(result), result);
                if (response.Value == null)
                {
                    throw new RequestFailedException(response.GetRawResponse());
                }
                return response;
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }

Metadata

Metadata

Assignees

Labels

CodeGenIssues that relate to code generationMgmtThis issue is related to a management package.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions