Skip to content
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

[csharp] Change sorting default #18942

Merged
merged 6 commits into from
Aug 11, 2024
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
4 changes: 2 additions & 2 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|library|HTTP library template (sub-template) to use|<dl><dt>**generichost**</dt><dd>HttpClient with Generic Host dependency injection (https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host) (Experimental. Subject to breaking changes without notice.)</dd><dt>**httpclient**</dt><dd>HttpClient (https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) (Experimental. Subject to breaking changes without notice.)</dd><dt>**unityWebRequest**</dt><dd>UnityWebRequest (...) (Experimental. Subject to breaking changes without notice.)</dd><dt>**restsharp**</dt><dd>RestSharp (https://github.com/restsharp/RestSharp)</dd></dl>|restsharp|
|licenseId|The identifier of the license| |null|
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |PascalCase|
|modelPropertySorting|One of legacy, alphabetical, default (only `generichost` library supports this option).| |legacy|
|modelPropertySorting|One of legacy, alphabetical, default.| |default|
|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|
|operationParameterSorting|One of legacy, alphabetical, default.| |default|
|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 @@ -3092,16 +3092,7 @@ public CodegenModel fromModel(String name, Schema schema) {
}

if (sortModelPropertiesByRequiredFlag) {
Comparator<CodegenProperty> comparator = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.required == another.required) return 0;
else if (one.required) return -1;
else return 1;
}
};
Collections.sort(m.vars, comparator);
Collections.sort(m.allVars, comparator);
SortModelPropertiesByRequiredFlag(m);
}

// post process model properties
Expand All @@ -3120,6 +3111,19 @@ public int compare(CodegenProperty one, CodegenProperty another) {
return m;
}

protected void SortModelPropertiesByRequiredFlag(CodegenModel model) {
Comparator<CodegenProperty> comparator = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.required == another.required) return 0;
else if (one.required) return -1;
else return 1;
}
};
Collections.sort(model.vars, comparator);
Collections.sort(model.allVars, comparator);
}

protected void setAddProps(Schema schema, IJsonSchemaValidationProperties property) {
if (schema.equals(new Schema())) {
// if we are trying to set additionalProperties on an empty schema stop recursing
Expand Down Expand Up @@ -4729,17 +4733,7 @@ public CodegenOperation fromOperation(String path,

// move "required" parameters in front of "optional" parameters
if (sortParamsByRequiredFlag) {
Collections.sort(allParams, new Comparator<CodegenParameter>() {
@Override
public int compare(CodegenParameter one, CodegenParameter another) {
if (one.required == another.required)
return 0;
else if (one.required)
return -1;
else
return 1;
}
});
SortParametersByRequiredFlag(allParams);
}

op.allParams = allParams;
Expand Down Expand Up @@ -4773,6 +4767,20 @@ else if (one.required)
return op;
}

public void SortParametersByRequiredFlag(List<CodegenParameter> parameters) {
Collections.sort(parameters, new Comparator<CodegenParameter>() {
@Override
public int compare(CodegenParameter one, CodegenParameter another) {
if (one.required == another.required)
return 0;
else if (one.required)
return -1;
else
return 1;
}
});
}

public boolean isParameterNameUnique(CodegenParameter p, List<CodegenParameter> parameters) {
for (CodegenParameter parameter : parameters) {
if (System.identityHashCode(p) == System.identityHashCode(parameter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ enum SortingMethod {
ALPHABETICAL,
LEGACY
}
private SortingMethod operationParameterSorting = SortingMethod.LEGACY;
private SortingMethod modelPropertySorting = SortingMethod.LEGACY;
private SortingMethod operationParameterSorting = SortingMethod.DEFAULT;
private SortingMethod modelPropertySorting = SortingMethod.DEFAULT;

protected boolean caseInsensitiveResponseHeaders = Boolean.FALSE;
protected String releaseNote = "Minor update";
Expand Down Expand Up @@ -229,11 +229,11 @@ public CSharpClientCodegen() {
null);

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

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

CliOption framework = new CliOption(
Expand Down Expand Up @@ -468,45 +468,48 @@ public CodegenModel fromModel(String name, Schema model) {
}
}

// avoid breaking changes
if (GENERICHOST.equals(getLibrary()) && codegenModel != null) {

if (this.modelPropertySorting == SortingMethod.LEGACY) {
if (codegenModel != null) {
if (this.modelPropertySorting == SortingMethod.ALPHABETICAL) {
Collections.sort(codegenModel.vars, propertyComparatorByName);
Collections.sort(codegenModel.allVars, propertyComparatorByName);
Collections.sort(codegenModel.requiredVars, propertyComparatorByName);
Collections.sort(codegenModel.optionalVars, propertyComparatorByName);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByName);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByName);
Collections.sort(codegenModel.parentVars, propertyComparatorByName);

Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
}
else {
if (this.modelPropertySorting == SortingMethod.ALPHABETICAL) {

if (GENERICHOST.equals(getLibrary())) {

if (this.modelPropertySorting == SortingMethod.LEGACY) {
Collections.sort(codegenModel.vars, propertyComparatorByName);
Collections.sort(codegenModel.allVars, propertyComparatorByName);
Collections.sort(codegenModel.requiredVars, propertyComparatorByName);
Collections.sort(codegenModel.optionalVars, propertyComparatorByName);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByName);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByName);
Collections.sort(codegenModel.parentVars, propertyComparatorByName);
}

Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefaultLegacy);
}
else {
Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefault);
}
}
} else {
SortModelPropertiesByRequiredFlag(codegenModel);
}

return codegenModel;
Expand Down Expand Up @@ -924,61 +927,61 @@ public CodegenOperation fromOperation(String path,
List<Server> servers) {
CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);

if (!GENERICHOST.equals(getLibrary())) {
return op;
}

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);
}
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);
if (GENERICHOST.equals(getLibrary())) {
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 {
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);
}
} else {
SortParametersByRequiredFlag(op.allParams);
}

return op;
Expand Down
Loading
Loading