diff --git a/docs/generators/csharp.md b/docs/generators/csharp.md index 97122c156778..2ba8b33b13ac 100644 --- a/docs/generators/csharp.md +++ b/docs/generators/csharp.md @@ -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| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java index 8355e1148455..b1d801d3cb6d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java @@ -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; @@ -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 @@ -490,6 +502,16 @@ public int compare(CodegenParameter one, CodegenParameter another) { } }; + public static Comparator parameterComparatorByName = new Comparator() { + @Override + public int compare(CodegenParameter one, CodegenParameter another) { + return one.paramName.compareTo(another.paramName); + } + }; + + public static Comparator parameterComparatorByNotNullableRequiredNoDefault = + Comparator.comparing(p -> p.isNullable || !p.required || p.defaultValue != null); + public static Comparator parameterComparatorByDefaultValue = new Comparator() { @Override public int compare(CodegenParameter one, CodegenParameter another) { @@ -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 { @@ -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 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 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; } @@ -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; }