Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
141 stars 161 forks source link

When Client parameter Type is enum or model, codegen throw exception #2880

Open chunyu3 opened 1 year ago

chunyu3 commented 1 year ago

When the type of client parameter is enum or model, authorest.csharp throw exception.

e.g. When define apiVersion as an enum APIVersion

@versioned(Versions)
@service({title: "Anomaly Detector"})
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "Ocp-Apim-Subscription-Key">)
@server(
  "{Endpoint}/anomalydetector/{ApiVersion}",
  "The Anomaly Detector API detects anomalies automatically in time series data. It supports two kinds of mode, one is for stateless using, another is for stateful using. In stateless mode, there are three functionalities. Entire Detect is for detecting the whole series with model trained by the time series, Last Detect is detecting last point with model trained by points before. ChangePoint Detect is for detecting trend changes in time series. In stateful mode, user can store time series, the stored time series will be used for detection anomalies. Under this mode, user can still use the above three functionalities by only giving a time range without preparing time series in client side. Besides the above three functionalities, stateful model also provide group based detection and labeling service. By leveraging labeling service user can provide labels for each detection result, these labels will be used for retuning or regenerating detection models. Inconsistency detection is a kind of group based detection, this detection will find inconsistency ones in a set of time series. By using anomaly detector service, business customers can discover incidents and establish a logic flow for root cause analysis.",
  {
    @doc("""
Supported Cognitive Services endpoints (protocol and hostname, for example:
https://westus2.api.cognitive.microsoft.com).
""")
    Endpoint: string,
    @doc("Api Version")
    @path
    ApiVersion: APIVersion,
  }
)
@doc("CADL project to test api versions.")
namespace ApiVersionInCadl;

enum Versions {
  v1_1: "v1.1",
}

@knownValues(Versions)
model APIVersion is string {}

exception occur: image

chunyu3 commented 1 year ago

Root cause: when create parameter, it will change the type of parameter from enum type to the type of enumValueType(e.g. string), model => {object}. https://github.com/Azure/autorest.csharp/blob/787427f570ab913fd4c0eb642f417fba892e2d55/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs#L359

https://github.com/Azure/autorest.csharp/blob/787427f570ab913fd4c0eb642f417fba892e2d55/src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs#L417

When _fields.GetFieldByParameter(parameter) to get client Field, it will fail to get one, return null Which cause exception.

lirenhe commented 1 year ago

Reassign the PR as there is already a fix for it.