Skip to content

AutoRest client generation workaround

Filipp Kuksov edited this page Feb 24, 2021 · 1 revision

Workaround for AutoRest file type generation with OpenApi 3

To fix this problem in the Storefront after generation new AutoRest client with Stream content endpoint we should update some code.

Change return type from string to Stream:

  1. Change all HttpOperationResponse<string> to HttpOperationResponse<Stream>
  2. public static string GetInvoicePdf to public static Stream GetInvoicePdf

Add option (change code)

_httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

to

_httpResponse = await Client.HttpClient.SendAsync(_httpRequest, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

Change for reading body

if ((int)_statusCode == 200)	            
{	            
    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);	                
    try	
    {	
        _result.Body = SafeJsonConvert.DeserializeObject<string>(_responseContent, Client.DeserializationSettings);	
    }	
    catch (JsonException ex)	
    {	
    _httpRequest.Dispose();	
    if (_httpResponse != null)	
    {	
        _httpResponse.Dispose();	
    }	
    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);	
    }	
}

to

if ((int)_statusCode == 200)
{
    _result.Body = await _httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
}

Change endpoint call code:

public static async Task<string> GetInvoicePdfAsync(this IOrderModule operations, string orderNumber, CancellationToken cancellationToken = default(CancellationToken))
{
    using (var _result = await operations.GetInvoicePdfWithHttpMessagesAsync(orderNumber, null, cancellationToken).ConfigureAwait(false))
    {
        return _result.Body;
    }
}

to

public static async Task<Stream> GetInvoicePdfAsync(this IOrderModule operations, string orderNumber, CancellationToken cancellationToken = default(CancellationToken))
{
    var _result = await operations.GetInvoicePdfWithHttpMessagesAsync(orderNumber, null, cancellationToken).ConfigureAwait(false);
    _result.Request.Dispose();
    return _result.Body;
}

Change var to Stream before returning File:

Stream stream = await _orderApi.GetInvoicePdfAsync(order.Number);