Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -49,6 +49,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|
|useLegacyOperationParameterSorting|Preserve legacy operation parameter sorting to avoid a breaking change.| |true|
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename this option to operationParameterSorting, which supports legacy and alphabetical?

and later we have the option to add another one called original if users want the order in the spec.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

|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 @@ -114,6 +114,8 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
protected boolean equatable = Boolean.FALSE;
// By default, generated code is considered public
protected boolean nonPublicApi = Boolean.FALSE;
private static final String USE_LEGACY_OPERATION_PARAMETER_SORTING_KEY = "useLegacyOperationParameterSorting";
private boolean useLegacyOperationParameterSorting = Boolean.TRUE;

protected boolean caseInsensitiveResponseHeaders = Boolean.FALSE;
protected String releaseNote = "Minor update";
Expand Down Expand Up @@ -323,6 +325,8 @@ public CSharpClientCodegen() {
CodegenConstants.EQUATABLE_DESC,
this.equatable);

addSwitch(CSharpClientCodegen.USE_LEGACY_OPERATION_PARAMETER_SORTING_KEY, "Preserve legacy operation parameter sorting to avoid a breaking change.", this.useLegacyOperationParameterSorting);

addSwitch("useSourceGeneration",
"Use source generation where available (only `generichost` library supports this option).",
this.getUseSourceGeneration());
Expand Down Expand Up @@ -487,6 +491,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 @@ -767,6 +781,7 @@ public void processOpts() {
syncBooleanProperty(additionalProperties, "netStandard", this::setNetStandard, this.netStandard);

syncBooleanProperty(additionalProperties, CodegenConstants.EQUATABLE, this::setEquatable, this.equatable);
syncBooleanProperty(additionalProperties, CSharpClientCodegen.USE_LEGACY_OPERATION_PARAMETER_SORTING_KEY, this::setUseLegacyOperationParameterSorting, this.useLegacyOperationParameterSorting);
syncBooleanProperty(additionalProperties, CodegenConstants.VALIDATABLE, this::setValidatable, this.validatable);
syncBooleanProperty(additionalProperties, CodegenConstants.SUPPORTS_ASYNC, this::setSupportsAsync, this.supportsAsync);
syncBooleanProperty(additionalProperties, SUPPORTS_RETRY, this::setSupportsRetry, this.supportsRetry);
Expand Down Expand Up @@ -870,30 +885,56 @@ 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.useLegacyOperationParameterSorting) {
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, 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 @@ -1144,6 +1185,10 @@ public void setEquatable(boolean equatable) {
this.equatable = equatable;
}

public void setUseLegacyOperationParameterSorting(boolean useLegacyOperationParameterSorting) {
this.useLegacyOperationParameterSorting = useLegacyOperationParameterSorting;
}

public void setCaseInsensitiveResponseHeaders(final Boolean caseInsensitiveResponseHeaders) {
this.caseInsensitiveResponseHeaders = caseInsensitiveResponseHeaders;
}
Expand Down