You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The c# implementation will return 204, if no content could be found under this key. However, the code that Visual Studio's code generator generates looks as follows:
publicvirtualasyncSystem.Threading.Tasks.Task<ProductionOrder>GetOrderDataAsync(stringproductionOrderNumber,System.Threading.CancellationTokencancellationToken){if(productionOrderNumber==null)thrownewSystem.ArgumentNullException("productionOrderNumber");varclient_=_httpClient;vardisposeClient_=false;try{using(varrequest_=newSystem.Net.Http.HttpRequestMessage()){request_.Method=newSystem.Net.Http.HttpMethod("GET");request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));varurlBuilder_=newSystem.Text.StringBuilder();if(!string.IsNullOrEmpty(_baseUrl))urlBuilder_.Append(_baseUrl);// Operation Path: "productionOrders/{productionOrderNumber}"urlBuilder_.Append("productionOrders/");urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(productionOrderNumber,System.Globalization.CultureInfo.InvariantCulture)));PrepareRequest(client_,request_,urlBuilder_);varurl_=urlBuilder_.ToString();request_.RequestUri=newSystem.Uri(url_,System.UriKind.RelativeOrAbsolute);PrepareRequest(client_,request_,url_);varresponse_=awaitclient_.SendAsync(request_,System.Net.Http.HttpCompletionOption.ResponseHeadersRead,cancellationToken).ConfigureAwait(false);vardisposeResponse_=true;try{varheaders_=newSystem.Collections.Generic.Dictionary<string,System.Collections.Generic.IEnumerable<string>>();foreach(varitem_inresponse_.Headers)headers_[item_.Key]=item_.Value;if(response_.Content!=null&&response_.Content.Headers!=null){foreach(varitem_inresponse_.Content.Headers)headers_[item_.Key]=item_.Value;}ProcessResponse(client_,response_);varstatus_=(int)response_.StatusCode;if(status_==200){varobjectResponse_=awaitReadObjectResponseAsync<ProductionOrder>(response_,headers_,cancellationToken).ConfigureAwait(false);if(objectResponse_.Object==null){thrownewApiException("Response was null which was not expected.",status_,objectResponse_.Text,headers_,null);}returnobjectResponse_.Object;}elseif(status_==204){stringresponseText_=(response_.Content==null)?string.Empty:awaitresponse_.Content.ReadAsStringAsync().ConfigureAwait(false);thrownewApiException("There is no content to send for this request, but the headers may be useful.",status_,responseText_,headers_,null);}elseif(status_==500){varobjectResponse_=awaitReadObjectResponseAsync<ProblemDetail>(response_,headers_,cancellationToken).ConfigureAwait(false);if(objectResponse_.Object==null){thrownewApiException("Response was null which was not expected.",status_,objectResponse_.Text,headers_,null);}thrownewApiException<ProblemDetail>("Server error",status_,objectResponse_.Text,headers_,objectResponse_.Object,null);}else{varresponseData_=response_.Content==null?null:awaitresponse_.Content.ReadAsStringAsync().ConfigureAwait(false);thrownewApiException("The HTTP status code of the response was not expected ("+status_+").",status_,responseData_,headers_,null);}}finally{if(disposeResponse_)response_.Dispose();}}}finally{if(disposeClient_)client_.Dispose();}}
As you can see, it'll always throw an exception for status 204 instead of simply returning null, which would be the expected behavior. I also tried to modify the TypeSpec return type definition to ProductionOrder | null | ProblemDetail to allow returning null, but that lead to a similar result with an exception.
How can I fix that weird behavior without workarounds? Is there a flag that I can set for the generator? I read that people have been having similar problems for years now. Is there a solution that I missed? Am I doing something wrong?
The text was updated successfully, but these errors were encountered:
I'm generating my server code with TypeSpec as a basis. For example the following method:
The c# implementation will return 204, if no content could be found under this key. However, the code that Visual Studio's code generator generates looks as follows:
As you can see, it'll always throw an exception for status 204 instead of simply returning
null
, which would be the expected behavior. I also tried to modify the TypeSpec return type definition toProductionOrder | null | ProblemDetail
to allow returningnull
, but that lead to a similar result with an exception.How can I fix that weird behavior without workarounds? Is there a flag that I can set for the generator? I read that people have been having similar problems for years now. Is there a solution that I missed? Am I doing something wrong?
The text was updated successfully, but these errors were encountered: