Skip to content

Commit

Permalink
[csharp] Add option to fallback to int for Timeout (#20069)
Browse files Browse the repository at this point in the history
* add option to fallback to int for Timeout

* update doc
  • Loading branch information
wing328 authored Nov 9, 2024
1 parent bbccd28 commit b34df34
Show file tree
Hide file tree
Showing 5 changed files with 1,781 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useCollection|Deserialize array types to Collection<T> instead of List<T>.| |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|
|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|
|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|
|useSourceGeneration|Use source generation where available (only `generichost` library supports this option).| |false|
|validatable|Generates self-validatable models.| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
protected boolean netStandard = Boolean.FALSE;
protected boolean supportsFileParameters = Boolean.TRUE;
protected boolean supportsDateOnly = Boolean.FALSE;
protected boolean useIntForTimeout = Boolean.FALSE;

@Setter protected boolean validatable = Boolean.TRUE;
@Setter protected boolean equatable = Boolean.FALSE;
Expand All @@ -120,6 +121,8 @@ 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";

enum SortingMethod {
DEFAULT,
ALPHABETICAL,
Expand Down Expand Up @@ -236,6 +239,10 @@ public CSharpClientCodegen() {
"One of legacy, alphabetical, default.",
this.modelPropertySorting.toString().toLowerCase(Locale.ROOT));

addOption(CSharpClientCodegen.USE_INT_FOR_TIMEOUT,
"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));

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

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

public void addSupportingFiles(final String clientPackageDir, final String packageFolder,
final AtomicReference<Boolean> excludeTests, final String testPackageFolder, final String testPackageName, final String modelPackageDir, final String authPackageDir) {
if (RESTSHARP.equals(getLibrary())) { // restsharp
if (useIntForTimeout) { // option to fall back to int for Timeout using v7.9.0 template
supportingFiles.add(new SupportingFile("ApiClient.v790.mustache", clientPackageDir, "ApiClient.cs"));
supportingFiles.add(new SupportingFile("IReadableConfiguration.v790.mustache", clientPackageDir, "IReadableConfiguration.cs"));
supportingFiles.add(new SupportingFile("Configuration.v790.mustache", clientPackageDir, "Configuration.cs"));
} else {
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache", clientPackageDir, "IReadableConfiguration.cs"));
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
}
} else { // other libs, e.g. httpclient
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache", clientPackageDir, "IReadableConfiguration.cs"));
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
}
supportingFiles.add(new SupportingFile("IApiAccessor.mustache", clientPackageDir, "IApiAccessor.cs"));
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
supportingFiles.add(new SupportingFile("ApiException.mustache", clientPackageDir, "ApiException.cs"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", clientPackageDir, "ApiResponse.cs"));
supportingFiles.add(new SupportingFile("ExceptionFactory.mustache", clientPackageDir, "ExceptionFactory.cs"));
Expand All @@ -1017,8 +1038,6 @@ public void addSupportingFiles(final String clientPackageDir, final String packa
supportingFiles.add(new SupportingFile("RetryConfiguration.mustache", clientPackageDir, "RetryConfiguration.cs"));
}

supportingFiles.add(new SupportingFile("IReadableConfiguration.mustache",
clientPackageDir, "IReadableConfiguration.cs"));
supportingFiles.add(new SupportingFile("GlobalConfiguration.mustache",
clientPackageDir, "GlobalConfiguration.cs"));

Expand Down Expand Up @@ -1187,6 +1206,10 @@ public void setSupportsDateOnly(Boolean supportsDateOnly) {
this.supportsDateOnly = supportsDateOnly;
}

public void setUseIntForTimeout(Boolean useIntForTimeout) {
this.useIntForTimeout = useIntForTimeout;
}

public void setSupportsRetry(Boolean supportsRetry) {
this.supportsRetry = supportsRetry;
}
Expand Down
Loading

0 comments on commit b34df34

Please sign in to comment.