Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
|sourceFolder|source folder for generated code| |src|
|targetFramework|The target .NET framework version. To target multiple frameworks, use `;` as the separator, e.g. `netstandard2.1;netcoreapp3.1`|<dl><dt>**netstandard1.3**</dt><dd>.NET Standard 1.3</dd><dt>**netstandard1.4**</dt><dd>.NET Standard 1.4</dd><dt>**netstandard1.5**</dt><dd>.NET Standard 1.5</dd><dt>**netstandard1.6**</dt><dd>.NET Standard 1.6</dd><dt>**netstandard2.0**</dt><dd>.NET Standard 2.0</dd><dt>**netstandard2.1**</dt><dd>.NET Standard 2.1</dd><dt>**net47**</dt><dd>.NET Framework 4.7</dd><dt>**net48**</dt><dd>.NET Framework 4.8</dd><dt>**net8.0**</dt><dd>.NET 8.0 (End of Support 10 November 2026)</dd><dt>**net9.0**</dt><dd>.NET 9.0 (End of Support 10 November 2026)</dd><dt>**net10.0**</dt><dd>.NET 10.0 (End of Support 14 November 2028)</dd></dl>|net10.0|
|throwOnAnyError|Configure RestSharp to rethrow deserialization and transport errors instead of swallowing them into RestResponse.ErrorException (which the default ToApiResponse&lt;T&gt; discards as null Data). Recommended for production use to surface bugs that would otherwise be invisible. (restsharp only)| |false|
|useCollection|Deserialize array types to Collection&lt;T&gt; instead of List&lt;T&gt;.| |false|
|useDateTimeForDate|Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)| |false|
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
protected boolean supportsFileParameters = Boolean.TRUE;
protected boolean supportsDateOnly = Boolean.FALSE;
protected boolean useIntForTimeout = Boolean.FALSE;
protected boolean throwOnAnyError = Boolean.FALSE;

@Setter protected boolean validatable = Boolean.TRUE;
@Setter protected boolean equatable = Boolean.FALSE;
Expand All @@ -132,6 +133,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
private static final String OPERATION_PARAMETER_SORTING_KEY = "operationParameterSorting";
private static final String MODEL_PROPERTY_SORTING_KEY = "modelPropertySorting";
private static final String USE_INT_FOR_TIMEOUT = "useIntForTimeout";
private static final String THROW_ON_ANY_ERROR = "throwOnAnyError";

enum SortingMethod {
DEFAULT,
Expand Down Expand Up @@ -249,6 +251,10 @@ public CSharpClientCodegen() {
"Use int for Timeout (fall back to v7.9.0 templates). This option (for restsharp only) will be deprecated so please migrated to TimeSpan instead.",
String.valueOf(this.useIntForTimeout));

addSwitch(CSharpClientCodegen.THROW_ON_ANY_ERROR,
"Configure RestSharp to rethrow deserialization and transport errors instead of swallowing them into RestResponse.ErrorException (which the default ToApiResponse<T> discards as null Data). Recommended for production use to surface bugs that would otherwise be invisible. (restsharp only)",
this.throwOnAnyError);

CliOption framework = new CliOption(
CodegenConstants.DOTNET_FRAMEWORK,
CodegenConstants.DOTNET_FRAMEWORK_DESC
Expand Down Expand Up @@ -871,6 +877,7 @@ public void processOpts() {
syncBooleanProperty(additionalProperties, "useSourceGeneration", this::setUseSourceGeneration, this.useSourceGeneration);
syncBooleanProperty(additionalProperties, "supportsDateOnly", this::setSupportsDateOnly, this.supportsDateOnly);
syncBooleanProperty(additionalProperties, "useIntForTimeout", this::setUseIntForTimeout, this.useIntForTimeout);
syncBooleanProperty(additionalProperties, "throwOnAnyError", this::setThrowOnAnyError, this.throwOnAnyError);

final String testPackageName = testPackageName();
String packageFolder = sourceFolder + File.separator + packageName;
Expand Down Expand Up @@ -1244,6 +1251,10 @@ public void setUseIntForTimeout(Boolean useIntForTimeout) {
this.useIntForTimeout = useIntForTimeout;
}

public void setThrowOnAnyError(Boolean throwOnAnyError) {
this.throwOnAnyError = throwOnAnyError;
}

public void setSupportsRetry(Boolean supportsRetry) {
this.supportsRetry = supportsRetry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ namespace {{packageName}}.Client
Proxy = configuration.Proxy,
UserAgent = configuration.UserAgent,
UseDefaultCredentials = configuration.UseDefaultCredentials,
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback{{#throwOnAnyError}},
ThrowOnAnyError = true{{/throwOnAnyError}}
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
};
setOptions(clientOptions);

Expand Down Expand Up @@ -560,16 +561,21 @@ namespace {{packageName}}.Client

private async Task<RestResponse<T>> DeserializeRestResponseFromPolicyAsync<T>(RestClient client, RestRequest request, PolicyResult<RestResponse> policyResult, CancellationToken cancellationToken = default)
{
if (policyResult.Outcome == OutcomeType.Successful)
if (policyResult.Outcome == OutcomeType.Successful)
{
return await client.Deserialize<T>(policyResult.Result, cancellationToken).ConfigureAwait(false);
}
else
{
{{#throwOnAnyError}}
throw policyResult.FinalException;
{{/throwOnAnyError}}
{{^throwOnAnyError}}
return new RestResponse<T>(request)
{
ErrorException = policyResult.FinalException
};
{{/throwOnAnyError}}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,21 @@ namespace {{packageName}}.Client

private async Task<RestResponse<T>> DeserializeRestResponseFromPolicyAsync<T>(RestClient client, RestRequest request, PolicyResult<RestResponse> policyResult, CancellationToken cancellationToken = default)
{
if (policyResult.Outcome == OutcomeType.Successful)
if (policyResult.Outcome == OutcomeType.Successful)
{
return await client.Deserialize<T>(policyResult.Result, cancellationToken);
}
else
{
{{#throwOnAnyError}}
throw policyResult.FinalException;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
{{/throwOnAnyError}}
{{^throwOnAnyError}}
return new RestResponse<T>(request)
{
ErrorException = policyResult.FinalException
};
{{/throwOnAnyError}}
}
}

Expand Down
Loading