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

adjusted csharp codegen to allow for nullable properties #20299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
adjusted csharp codegen to allow for nullable properties (on value ty…
…pes only unless nullable reference types is enabled).

Edited tests to compensate for the actual data types. They were optional/not-required in the yaml, but came back with non-nullable types.
BryanAldrich committed Jan 29, 2025
commit d5478afea854df7544408cd8e319d6af0fc988ae
Original file line number Diff line number Diff line change
@@ -488,15 +488,15 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}

Double maximum = asDouble(property.maximum);
if (property.dataType.equals("int") && maximum != null) {
if (property.dataType.startsWith("int") && maximum != null) {
if ((!property.exclusiveMaximum && asInteger(property.maximum) == null) || (property.exclusiveMaximum && asInteger((maximum + 1) + "") == null)) {
property.dataType = "long";
property.datatypeWithEnum = "long";
}
}

Double minimum = asDouble(property.minimum);
if (property.dataType.equals("int") && minimum != null) {
if (property.dataType.startsWith("int") && minimum != null) {
if ((!property.exclusiveMinimum && asInteger(property.minimum) == null) || (property.exclusiveMinimum && asInteger((minimum - 1) + "") == null)) {
property.dataType = "long";
property.datatypeWithEnum = "long";
Original file line number Diff line number Diff line change
@@ -670,6 +670,10 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
postProcessEmitDefaultValue(property.vendorExtensions);

super.postProcessModelProperty(model, property);

if (!GENERICHOST.equals(getLibrary()) && !property.dataType.endsWith("?") && !property.required && property.defaultValue == null && (this.getValueTypes().contains(property.dataType) || (nullReferenceTypesFlag && !property.isArray && !property.isMap))) {
property.dataType = property.dataType + "?";
}
}

@Override
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ public void testUnsigned() {

final CodegenProperty property1 = cm1.allVars.get(2);
Assert.assertEquals(property1.baseName, "unsigned_integer");
Assert.assertEquals(property1.dataType, "uint");
Assert.assertEquals(property1.dataType, "uint?");
Assert.assertEquals(property1.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
Assert.assertTrue(property1.isPrimitiveType);
Assert.assertTrue(property1.isInteger);
@@ -82,7 +82,7 @@ public void testUnsigned() {

final CodegenProperty property2 = cm1.allVars.get(4);
Assert.assertEquals(property2.baseName, "unsigned_long");
Assert.assertEquals(property2.dataType, "ulong");
Assert.assertEquals(property2.dataType, "ulong?");
Assert.assertEquals(property2.vendorExtensions.get("x-unsigned"), Boolean.TRUE);
Assert.assertTrue(property2.isPrimitiveType);
Assert.assertTrue(property2.isLong);
Original file line number Diff line number Diff line change
@@ -194,7 +194,7 @@ public void simpleModelTest() {

final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.dataType, "DateTime");
Assert.assertEquals(property3.dataType, "DateTime?");
Assert.assertEquals(property3.name, "CreatedAt");
Assert.assertNull(property3.defaultValue);
Assert.assertEquals(property3.baseType, "DateTime");
@@ -243,7 +243,7 @@ public void nonNullablePropertyTest() {

final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "name");
Assert.assertEquals(property3.dataType, "string");
Assert.assertEquals(property3.dataType, "string?");
Assert.assertEquals(property3.name, "Name");
Assert.assertNull(property3.defaultValue);
Assert.assertEquals(property3.baseType, "string");
@@ -293,7 +293,7 @@ public void nullablePropertyTest() {

final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "name");
Assert.assertEquals(property3.dataType, "string");
Assert.assertEquals(property3.dataType, "string?");
Assert.assertEquals(property3.name, "Name");
Assert.assertNull(property3.defaultValue);
Assert.assertEquals(property3.baseType, "string");
@@ -510,7 +510,7 @@ public void complexPropertyTest() {

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "children");
Assert.assertEquals(property1.dataType, "Children");
Assert.assertEquals(property1.dataType, "Children?");
Assert.assertEquals(property1.name, "Children");
Assert.assertEquals(property1.baseType, "Children");
Assert.assertFalse(property1.required);
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Size** | **string** | | [optional]
**Color** | **string** | | [optional]
**Size** | **string?** | | [optional]
**Color** | **string?** | | [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
@@ -4,8 +4,8 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Id** | **long** | | [optional]
**Name** | **string** | | [optional]
**Id** | **long?** | | [optional]
**Name** | **string?** | | [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
@@ -4,11 +4,11 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Id** | **long** | Query | [optional]
**Id** | **long?** | Query | [optional]
**Outcomes** | **List<Query.OutcomesEnum>** | | [optional]
**Suffix** | **string** | test suffix | [optional]
**Text** | **string** | Some text containing white spaces | [optional]
**Date** | **DateTime** | A date | [optional]
**Suffix** | **string?** | test suffix | [optional]
**Text** | **string?** | Some text containing white spaces | [optional]
**Date** | **DateTime?** | A date | [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
@@ -12,7 +12,7 @@ Name | Type | Description | Notes
**ArrayString** | **List<string>** | | [optional]
**ArrayStringNullable** | **List<string>** | | [optional]
**ArrayStringExtensionNullable** | **List<string>** | | [optional]
**StringNullable** | **string** | | [optional]
**StringNullable** | **string?** | | [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
@@ -4,9 +4,9 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Number** | **decimal** | | [optional]
**Float** | **float** | | [optional]
**Double** | **double** | | [optional]
**Number** | **decimal?** | | [optional]
**Float** | **float?** | | [optional]
**Double** | **double?** | | [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
@@ -4,12 +4,12 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Id** | **long** | | [optional]
**Id** | **long?** | | [optional]
**Name** | **string** | |
**Category** | [**Category**](Category.md) | | [optional]
**Category** | [**Category?**](Category.md) | | [optional]
**PhotoUrls** | **List<string>** | |
**Tags** | [**List<Tag>**](Tag.md) | | [optional]
**Status** | **string** | pet status in the store | [optional]
**Status** | **string?** | pet status in the store | [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
@@ -4,7 +4,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Id** | **long** | Query | [optional]
**Id** | **long?** | Query | [optional]
**Outcomes** | **List<Query.OutcomesEnum>** | | [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
@@ -4,7 +4,7 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Name** | **string** | | [optional]
**Name** | **string?** | | [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
@@ -4,10 +4,10 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Size** | **string** | | [optional]
**Color** | **string** | | [optional]
**Id** | **long** | | [optional]
**Name** | **string** | | [optional]
**Size** | **string?** | | [optional]
**Color** | **string?** | | [optional]
**Id** | **long?** | | [optional]
**Name** | **string?** | | [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
@@ -47,13 +47,13 @@ public partial class Bird : IEquatable<Bird>, IValidatableObject
/// Gets or Sets Size
/// </summary>
[DataMember(Name = "size", EmitDefaultValue = false)]
public string Size { get; set; }
public string? Size { get; set; }

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

/// <summary>
/// Returns the string presentation of the object
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ public partial class Category : IEquatable<Category>, IValidatableObject
<example>1</example>
*/
[DataMember(Name = "id", EmitDefaultValue = false)]
public long Id { get; set; }
public long? Id { get; set; }

/// <summary>
/// Gets or Sets Name
@@ -59,7 +59,7 @@ public partial class Category : IEquatable<Category>, IValidatableObject
<example>Dogs</example>
*/
[DataMember(Name = "name", EmitDefaultValue = false)]
public string Name { get; set; }
public string? Name { get; set; }

/// <summary>
/// Returns the string presentation of the object
@@ -108,7 +108,8 @@ public bool Equals(Category input)
return
(
this.Id == input.Id ||
this.Id.Equals(input.Id)
(this.Id != null &&
this.Id.Equals(input.Id))
) &&
(
this.Name == input.Name ||
@@ -126,7 +127,10 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
hashCode = (hashCode * 59) + this.Id.GetHashCode();
if (this.Id != null)
{
hashCode = (hashCode * 59) + this.Id.GetHashCode();
}
if (this.Name != null)
{
hashCode = (hashCode * 59) + this.Name.GetHashCode();
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ public partial class DataQuery : Query, IEquatable<DataQuery>, IValidatableObjec
/// </summary>
/// <value>test suffix</value>
[DataMember(Name = "suffix", EmitDefaultValue = false)]
public string Suffix { get; set; }
public string? Suffix { get; set; }

/// <summary>
/// Some text containing white spaces
@@ -62,14 +62,14 @@ public partial class DataQuery : Query, IEquatable<DataQuery>, IValidatableObjec
<example>Some text</example>
*/
[DataMember(Name = "text", EmitDefaultValue = false)]
public string Text { get; set; }
public string? Text { get; set; }

/// <summary>
/// A date
/// </summary>
/// <value>A date</value>
[DataMember(Name = "date", EmitDefaultValue = false)]
public DateTime Date { get; set; }
public DateTime? Date { get; set; }

/// <summary>
/// Returns the string presentation of the object
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ public enum ArrayStringEnumDefaultEnum
/// Gets or Sets StringNullable
/// </summary>
[DataMember(Name = "string_nullable", EmitDefaultValue = true)]
public string StringNullable { get; set; }
public string? StringNullable { get; set; }

/// <summary>
/// Returns the string presentation of the object
Original file line number Diff line number Diff line change
@@ -49,19 +49,19 @@ public partial class NumberPropertiesOnly : IEquatable<NumberPropertiesOnly>, IV
/// Gets or Sets Number
/// </summary>
[DataMember(Name = "number", EmitDefaultValue = false)]
public decimal Number { get; set; }
public decimal? Number { get; set; }

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

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

/// <summary>
/// Returns the string presentation of the object
@@ -111,15 +111,18 @@ public bool Equals(NumberPropertiesOnly input)
return
(
this.Number == input.Number ||
this.Number.Equals(input.Number)
(this.Number != null &&
this.Number.Equals(input.Number))
) &&
(
this.Float == input.Float ||
this.Float.Equals(input.Float)
(this.Float != null &&
this.Float.Equals(input.Float))
) &&
(
this.Double == input.Double ||
this.Double.Equals(input.Double)
(this.Double != null &&
this.Double.Equals(input.Double))
);
}

@@ -132,9 +135,18 @@ public override int GetHashCode()
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
hashCode = (hashCode * 59) + this.Number.GetHashCode();
hashCode = (hashCode * 59) + this.Float.GetHashCode();
hashCode = (hashCode * 59) + this.Double.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();
}
return hashCode;
}
}
@@ -146,14 +158,14 @@ public override int GetHashCode()
/// <returns>Validation Result</returns>
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
// Double (double) maximum
if (this.Double > (double)50.2)
// Double (double?) maximum
if (this.Double > (double?)50.2)
{
yield return new ValidationResult("Invalid value for Double, must be a value less than or equal to 50.2.", new [] { "Double" });
}

// Double (double) minimum
if (this.Double < (double)0.8)
// Double (double?) minimum
if (this.Double < (double?)0.8)
{
yield return new ValidationResult("Invalid value for Double, must be a value greater than or equal to 0.8.", new [] { "Double" });
}
Loading