Skip to content

Commit b34df34

Browse files
authored
[csharp] Add option to fallback to int for Timeout (#20069)
* add option to fallback to int for Timeout * update doc
1 parent bbccd28 commit b34df34

File tree

5 files changed

+1781
-4
lines changed

5 files changed

+1781
-4
lines changed

docs/generators/csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5151
|useCollection|Deserialize array types to Collection<T> instead of List<T>.| |false|
5252
|useDateTimeForDate|Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)| |false|
5353
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
54+
|useIntForTimeout|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.| |false|
5455
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
5556
|useSourceGeneration|Use source generation where available (only `generichost` library supports this option).| |false|
5657
|validatable|Generates self-validatable models.| |true|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
111111
protected boolean netStandard = Boolean.FALSE;
112112
protected boolean supportsFileParameters = Boolean.TRUE;
113113
protected boolean supportsDateOnly = Boolean.FALSE;
114+
protected boolean useIntForTimeout = Boolean.FALSE;
114115

115116
@Setter protected boolean validatable = Boolean.TRUE;
116117
@Setter protected boolean equatable = Boolean.FALSE;
@@ -120,6 +121,8 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
120121

121122
private static final String OPERATION_PARAMETER_SORTING_KEY = "operationParameterSorting";
122123
private static final String MODEL_PROPERTY_SORTING_KEY = "modelPropertySorting";
124+
private static final String USE_INT_FOR_TIMEOUT = "useIntForTimeout";
125+
123126
enum SortingMethod {
124127
DEFAULT,
125128
ALPHABETICAL,
@@ -236,6 +239,10 @@ public CSharpClientCodegen() {
236239
"One of legacy, alphabetical, default.",
237240
this.modelPropertySorting.toString().toLowerCase(Locale.ROOT));
238241

242+
addOption(CSharpClientCodegen.USE_INT_FOR_TIMEOUT,
243+
"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.",
244+
String.valueOf(this.useIntForTimeout));
245+
239246
CliOption framework = new CliOption(
240247
CodegenConstants.DOTNET_FRAMEWORK,
241248
CodegenConstants.DOTNET_FRAMEWORK_DESC
@@ -841,6 +848,7 @@ public void processOpts() {
841848
syncBooleanProperty(additionalProperties, "supportsFileParameters", this::setSupportsFileParameters, this.supportsFileParameters);
842849
syncBooleanProperty(additionalProperties, "useSourceGeneration", this::setUseSourceGeneration, this.useSourceGeneration);
843850
syncBooleanProperty(additionalProperties, "supportsDateOnly", this::setSupportsDateOnly, this.supportsDateOnly);
851+
syncBooleanProperty(additionalProperties, "useIntForTimeout", this::setUseIntForTimeout, this.useIntForTimeout);
844852

845853
final String testPackageName = testPackageName();
846854
String packageFolder = sourceFolder + File.separator + packageName;
@@ -989,9 +997,22 @@ public CodegenOperation fromOperation(String path,
989997

990998
public void addSupportingFiles(final String clientPackageDir, final String packageFolder,
991999
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir, final String authPackageDir) {
1000+
if (RESTSHARP.equals(getLibrary())) { // restsharp
1001+
if (useIntForTimeout) { // option to fall back to int for Timeout using v7.9.0 template
1002+
supportingFiles.add(new SupportingFile("ApiClient.v790.mustache", clientPackageDir, "ApiClient.cs"));
1003+
supportingFiles.add(new SupportingFile("IReadableConfiguration.v790.mustache", clientPackageDir, "IReadableConfiguration.cs"));
1004+
supportingFiles.add(new SupportingFile("Configuration.v790.mustache", clientPackageDir, "Configuration.cs"));
1005+
} else {
1006+
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
1007+
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache", clientPackageDir, "IReadableConfiguration.cs"));
1008+
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
1009+
}
1010+
} else { // other libs, e.g. httpclient
1011+
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
1012+
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache", clientPackageDir, "IReadableConfiguration.cs"));
1013+
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
1014+
}
9921015
supportingFiles.add(new SupportingFile("IApiAccessor.mustache", clientPackageDir, "IApiAccessor.cs"));
993-
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
994-
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
9951016
supportingFiles.add(new SupportingFile("ApiException.mustache", clientPackageDir, "ApiException.cs"));
9961017
supportingFiles.add(new SupportingFile("ApiResponse.mustache", clientPackageDir, "ApiResponse.cs"));
9971018
supportingFiles.add(new SupportingFile("ExceptionFactory.mustache", clientPackageDir, "ExceptionFactory.cs"));
@@ -1017,8 +1038,6 @@ public void addSupportingFiles(final String clientPackageDir, final String packa
10171038
supportingFiles.add(new SupportingFile("RetryConfiguration.mustache", clientPackageDir, "RetryConfiguration.cs"));
10181039
}
10191040

1020-
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache",
1021-
clientPackageDir, "IReadableConfiguration.cs"));
10221041
supportingFiles.add(new SupportingFile("GlobalConfiguration.mustache",
10231042
clientPackageDir, "GlobalConfiguration.cs"));
10241043

@@ -1187,6 +1206,10 @@ public void setSupportsDateOnly(Boolean supportsDateOnly) {
11871206
this.supportsDateOnly = supportsDateOnly;
11881207
}
11891208

1209+
public void setUseIntForTimeout(Boolean useIntForTimeout) {
1210+
this.useIntForTimeout = useIntForTimeout;
1211+
}
1212+
11901213
public void setSupportsRetry(Boolean supportsRetry) {
11911214
this.supportsRetry = supportsRetry;
11921215
}

0 commit comments

Comments
 (0)