Skip to content

[csharp][generichost] Fixed parameter ordering #18823

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|netCoreProjectFile|Use the new format (.NET Core) for .NET project files (.csproj).| |false|
|nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false|
|nullableReferenceTypes|Use nullable annotations in the project. Only supported on C# 8 / ASP.NET Core 3.1 or newer. Starting in .NET 6.0 the default is true.| |false|
|operationParameterSorting|One of legacy, alphabetical, default (only `generichost` library supports this option).| |legacy|
|optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true|
|optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false|
|optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
@Getter @Setter
protected boolean nonPublicApi = Boolean.FALSE;

private static final String OPERATION_PARAMETER_SORTING_KEY = "operationParameterSorting";
enum SortingMethod {
DEFAULT,
ALPHABETICAL,
LEGACY
}
private SortingMethod operationParameterSorting = SortingMethod.LEGACY;

protected boolean caseInsensitiveResponseHeaders = Boolean.FALSE;
protected String releaseNote = "Minor update";
@Setter protected String licenseId;
Expand Down Expand Up @@ -218,6 +226,10 @@ public CSharpClientCodegen() {
"Enumerations with string values will start from 0 when true, 1 when false. If not set, enumerations with string values will start from 0 if the first value is 'unknown', case insensitive.",
null);

addOption(CSharpClientCodegen.OPERATION_PARAMETER_SORTING_KEY,
"One of legacy, alphabetical, default (only `generichost` library supports this option).",
this.operationParameterSorting.toString().toLowerCase(Locale.ROOT));

CliOption framework = new CliOption(
CodegenConstants.DOTNET_FRAMEWORK,
CodegenConstants.DOTNET_FRAMEWORK_DESC
Expand Down Expand Up @@ -490,6 +502,16 @@ public int compare(CodegenParameter one, CodegenParameter another) {
}
};

public static Comparator<CodegenParameter> parameterComparatorByName = new Comparator<CodegenParameter>() {
@Override
public int compare(CodegenParameter one, CodegenParameter another) {
return one.paramName.compareTo(another.paramName);
}
};

public static Comparator<CodegenParameter> parameterComparatorByNotNullableRequiredNoDefault =
Comparator.comparing(p -> p.isNullable || !p.required || p.defaultValue != null);

public static Comparator<CodegenParameter> parameterComparatorByDefaultValue = new Comparator<CodegenParameter>() {
@Override
public int compare(CodegenParameter one, CodegenParameter another) {
Expand Down Expand Up @@ -660,6 +682,10 @@ public void processOpts() {
setLicenseId((String) additionalProperties.get(CodegenConstants.LICENSE_ID));
}

if (additionalProperties.containsKey(CSharpClientCodegen.OPERATION_PARAMETER_SORTING_KEY)) {
setOperationParameterSorting((String) additionalProperties.get(CSharpClientCodegen.OPERATION_PARAMETER_SORTING_KEY));
}

if (additionalProperties.containsKey(CodegenConstants.API_NAME)) {
setApiName((String) additionalProperties.get(CodegenConstants.API_NAME));
} else {
Expand Down Expand Up @@ -861,30 +887,58 @@ public CodegenOperation fromOperation(String path,
return op;
}

Collections.sort(op.allParams, parameterComparatorByDataType);
Collections.sort(op.bodyParams, parameterComparatorByDataType);
Collections.sort(op.pathParams, parameterComparatorByDataType);
Collections.sort(op.queryParams, parameterComparatorByDataType);
Collections.sort(op.headerParams, parameterComparatorByDataType);
Collections.sort(op.implicitHeadersParams, parameterComparatorByDataType);
Collections.sort(op.formParams, parameterComparatorByDataType);
Collections.sort(op.cookieParams, parameterComparatorByDataType);
Collections.sort(op.requiredParams, parameterComparatorByDataType);
Collections.sort(op.optionalParams, parameterComparatorByDataType);
Collections.sort(op.notNullableParams, parameterComparatorByDataType);

Comparator<CodegenParameter> comparator = parameterComparatorByRequired.thenComparing(parameterComparatorByDefaultValue);
Collections.sort(op.allParams, comparator);
Collections.sort(op.bodyParams, comparator);
Collections.sort(op.pathParams, comparator);
Collections.sort(op.queryParams, comparator);
Collections.sort(op.headerParams, comparator);
Collections.sort(op.implicitHeadersParams, comparator);
Collections.sort(op.formParams, comparator);
Collections.sort(op.cookieParams, comparator);
Collections.sort(op.requiredParams, comparator);
Collections.sort(op.optionalParams, comparator);
Collections.sort(op.notNullableParams, comparator);
if (this.operationParameterSorting == SortingMethod.LEGACY) {
Collections.sort(op.allParams, parameterComparatorByDataType);
Collections.sort(op.bodyParams, parameterComparatorByDataType);
Collections.sort(op.pathParams, parameterComparatorByDataType);
Collections.sort(op.queryParams, parameterComparatorByDataType);
Collections.sort(op.headerParams, parameterComparatorByDataType);
Collections.sort(op.implicitHeadersParams, parameterComparatorByDataType);
Collections.sort(op.formParams, parameterComparatorByDataType);
Collections.sort(op.cookieParams, parameterComparatorByDataType);
Collections.sort(op.requiredParams, parameterComparatorByDataType);
Collections.sort(op.optionalParams, parameterComparatorByDataType);
Collections.sort(op.notNullableParams, parameterComparatorByDataType);

Comparator<CodegenParameter> comparator = parameterComparatorByRequired.thenComparing(parameterComparatorByDefaultValue);
Collections.sort(op.allParams, comparator);
Collections.sort(op.bodyParams, comparator);
Collections.sort(op.pathParams, comparator);
Collections.sort(op.queryParams, comparator);
Collections.sort(op.headerParams, comparator);
Collections.sort(op.implicitHeadersParams, comparator);
Collections.sort(op.formParams, comparator);
Collections.sort(op.cookieParams, comparator);
Collections.sort(op.requiredParams, comparator);
Collections.sort(op.optionalParams, comparator);
Collections.sort(op.notNullableParams, comparator);
} else {
if (this.operationParameterSorting == SortingMethod.ALPHABETICAL) {
Collections.sort(op.allParams, parameterComparatorByName);
Collections.sort(op.bodyParams, parameterComparatorByName);
Collections.sort(op.pathParams, parameterComparatorByName);
Collections.sort(op.queryParams, parameterComparatorByName);
Collections.sort(op.headerParams, parameterComparatorByName);
Collections.sort(op.implicitHeadersParams, parameterComparatorByName);
Collections.sort(op.formParams, parameterComparatorByName);
Collections.sort(op.cookieParams, parameterComparatorByName);
Collections.sort(op.requiredParams, parameterComparatorByName);
Collections.sort(op.optionalParams, parameterComparatorByName);
Collections.sort(op.notNullableParams, parameterComparatorByName);
}

Collections.sort(op.allParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.bodyParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.pathParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.queryParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.headerParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.implicitHeadersParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.formParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.cookieParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.requiredParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.optionalParams, parameterComparatorByNotNullableRequiredNoDefault);
Collections.sort(op.notNullableParams, parameterComparatorByNotNullableRequiredNoDefault);
}

return op;
}
Expand Down Expand Up @@ -1061,6 +1115,14 @@ public void setApiName(String apiName) {
this.apiName = apiName;
}

public void setOperationParameterSorting(String operationParameterSorting) {
if (operationParameterSorting == null) {
operationParameterSorting = "DEFAULT";
}

this.operationParameterSorting = SortingMethod.valueOf(operationParameterSorting.toUpperCase(Locale.ROOT));
}

public void setSupportsAsync(Boolean supportsAsync) {
this.supportsAsync = supportsAsync;
}
Expand Down
Loading