added equatable option (#16106)

This commit is contained in:
devhl-labs
2023-07-16 23:56:04 -04:00
committed by GitHub
parent 6983a3acf3
commit f8e19b4ebc
22 changed files with 39 additions and 26 deletions

View File

@@ -25,6 +25,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|dateFormat|The default Date format (only `generichost` library supports this option).| |yyyy'-'MM'-'dd| |dateFormat|The default Date format (only `generichost` library supports this option).| |yyyy'-'MM'-'dd|
|dateTimeFormat|The default DateTime format (only `generichost` library supports this option).| |yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK| |dateTimeFormat|The default DateTime format (only `generichost` library supports this option).| |yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK|
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true| |disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
|equatable|Overrides Equals and GetHashCode methods.| |true|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| |hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|interfacePrefix|Prefix interfaces with a community standard or widely accepted prefix.| |I| |interfacePrefix|Prefix interfaces with a community standard or widely accepted prefix.| |I|
|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| |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|

View File

@@ -320,6 +320,9 @@ public class CodegenConstants {
public static final String VALIDATABLE = "validatable"; public static final String VALIDATABLE = "validatable";
public static final String VALIDATABLE_DESC = "Generates self-validatable models."; public static final String VALIDATABLE_DESC = "Generates self-validatable models.";
public static final String EQUATABLE = "equatable";
public static final String EQUATABLE_DESC = "Overrides Equals and GetHashCode methods.";
public static final String IGNORE_FILE_OVERRIDE = "ignoreFileOverride"; public static final String IGNORE_FILE_OVERRIDE = "ignoreFileOverride";
public static final String IGNORE_FILE_OVERRIDE_DESC = "Specifies an override location for the .openapi-generator-ignore file. Most useful on initial generation."; public static final String IGNORE_FILE_OVERRIDE_DESC = "Specifies an override location for the .openapi-generator-ignore file. Most useful on initial generation.";

View File

@@ -416,7 +416,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
.put("joinWithComma", new JoinWithCommaLambda()) .put("joinWithComma", new JoinWithCommaLambda())
.put("trimLineBreaks", new TrimLineBreaksLambda()) .put("trimLineBreaks", new TrimLineBreaksLambda())
.put("trimTrailingWhiteSpace", new TrimTrailingWhiteSpaceLambda()) .put("trimTrailingWhiteSpace", new TrimTrailingWhiteSpaceLambda())
.put("first", new FirstLambda()) .put("first", new FirstLambda(" "))
.put("firstDot", new FirstLambda("\\."))
.put("indent3", new IndentedLambda(12, " ")) .put("indent3", new IndentedLambda(12, " "))
.put("indent4", new IndentedLambda(16, " ")); .put("indent4", new IndentedLambda(16, " "));
} }

View File

@@ -114,6 +114,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
protected boolean supportsFileParameters = Boolean.TRUE; protected boolean supportsFileParameters = Boolean.TRUE;
protected boolean validatable = Boolean.TRUE; protected boolean validatable = Boolean.TRUE;
protected boolean equatable = Boolean.TRUE;
protected Map<Character, String> regexModifiers; protected Map<Character, String> regexModifiers;
// By default, generated code is considered public // By default, generated code is considered public
protected boolean nonPublicApi = Boolean.FALSE; protected boolean nonPublicApi = Boolean.FALSE;
@@ -318,6 +319,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
CodegenConstants.CASE_INSENSITIVE_RESPONSE_HEADERS_DESC, CodegenConstants.CASE_INSENSITIVE_RESPONSE_HEADERS_DESC,
this.caseInsensitiveResponseHeaders); this.caseInsensitiveResponseHeaders);
addSwitch(CodegenConstants.EQUATABLE,
CodegenConstants.EQUATABLE_DESC,
this.equatable);
regexModifiers = new HashMap<>(); regexModifiers = new HashMap<>();
regexModifiers.put('i', "IgnoreCase"); regexModifiers.put('i', "IgnoreCase");
regexModifiers.put('m', "Multiline"); regexModifiers.put('m', "Multiline");
@@ -820,6 +825,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
syncBooleanProperty(additionalProperties, "netStandard", this::setNetStandard, this.netStandard); syncBooleanProperty(additionalProperties, "netStandard", this::setNetStandard, this.netStandard);
syncBooleanProperty(additionalProperties, CodegenConstants.EQUATABLE, this::setEquatable, this.equatable);
syncBooleanProperty(additionalProperties, CodegenConstants.VALIDATABLE, this::setValidatable, this.validatable); syncBooleanProperty(additionalProperties, CodegenConstants.VALIDATABLE, this::setValidatable, this.validatable);
syncBooleanProperty(additionalProperties, CodegenConstants.SUPPORTS_ASYNC, this::setSupportsAsync, this.supportsAsync); syncBooleanProperty(additionalProperties, CodegenConstants.SUPPORTS_ASYNC, this::setSupportsAsync, this.supportsAsync);
syncBooleanProperty(additionalProperties, SUPPORTS_RETRY, this::setSupportsRetry, this.supportsRetry); syncBooleanProperty(additionalProperties, SUPPORTS_RETRY, this::setSupportsRetry, this.supportsRetry);
@@ -1183,6 +1189,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
this.validatable = validatable; this.validatable = validatable;
} }
public void setEquatable(boolean equatable) {
this.equatable = equatable;
}
public void setCaseInsensitiveResponseHeaders(final Boolean caseInsensitiveResponseHeaders) { public void setCaseInsensitiveResponseHeaders(final Boolean caseInsensitiveResponseHeaders) {
this.caseInsensitiveResponseHeaders = caseInsensitiveResponseHeaders; this.caseInsensitiveResponseHeaders = caseInsensitiveResponseHeaders;
} }

View File

@@ -37,13 +37,19 @@ import java.util.Arrays;
* </pre> * </pre>
*/ */
public class FirstLambda implements Mustache.Lambda { public class FirstLambda implements Mustache.Lambda {
public FirstLambda() { private final String delimiter;
public FirstLambda(String delimiter) {
this.delimiter = delimiter;
} }
@Override @Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException { public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String[] parts = fragment.execute().trim().split(" ");
String a = fragment.execute();
String[] parts = fragment.execute().split(this.delimiter);
writer.write(Arrays.stream(parts).findFirst().orElse("")); writer.write(Arrays.stream(parts).findFirst().orElse(""));
} }

View File

@@ -1,3 +1 @@
{{#readOnlyVars}} {{#equatable}}{{#readOnlyVars}}{{#-first}}IEquatable<{{classname}}{{nrt?}}> {{/-first}}{{/readOnlyVars}}{{/equatable}}
{{#-first}}
{{#parent}}, {{/parent}}{{^parent}} : {{/parent}}IEquatable<{{classname}}{{nrt?}}>{{/-first}}{{/readOnlyVars}}

View File

@@ -1,4 +1 @@
{{#validatable}} {{#validatable}}IValidatableObject {{/validatable}}
{{^parent}}
{{^readOnlyVars}}
: {{/readOnlyVars}}{{/parent}}{{#parent}}{{^readOnlyVars}}, {{/readOnlyVars}}{{/parent}}{{^parent}}{{#readOnlyVars}}{{#-first}}, {{/-first}}{{/readOnlyVars}}{{/parent}}IValidatableObject{{/validatable}}

View File

@@ -1,7 +1,7 @@
/// <summary> /// <summary>
/// {{description}}{{^description}}{{classname}}{{/description}} /// {{description}}{{^description}}{{classname}}{{/description}}
/// </summary> /// </summary>
{{>visibility}} partial class {{classname}}{{#parent}} : {{{.}}}{{/parent}}{{>ImplementsIEquatable}}{{>ImplementsValidatable}} {{>visibility}} partial class {{classname}}{{#lambda.firstDot}}{{#parent}} : .{{/parent}}{{#validatable}} : .{{/validatable}}{{#equatable}} : .{{/equatable}}{{/lambda.firstDot}}{{#lambda.joinWithComma}}{{#parent}}{{{.}}} {{/parent}}{{>ImplementsIEquatable}}{{#validatable}}IValidatableObject {{/validatable}}{{/lambda.joinWithComma}}
{ {
{{#composedSchemas.oneOf}} {{#composedSchemas.oneOf}}
{{^vendorExtensions.x-duplicated-data-type}} {{^vendorExtensions.x-duplicated-data-type}}
@@ -200,6 +200,7 @@
sb.Append("}\n"); sb.Append("}\n");
return sb.ToString(); return sb.ToString();
} }
{{#equatable}}
{{#readOnlyVars}} {{#readOnlyVars}}
{{#-first}} {{#-first}}
@@ -294,6 +295,7 @@
} }
{{/-first}} {{/-first}}
{{/readOnlyVars}} {{/readOnlyVars}}
{{/equatable}}
{{#validatable}} {{#validatable}}
{{^parentModel}} {{^parentModel}}

View File

@@ -10,7 +10,7 @@
{{/vendorExtensions.x-com-visible}} {{/vendorExtensions.x-com-visible}}
[JsonConverter(typeof({{classname}}JsonConverter))] [JsonConverter(typeof({{classname}}JsonConverter))]
[DataContract(Name = "{{{name}}}")] [DataContract(Name = "{{{name}}}")]
{{>visibility}} partial class {{classname}} : AbstractOpenAPISchema, {{#parent}}{{{.}}}, {{/parent}}IEquatable<{{classname}}>{{#validatable}}, IValidatableObject{{/validatable}} {{>visibility}} partial class {{classname}} : AbstractOpenAPISchema, {{#lambda.joinWithComma}}{{#parent}}{{{.}}} {{/parent}}{{#equatable}}IEquatable<{{classname}}> {{/equatable}}{{#validatable}}IValidatableObject {{/validatable}}{{/lambda.joinWithComma}}
{ {
{{#isNullable}} {{#isNullable}}
/// <summary> /// <summary>
@@ -137,6 +137,7 @@
// no match found, throw an exception // no match found, throw an exception
throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined.");
} }
{{#equatable}}
/// <summary> /// <summary>
/// Returns true if objects are equal /// Returns true if objects are equal
@@ -185,6 +186,7 @@
return hashCode; return hashCode;
} }
} }
{{/equatable}}
{{#validatable}} {{#validatable}}
/// <summary> /// <summary>

View File

@@ -16,7 +16,7 @@
{{/mappedModels}} {{/mappedModels}}
{{/discriminator}} {{/discriminator}}
{{/useUnityWebRequest}} {{/useUnityWebRequest}}
{{>visibility}} partial class {{classname}} : {{#parent}}{{{.}}}, {{/parent}}IEquatable<{{classname}}>{{#validatable}}, IValidatableObject{{/validatable}} {{>visibility}} partial class {{classname}}{{#lambda.firstDot}}{{#parent}} : .{{/parent}}{{#validatable}} : .{{/validatable}}{{#equatable}} : .{{/equatable}}{{/lambda.firstDot}}{{#lambda.joinWithComma}}{{#parent}}{{{.}}} {{/parent}}{{#equatable}}IEquatable<{{classname}}> {{/equatable}}{{#validatable}}IValidatableObject {{/validatable}}{{/lambda.joinWithComma}}
{ {
{{#vars}} {{#vars}}
{{#items.isEnum}} {{#items.isEnum}}
@@ -327,6 +327,7 @@
{ {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented); return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
} }
{{#equatable}}
/// <summary> /// <summary>
/// Returns true if objects are equal /// Returns true if objects are equal
@@ -415,6 +416,7 @@
return hashCode; return hashCode;
} }
} }
{{/equatable}}
{{#validatable}} {{#validatable}}
{{>validatable}} {{>validatable}}

View File

@@ -10,7 +10,7 @@
{{/vendorExtensions.x-com-visible}} {{/vendorExtensions.x-com-visible}}
[JsonConverter(typeof({{classname}}JsonConverter))] [JsonConverter(typeof({{classname}}JsonConverter))]
[DataContract(Name = "{{{name}}}")] [DataContract(Name = "{{{name}}}")]
{{>visibility}} partial class {{classname}} : AbstractOpenAPISchema, {{#parent}}{{{.}}}, {{/parent}}IEquatable<{{classname}}>{{#validatable}}, IValidatableObject{{/validatable}} {{>visibility}} partial class {{classname}} : {{#lambda.joinWithComma}}AbstractOpenAPISchema {{#parent}}{{{.}}} {{/parent}}{{#equatable}}IEquatable<{{classname}}> {{/equatable}}{{#validatable}}IValidatableObject {{/validatable}}{{/lambda.joinWithComma}}
{ {
{{#isNullable}} {{#isNullable}}
/// <summary> /// <summary>
@@ -183,6 +183,7 @@
return new{{classname}}; return new{{classname}};
} }
{{#equatable}}
/// <summary> /// <summary>
/// Returns true if objects are equal /// Returns true if objects are equal
/// </summary> /// </summary>
@@ -230,8 +231,9 @@
return hashCode; return hashCode;
} }
} }
{{/equatable}}
{{#validatable}} {{#validatable}}
/// <summary> /// <summary>
/// To validate all properties of the instance /// To validate all properties of the instance
/// </summary> /// </summary>

View File

@@ -233,7 +233,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -242,7 +242,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -279,7 +279,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -242,7 +242,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -187,7 +187,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -233,7 +233,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -325,7 +325,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -233,7 +233,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -233,7 +233,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -242,7 +242,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>

View File

@@ -279,7 +279,6 @@ namespace Org.OpenAPITools.Model
return hashCode; return hashCode;
} }
} }
} }
/// <summary> /// <summary>