Skip to content

Commit

Permalink
adjusted csharp codegen to allow for nullable properties (on value ty…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
BryanAldrich committed Dec 11, 2024
1 parent e7b5f34 commit 99d050f
Show file tree
Hide file tree
Showing 537 changed files with 2,905 additions and 1,862 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/csharp-restsharp/docs/Bird.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

4 changes: 2 additions & 2 deletions samples/client/echo_api/csharp-restsharp/docs/Category.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

8 changes: 4 additions & 4 deletions samples/client/echo_api/csharp-restsharp/docs/DataQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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)

6 changes: 3 additions & 3 deletions samples/client/echo_api/csharp-restsharp/docs/Pet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

2 changes: 1 addition & 1 deletion samples/client/echo_api/csharp-restsharp/docs/Query.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/csharp-restsharp/docs/Tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ||
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
);
}

Expand All @@ -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;
}
}
Expand All @@ -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" });
}
Expand Down
Loading

0 comments on commit 99d050f

Please sign in to comment.