Skip to content

Commit 4494bf1

Browse files
yzhoholievjimschubert
authored andcommitted
[csharp-netcore] Propagate raw content to the ApiException error content. (#4381)
* [csharp-netcore] Propagate raw content to the ApiException error content.
1 parent c663124 commit 4494bf1

File tree

9 files changed

+117
-21
lines changed

9 files changed

+117
-21
lines changed

modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ namespace {{packageName}}.Client
347347
private ApiResponse<T> toApiResponse<T>({{#supportsAsync}}IRestResponse<T> response{{/supportsAsync}}{{^supportsAsync}}IRestResponse response, CustomJsonCodec des{{/supportsAsync}})
348348
{
349349
T result = {{#supportsAsync}}response.Data{{/supportsAsync}}{{^supportsAsync}}(T)des.Deserialize(response, typeof(T)){{/supportsAsync}};
350-
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>({{#caseInsensitiveResponseHeaders}}StringComparer.OrdinalIgnoreCase{{/caseInsensitiveResponseHeaders}}), result)
350+
string rawContent = response.Content;
351+
352+
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>({{#caseInsensitiveResponseHeaders}}StringComparer.OrdinalIgnoreCase{{/caseInsensitiveResponseHeaders}}), result, rawContent)
351353
{
352354
ErrorText = response.ErrorMessage,
353355
Cookies = new List<Cookie>()

modules/openapi-generator/src/main/resources/csharp-netcore/ApiResponse.mustache

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ namespace {{packageName}}.Client
4242
/// Gets or sets any cookies passed along on the response.
4343
/// </summary>
4444
List<Cookie> Cookies { get; set; }
45+
46+
/// <summary>
47+
/// The raw content of this response
48+
/// </summary>
49+
string RawContent { get; }
4550
}
4651

4752
/// <summary>
@@ -94,6 +99,11 @@ namespace {{packageName}}.Client
9499
{
95100
get { return Data; }
96101
}
102+
103+
/// <summary>
104+
/// The raw content
105+
/// </summary>
106+
public string RawContent { get;}
97107

98108
#endregion Properties
99109

@@ -105,22 +115,42 @@ namespace {{packageName}}.Client
105115
/// <param name="statusCode">HTTP status code.</param>
106116
/// <param name="headers">HTTP headers.</param>
107117
/// <param name="data">Data (parsed HTTP body)</param>
108-
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data)
118+
/// <param name="rawContent">Raw content.</param>
119+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data, string rawContent)
109120
{
110121
StatusCode = statusCode;
111122
Headers = headers;
112123
Data = data;
124+
RawContent = rawContent;
113125
}
114126

115127
/// <summary>
116128
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
117129
/// </summary>
118130
/// <param name="statusCode">HTTP status code.</param>
131+
/// <param name="headers">HTTP headers.</param>
119132
/// <param name="data">Data (parsed HTTP body)</param>
120-
public ApiResponse(HttpStatusCode statusCode, T data)
133+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data) : this(statusCode, headers, data, null)
134+
{
135+
}
136+
137+
/// <summary>
138+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
139+
/// </summary>
140+
/// <param name="statusCode">HTTP status code.</param>
141+
/// <param name="data">Data (parsed HTTP body)</param>
142+
/// <param name="rawContent">Raw content.</param>
143+
public ApiResponse(HttpStatusCode statusCode, T data, string rawContent) : this(statusCode, null, data, rawContent)
144+
{
145+
}
146+
147+
/// <summary>
148+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
149+
/// </summary>
150+
/// <param name="statusCode">HTTP status code.</param>
151+
/// <param name="data">Data (parsed HTTP body)</param>
152+
public ApiResponse(HttpStatusCode statusCode, T data) : this(statusCode, data, null)
121153
{
122-
StatusCode = statusCode;
123-
Data = data;
124154
}
125155

126156
#endregion Constructors

modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ namespace {{packageName}}.Client
4545
if (status >= 400)
4646
{
4747
return new ApiException(status,
48-
string.Format("Error calling {0}: {1}", methodName, response.Content),
49-
response.Content);
48+
string.Format("Error calling {0}: {1}", methodName, response.RawContent),
49+
response.RawContent);
5050
}
5151
{{^netStandard}}if (status == 0)
5252
{

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ private RestRequest newRequest(
350350
private ApiResponse<T> toApiResponse<T>(IRestResponse<T> response)
351351
{
352352
T result = response.Data;
353-
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result)
353+
string rawContent = response.Content;
354+
355+
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result, rawContent)
354356
{
355357
ErrorText = response.ErrorMessage,
356358
Cookies = new List<Cookie>()

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiResponse.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public interface IApiResponse
5151
/// Gets or sets any cookies passed along on the response.
5252
/// </summary>
5353
List<Cookie> Cookies { get; set; }
54+
55+
/// <summary>
56+
/// The raw content of this response
57+
/// </summary>
58+
string RawContent { get; }
5459
}
5560

5661
/// <summary>
@@ -103,6 +108,11 @@ public object Content
103108
{
104109
get { return Data; }
105110
}
111+
112+
/// <summary>
113+
/// The raw content
114+
/// </summary>
115+
public string RawContent { get;}
106116

107117
#endregion Properties
108118

@@ -114,22 +124,42 @@ public object Content
114124
/// <param name="statusCode">HTTP status code.</param>
115125
/// <param name="headers">HTTP headers.</param>
116126
/// <param name="data">Data (parsed HTTP body)</param>
117-
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data)
127+
/// <param name="rawContent">Raw content.</param>
128+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data, string rawContent)
118129
{
119130
StatusCode = statusCode;
120131
Headers = headers;
121132
Data = data;
133+
RawContent = rawContent;
122134
}
123135

124136
/// <summary>
125137
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
126138
/// </summary>
127139
/// <param name="statusCode">HTTP status code.</param>
140+
/// <param name="headers">HTTP headers.</param>
128141
/// <param name="data">Data (parsed HTTP body)</param>
129-
public ApiResponse(HttpStatusCode statusCode, T data)
142+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data) : this(statusCode, headers, data, null)
143+
{
144+
}
145+
146+
/// <summary>
147+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
148+
/// </summary>
149+
/// <param name="statusCode">HTTP status code.</param>
150+
/// <param name="data">Data (parsed HTTP body)</param>
151+
/// <param name="rawContent">Raw content.</param>
152+
public ApiResponse(HttpStatusCode statusCode, T data, string rawContent) : this(statusCode, null, data, rawContent)
153+
{
154+
}
155+
156+
/// <summary>
157+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
158+
/// </summary>
159+
/// <param name="statusCode">HTTP status code.</param>
160+
/// <param name="data">Data (parsed HTTP body)</param>
161+
public ApiResponse(HttpStatusCode statusCode, T data) : this(statusCode, data, null)
130162
{
131-
StatusCode = statusCode;
132-
Data = data;
133163
}
134164

135165
#endregion Constructors

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class Configuration : IReadableConfiguration
5252
if (status >= 400)
5353
{
5454
return new ApiException(status,
55-
string.Format("Error calling {0}: {1}", methodName, response.Content),
56-
response.Content);
55+
string.Format("Error calling {0}: {1}", methodName, response.RawContent),
56+
response.RawContent);
5757
}
5858

5959
return null;

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ private RestRequest newRequest(
351351
private ApiResponse<T> toApiResponse<T>(IRestResponse<T> response)
352352
{
353353
T result = response.Data;
354-
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result)
354+
string rawContent = response.Content;
355+
356+
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result, rawContent)
355357
{
356358
ErrorText = response.ErrorMessage,
357359
Cookies = new List<Cookie>()

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiResponse.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public interface IApiResponse
5151
/// Gets or sets any cookies passed along on the response.
5252
/// </summary>
5353
List<Cookie> Cookies { get; set; }
54+
55+
/// <summary>
56+
/// The raw content of this response
57+
/// </summary>
58+
string RawContent { get; }
5459
}
5560

5661
/// <summary>
@@ -103,6 +108,11 @@ public object Content
103108
{
104109
get { return Data; }
105110
}
111+
112+
/// <summary>
113+
/// The raw content
114+
/// </summary>
115+
public string RawContent { get;}
106116

107117
#endregion Properties
108118

@@ -114,22 +124,42 @@ public object Content
114124
/// <param name="statusCode">HTTP status code.</param>
115125
/// <param name="headers">HTTP headers.</param>
116126
/// <param name="data">Data (parsed HTTP body)</param>
117-
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data)
127+
/// <param name="rawContent">Raw content.</param>
128+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data, string rawContent)
118129
{
119130
StatusCode = statusCode;
120131
Headers = headers;
121132
Data = data;
133+
RawContent = rawContent;
122134
}
123135

124136
/// <summary>
125137
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
126138
/// </summary>
127139
/// <param name="statusCode">HTTP status code.</param>
140+
/// <param name="headers">HTTP headers.</param>
128141
/// <param name="data">Data (parsed HTTP body)</param>
129-
public ApiResponse(HttpStatusCode statusCode, T data)
142+
public ApiResponse(HttpStatusCode statusCode, Multimap<string, string> headers, T data) : this(statusCode, headers, data, null)
143+
{
144+
}
145+
146+
/// <summary>
147+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
148+
/// </summary>
149+
/// <param name="statusCode">HTTP status code.</param>
150+
/// <param name="data">Data (parsed HTTP body)</param>
151+
/// <param name="rawContent">Raw content.</param>
152+
public ApiResponse(HttpStatusCode statusCode, T data, string rawContent) : this(statusCode, null, data, rawContent)
153+
{
154+
}
155+
156+
/// <summary>
157+
/// Initializes a new instance of the <see cref="ApiResponse{T}" /> class.
158+
/// </summary>
159+
/// <param name="statusCode">HTTP status code.</param>
160+
/// <param name="data">Data (parsed HTTP body)</param>
161+
public ApiResponse(HttpStatusCode statusCode, T data) : this(statusCode, data, null)
130162
{
131-
StatusCode = statusCode;
132-
Data = data;
133163
}
134164

135165
#endregion Constructors

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class Configuration : IReadableConfiguration
5252
if (status >= 400)
5353
{
5454
return new ApiException(status,
55-
string.Format("Error calling {0}: {1}", methodName, response.Content),
56-
response.Content);
55+
string.Format("Error calling {0}: {1}", methodName, response.RawContent),
56+
response.RawContent);
5757
}
5858
if (status == 0)
5959
{

0 commit comments

Comments
 (0)