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

[C#] Remove null checks for C# value types #2933

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
// nullable type
protected Set<String> nullableType = new HashSet<String>();

protected Set<String> valueTypes = new HashSet<String>();

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCSharpCodegen.class);

public AbstractCSharpCodegen() {
Expand Down Expand Up @@ -190,6 +192,10 @@ public AbstractCSharpCodegen() {
nullableType = new HashSet<String>(
Arrays.asList("decimal", "bool", "int", "float", "long", "double", "DateTime", "Guid")
);
// value Types
valueTypes = new HashSet<String>(
Arrays.asList("decimal", "bool", "int", "float", "long", "double")
);
}

public void setReturnICollection(boolean returnICollection) {
Expand Down Expand Up @@ -414,6 +420,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
final Map<String, Object> processed = super.postProcessAllModels(objs);
postProcessEnumRefs(processed);
updateValueTypeProperty(processed);
return processed;
}

Expand Down Expand Up @@ -454,6 +461,19 @@ private void postProcessEnumRefs(final Map<String, Object> models) {
var.isPrimitiveType = true;
}
}
for (CodegenProperty var : model.vars) {
Copy link
Contributor Author

@etherealjoy etherealjoy May 19, 2019

Choose a reason for hiding this comment

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

Question:
why is this line (some lines above) not covering all enums

for (CodegenProperty var : model.allVars) 

I had to add a separate one for this below

for (CodegenProperty var : model.vars) {

Copy link
Member

Choose a reason for hiding this comment

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

It should. That could be a bug with allVars.

I'll look into it in the coming week.

if (enumRefs.containsKey(var.dataType)) {
// Handle any enum properties referred to by $ref.
// This is different in C# than most other generators, because enums in C# are compiled to integral types,
// while enums in many other languages are true objects.
CodegenModel refModel = enumRefs.get(var.dataType);
var.allowableValues = refModel.allowableValues;
var.isEnum = true;

// We do these after updateCodegenPropertyEnum to avoid generalities that don't mesh with C#.
var.isPrimitiveType = true;
}
}

// We're looping all models here.
if (model.isEnum) {
Expand Down Expand Up @@ -541,6 +561,23 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}
}

/**
* Update property if it is a C# value type
*
* @param models list of all models
*/
protected void updateValueTypeProperty(Map<String, Object> models) {
for (Map.Entry<String, Object> entry : models.entrySet()) {
String openAPIName = entry.getKey();
CodegenModel model = ModelUtils.getModelByName(openAPIName, models);
if (model != null) {
for (CodegenProperty var : model.vars) {
var.vendorExtensions.put("isValueType", isValueType(var));
}
}
}
}

@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
super.postProcessOperationsWithModels(objs, allModels);
Expand Down Expand Up @@ -1055,6 +1092,17 @@ public boolean isDataTypeString(String dataType) {
"double".equals(dataType) || "decimal".equals(dataType) || "float".equals(dataType);
}

/**
* Return true if the property being passed is a C# value type
*
* @param var property
* @return true if property is a value type
*/

protected boolean isValueType(CodegenProperty var) {
return (valueTypes.contains(var.dataType) || var.isEnum ) ;
}

@Override
public void setParameterExampleValue(CodegenParameter codegenParameter) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ namespace {{modelPackage}}
return {{#vars}}{{^isContainer}}
(
{{name}} == other.{{name}} ||
{{name}} != null &&
{{^vendorExtensions.isValueType}}{{name}} != null &&{{/vendorExtensions.isValueType}}
{{name}}.Equals(other.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}}
(
{{name}} == other.{{name}} ||
{{name}} != null &&
{{^vendorExtensions.isValueType}}{{name}} != null &&
other.{{name}} != null &&
{{name}}.SequenceEqual(other.{{name}})
{{/vendorExtensions.isValueType}}{{name}}.SequenceEqual(other.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}false{{/vars}};
}

Expand All @@ -108,7 +108,7 @@ namespace {{modelPackage}}
var hashCode = 41;
// Suitable nullity checks etc, of course :)
{{#vars}}
if ({{name}} != null)
{{^vendorExtensions.isValueType}}if ({{name}} != null){{/vendorExtensions.isValueType}}
hashCode = hashCode * 59 + {{name}}.GetHashCode();
{{/vars}}
return hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,19 @@
return {{#vars}}{{#parent}}base.Equals(input) && {{/parent}}{{^isContainer}}
(
this.{{name}} == input.{{name}} ||
{{^isEnum}}
{{^vendorExtensions.isValueType}}
(this.{{name}} != null &&
this.{{name}}.Equals(input.{{name}}))
{{/isEnum}}
{{#isEnum}}
{{/vendorExtensions.isValueType}}
{{#vendorExtensions.isValueType}}
this.{{name}}.Equals(input.{{name}})
{{/isEnum}}
{{/vendorExtensions.isValueType}}
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}}
(
this.{{name}} == input.{{name}} ||
this.{{name}} != null &&
{{^vendorExtensions.isValueType}}this.{{name}} != null &&
input.{{name}} != null &&
this.{{name}}.SequenceEqual(input.{{name}})
{{/vendorExtensions.isValueType}}this.{{name}}.SequenceEqual(input.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}};
{{/useCompareNetObjects}}
}
Expand All @@ -214,13 +214,13 @@
int hashCode = 41;
{{/parent}}
{{#vars}}
{{^isEnum}}
{{^vendorExtensions.isValueType}}
if (this.{{name}} != null)
hashCode = hashCode * 59 + this.{{name}}.GetHashCode();
{{/isEnum}}
{{#isEnum}}
{{/vendorExtensions.isValueType}}
{{#vendorExtensions.isValueType}}
hashCode = hashCode * 59 + this.{{name}}.GetHashCode();
{{/isEnum}}
{{/vendorExtensions.isValueType}}
{{/vars}}
return hashCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Name | Type | Description | Notes
**EnumStringRequired** | **string** | |
**EnumInteger** | **int** | | [optional]
**EnumNumber** | **double** | | [optional]
**OuterEnum** | [**OuterEnum**](OuterEnum.md) | | [optional]
**OuterEnum** | **OuterEnum** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Code != null)
hashCode = hashCode * 59 + this.Code.GetHashCode();
hashCode = hashCode * 59 + this.Code.GetHashCode();
if (this.Type != null)
hashCode = hashCode * 59 + this.Type.GetHashCode();
if (this.Message != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = base.GetHashCode();
if (this.Declawed != null)
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Declawed != null)
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
hashCode = hashCode * 59 + this.Declawed.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Id != null)
hashCode = hashCode * 59 + this.Id.GetHashCode();
hashCode = hashCode * 59 + this.Id.GetHashCode();
if (this.Name != null)
hashCode = hashCode * 59 + this.Name.GetHashCode();
return hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public enum EnumNumberEnum
[DataMember(Name="enum_number", EmitDefaultValue=false)]
public EnumNumberEnum? EnumNumber { get; set; }
/// <summary>
/// Gets or Sets OuterEnum
/// </summary>
[DataMember(Name="outerEnum", EmitDefaultValue=false)]
public OuterEnum? OuterEnum { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="EnumTest" /> class.
/// </summary>
[JsonConstructorAttribute]
Expand All @@ -163,12 +168,6 @@ protected EnumTest() { }
this.OuterEnum = outerEnum;
}

/// <summary>
/// Gets or Sets OuterEnum
/// </summary>
[DataMember(Name="outerEnum", EmitDefaultValue=false)]
public OuterEnum OuterEnum { get; set; }

/// <summary>
/// Returns the string presentation of the object
/// </summary>
Expand Down Expand Up @@ -228,8 +227,7 @@ public override int GetHashCode()
hashCode = hashCode * 59 + this.EnumStringRequired.GetHashCode();
hashCode = hashCode * 59 + this.EnumInteger.GetHashCode();
hashCode = hashCode * 59 + this.EnumNumber.GetHashCode();
if (this.OuterEnum != null)
hashCode = hashCode * 59 + this.OuterEnum.GetHashCode();
hashCode = hashCode * 59 + this.OuterEnum.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,12 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Integer != null)
hashCode = hashCode * 59 + this.Integer.GetHashCode();
if (this.Int32 != null)
hashCode = hashCode * 59 + this.Int32.GetHashCode();
if (this.Int64 != null)
hashCode = hashCode * 59 + this.Int64.GetHashCode();
if (this.Number != null)
hashCode = hashCode * 59 + this.Number.GetHashCode();
if (this.Float != null)
hashCode = hashCode * 59 + this.Float.GetHashCode();
if (this.Double != null)
hashCode = hashCode * 59 + this.Double.GetHashCode();
hashCode = hashCode * 59 + this.Integer.GetHashCode();
hashCode = hashCode * 59 + this.Int32.GetHashCode();
hashCode = hashCode * 59 + this.Int64.GetHashCode();
hashCode = hashCode * 59 + this.Number.GetHashCode();
hashCode = hashCode * 59 + this.Float.GetHashCode();
hashCode = hashCode * 59 + this.Double.GetHashCode();
if (this.String != null)
hashCode = hashCode * 59 + this.String.GetHashCode();
if (this.Byte != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Name != null)
hashCode = hashCode * 59 + this.Name.GetHashCode();
hashCode = hashCode * 59 + this.Name.GetHashCode();
if (this.Class != null)
hashCode = hashCode * 59 + this.Class.GetHashCode();
return hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this._Name != null)
hashCode = hashCode * 59 + this._Name.GetHashCode();
if (this.SnakeCase != null)
hashCode = hashCode * 59 + this.SnakeCase.GetHashCode();
hashCode = hashCode * 59 + this._Name.GetHashCode();
hashCode = hashCode * 59 + this.SnakeCase.GetHashCode();
if (this.Property != null)
hashCode = hashCode * 59 + this.Property.GetHashCode();
if (this._123Number != null)
hashCode = hashCode * 59 + this._123Number.GetHashCode();
hashCode = hashCode * 59 + this._123Number.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.JustNumber != null)
hashCode = hashCode * 59 + this.JustNumber.GetHashCode();
hashCode = hashCode * 59 + this.JustNumber.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,13 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Id != null)
hashCode = hashCode * 59 + this.Id.GetHashCode();
if (this.PetId != null)
hashCode = hashCode * 59 + this.PetId.GetHashCode();
if (this.Quantity != null)
hashCode = hashCode * 59 + this.Quantity.GetHashCode();
hashCode = hashCode * 59 + this.Id.GetHashCode();
hashCode = hashCode * 59 + this.PetId.GetHashCode();
hashCode = hashCode * 59 + this.Quantity.GetHashCode();
if (this.ShipDate != null)
hashCode = hashCode * 59 + this.ShipDate.GetHashCode();
hashCode = hashCode * 59 + this.Status.GetHashCode();
if (this.Complete != null)
hashCode = hashCode * 59 + this.Complete.GetHashCode();
hashCode = hashCode * 59 + this.Complete.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.MyNumber != null)
hashCode = hashCode * 59 + this.MyNumber.GetHashCode();
hashCode = hashCode * 59 + this.MyNumber.GetHashCode();
if (this.MyString != null)
hashCode = hashCode * 59 + this.MyString.GetHashCode();
if (this.MyBoolean != null)
hashCode = hashCode * 59 + this.MyBoolean.GetHashCode();
hashCode = hashCode * 59 + this.MyBoolean.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Id != null)
hashCode = hashCode * 59 + this.Id.GetHashCode();
hashCode = hashCode * 59 + this.Id.GetHashCode();
if (this.Category != null)
hashCode = hashCode * 59 + this.Category.GetHashCode();
if (this.Name != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this._Return != null)
hashCode = hashCode * 59 + this._Return.GetHashCode();
hashCode = hashCode * 59 + this._Return.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.SpecialPropertyName != null)
hashCode = hashCode * 59 + this.SpecialPropertyName.GetHashCode();
hashCode = hashCode * 59 + this.SpecialPropertyName.GetHashCode();
return hashCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Id != null)
hashCode = hashCode * 59 + this.Id.GetHashCode();
hashCode = hashCode * 59 + this.Id.GetHashCode();
if (this.Name != null)
hashCode = hashCode * 59 + this.Name.GetHashCode();
return hashCode;
Expand Down
Loading