Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate headers dictionary only when needed #4602

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
{% if response.IsFile -%}
{% if response.IsSuccess -%}
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, {% if InjectHttpClient or DisposeHttpClient == false %}null{% else %}client_{% endif %}, response_);
var fileResponse_ = new FileResponse(status_, GetResponseHeaders(response_), responseStream_, {% if InjectHttpClient or DisposeHttpClient == false %}null{% else %}client_{% endif %}, response_);
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
return fileResponse_;
{% else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, cancellationToken).ConfigureAwait(false);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, GetResponseHeaders(response_), objectResponse_.Object, null);
{% endif -%}
{% elsif response.IsPlainText -%}
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = ({{ response.Type }})System.Convert.ChangeType(responseData_, typeof({{ response.Type }}));
{% if response.IsSuccess -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, result_);
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, GetResponseHeaders(response_), result_);
{% else -%}
return result_;
{% endif -%}
{% else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, responseData_, headers_, result_, null);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, responseData_, GetResponseHeaders(response_), result_, null);
{% endif -%}
{% else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, cancellationToken).ConfigureAwait(false);
{% if response.IsNullable == false -%}
if (objectResponse_.Object == null)
{
throw new {{ ExceptionClass }}("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
throw new {{ ExceptionClass }}("Response was null which was not expected.", status_, objectResponse_.Text, GetResponseHeaders(response_), null);
}
{% endif -%}
{% if response.IsSuccess -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, objectResponse_.Object);
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, GetResponseHeaders(response_), objectResponse_.Object);
{% else -%}
return objectResponse_.Object;
{% endif -%}
Expand All @@ -40,33 +40,33 @@ return objectResponse_.Object;
{% if response.InheritsExceptionSchema -%}
var responseObject_ = objectResponse_.Object != null ? objectResponse_.Object : new {{ response.Type }}();
responseObject_.Data.Add("HttpStatus", status_.ToString());
responseObject_.Data.Add("HttpHeaders", headers_);
responseObject_.Data.Add("HttpHeaders", GetResponseHeaders(response_));
responseObject_.Data.Add("HttpResponse", objectResponse_.Text);
{% if WrapDtoExceptions -%}
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, responseObject_);
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, GetResponseHeaders(response_), responseObject_);
{% else -%}
throw responseObject_;
{% endif -%}
{% else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, GetResponseHeaders(response_), objectResponse_.Object, null);
{% endif -%}
{% endif -%}
{% endif -%}
{% elsif response.IsSuccess -%}
{% if operation.HasResultType -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, GetResponseHeaders(response_), {{ operation.UnwrappedResultDefaultValue }});
{% else -%}
return {{ operation.UnwrappedResultDefaultValue }};
{% endif -%}
{% else -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}(status_, headers_);
return new {{ ResponseClass }}(status_, GetResponseHeaders(response_));
{% else -%}
return;
{% endif -%}
{% endif -%}
{% else -%}{% comment %} implied: `if !response.HasType` so just read it as text {% endcomment %}
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, responseText_, headers_, null);
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, responseText_, GetResponseHeaders(response_), null);
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Threading.CancellationToken cancellationToken)
{
if (response == null || response.Content == null)
{
Expand All @@ -26,7 +26,7 @@ protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> Rea
catch ({% if UseSystemTextJson %}System.Text.Json.JsonException{% else %}Newtonsoft.Json.JsonException{% endif %} exception)
{
var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
throw new {{ ExceptionClass }}(message, (int)response.StatusCode, responseText, headers, exception);
throw new {{ ExceptionClass }}(message, (int)response.StatusCode, responseText, GetResponseHeaders(response), exception);
}
}
else
Expand Down Expand Up @@ -54,7 +54,7 @@ protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> Rea
catch ({% if UseSystemTextJson %}System.Text.Json.JsonException{% else %}Newtonsoft.Json.JsonException{% endif %} exception)
{
var message = "Could not deserialize the response body stream as " + typeof(T).FullName + ".";
throw new {{ ExceptionClass }}(message, (int)response.StatusCode, string.Empty, headers, exception);
throw new {{ ExceptionClass }}(message, (int)response.StatusCode, string.Empty, GetResponseHeaders(response), exception);
}
}
}
34 changes: 19 additions & 15 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,6 @@
var disposeResponse_ = true;
try
{
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}

{% if GeneratePrepareRequestAndProcessResponseAsAsyncMethods %}
await ProcessResponseAsync(client_, response_, cancellationToken).ConfigureAwait(false);
{% else %}
Expand All @@ -376,16 +367,16 @@
{% elsif operation.HasSuccessResponse -%}
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new {{ ExceptionClass }}("{{ operation.DefaultResponse.ExceptionDescription }}", status_, responseData_, headers_, null);
throw new {{ ExceptionClass }}("{{ operation.DefaultResponse.ExceptionDescription }}", status_, responseData_, GetResponseHeaders(response_), null);
}
{% elsif operation.HasResultType -%}
{% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, GetResponseHeaders(response_), {{ operation.UnwrappedResultDefaultValue }});
{% else -%}
return {{ operation.UnwrappedResultDefaultValue }};
{% endif -%}
{% elsif operation.WrapResponse -%}
return new {{ ResponseClass }}(status_, headers_);
return new {{ ResponseClass }}(status_, GetResponseHeaders(response_));
{% endif -%}
{% else -%}
{% if operation.HasSuccessResponse == false -%}
Expand All @@ -398,12 +389,12 @@
{
{% if operation.HasResultType -%}
{% if operation.WrapResponse and operation.UnwrappedResultType != "FileResponse" %}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, GetResponseHeaders(response_), {{ operation.UnwrappedResultDefaultValue }});
{% else -%}
return {{ operation.UnwrappedResultDefaultValue }};
{% endif -%}
{% elsif operation.WrapResponse -%}
return new {{ ResponseClass }}(status_, headers_);
return new {{ ResponseClass }}(status_, GetResponseHeaders(response_));
{% else -%}{% comment %} This method isn't expected to return a value. Just return. {% endcomment %}
return;
{% endif -%}
Expand All @@ -412,7 +403,7 @@
{% endif -%}
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new {{ ExceptionClass }}("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
throw new {{ ExceptionClass }}("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, GetResponseHeaders(response_), null);
}
{% endif -%}
}
Expand Down Expand Up @@ -444,6 +435,19 @@
public string Text { get; }
}

private static System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>> GetResponseHeaders(System.Net.Http.HttpResponseMessage response)
{
var headers = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item in response.Headers)
headers[item.Key] = item.Value;
if (response.Content != null && response.Content.Headers != null)
{
foreach (var item in response.Content.Headers)
headers[item.Key] = item.Value;
}
return headers;
}

{% template Client.Class.ReadObjectResponse %}

{% template Client.Class.ConvertToString %}
Expand Down