Skip to content

[csharp] Mark optional fields in generated client properties as nullable #17266

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

Closed
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 @@ -1513,6 +1513,14 @@ protected void patchProperty(Map<String, CodegenModel> enumRefs, CodegenModel mo
property.isInherited = true;
}
}

if (!GENERICHOST.equals(getLibrary()) && !property.dataType.endsWith("?") && !property.required && (nullReferenceTypesFlag || this.getNullableTypes().contains(property.dataType))) {
property.dataType = property.dataType + "?";
}
// Don't attach a '?' to enums - that's already handled by the templates
if (!GENERICHOST.equals(getLibrary()) && !property.isEnum && !property.datatypeWithEnum.endsWith("?") && !property.required && (nullReferenceTypesFlag || this.getNullableTypes().contains(property.datatypeWithEnum))) {
property.datatypeWithEnum = property.datatypeWithEnum + "?";
}
}

@Override
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)

10 changes: 5 additions & 5 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]
**Outcomes** | **List&lt;Query.OutcomesEnum&gt;** | | [optional]
**Suffix** | **string** | test suffix | [optional]
**Text** | **string** | Some text containing white spaces | [optional]
**Date** | **DateTime** | A date | [optional]
**Id** | **long?** | Query | [optional]
**Outcomes** | **List&lt;Query.OutcomesEnum&gt;?** | | [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)

16 changes: 8 additions & 8 deletions samples/client/echo_api/csharp-restsharp/docs/DefaultValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ to test the default value of properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ArrayStringEnumRefDefault** | [**List&lt;StringEnumRef&gt;**](StringEnumRef.md) | | [optional]
**ArrayStringEnumDefault** | **List&lt;DefaultValue.ArrayStringEnumDefaultEnum&gt;** | | [optional]
**ArrayStringDefault** | **List&lt;string&gt;** | | [optional]
**ArrayIntegerDefault** | **List&lt;int&gt;** | | [optional]
**ArrayString** | **List&lt;string&gt;** | | [optional]
**ArrayStringNullable** | **List&lt;string&gt;** | | [optional]
**ArrayStringExtensionNullable** | **List&lt;string&gt;** | | [optional]
**StringNullable** | **string** | | [optional]
**ArrayStringEnumRefDefault** | [**List&lt;StringEnumRef&gt;?**](StringEnumRef.md) | | [optional]
**ArrayStringEnumDefault** | **List&lt;DefaultValue.ArrayStringEnumDefaultEnum&gt;?** | | [optional]
**ArrayStringDefault** | **List&lt;string&gt;?** | | [optional]
**ArrayIntegerDefault** | **List&lt;int&gt;?** | | [optional]
**ArrayString** | **List&lt;string&gt;?** | | [optional]
**ArrayStringNullable** | **List&lt;string&gt;?** | | [optional]
**ArrayStringExtensionNullable** | **List&lt;string&gt;?** | | [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]
**VarFloat** | **float** | | [optional]
**VarDouble** | **double** | | [optional]
**Number** | **decimal?** | | [optional]
**VarFloat** | **float?** | | [optional]
**VarDouble** | **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)

8 changes: 4 additions & 4 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&lt;string&gt;** | |
**Tags** | [**List&lt;Tag&gt;**](Tag.md) | | [optional]
**Status** | **string** | pet status in the store | [optional]
**Tags** | [**List&lt;Tag&gt;?**](Tag.md) | | [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)

4 changes: 2 additions & 2 deletions samples/client/echo_api/csharp-restsharp/docs/Query.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

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

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Values** | **List&lt;string&gt;** | | [optional]
**Values** | **List&lt;string&gt;?** | | [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 @@ -37,7 +37,7 @@ public partial class Bird : IEquatable<Bird>, IValidatableObject
/// </summary>
/// <param name="size">size.</param>
/// <param name="color">color.</param>
public Bird(string size = default(string), string color = default(string))
public Bird(string? size = default(string?), string? color = default(string?))
{
this.Size = size;
this.Color = color;
Expand All @@ -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 @@ -37,7 +37,7 @@ public partial class Category : IEquatable<Category>, IValidatableObject
/// </summary>
/// <param name="id">id.</param>
/// <param name="name">name.</param>
public Category(long id = default(long), string name = default(string))
public Category(long? id = default(long?), string? name = default(string?))
{
this.Id = id;
this.Name = name;
Expand All @@ -48,14 +48,14 @@ public partial class Category : IEquatable<Category>, IValidatableObject
/// </summary>
/// <example>1</example>
[DataMember(Name = "id", EmitDefaultValue = false)]
public long Id { get; set; }
public long? Id { get; set; }

/// <summary>
/// Gets or Sets Name
/// </summary>
/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public partial class DataQuery : Query, IEquatable<DataQuery>, IValidatableObjec
/// <param name="date">A date.</param>
/// <param name="id">Query.</param>
/// <param name="outcomes">outcomes.</param>
public DataQuery(string suffix = default(string), string text = default(string), DateTime date = default(DateTime), long id = default(long), List<OutcomesEnum> outcomes = default(List<OutcomesEnum>)) : base(id, outcomes)
public DataQuery(string? suffix = default(string?), string? text = default(string?), DateTime? date = default(DateTime?), long? id = default(long?), List<OutcomesEnum> outcomes = default(List<OutcomesEnum>)) : base(id, outcomes)
{
this.Suffix = suffix;
this.Text = text;
Expand All @@ -52,22 +52,22 @@ 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
/// </summary>
/// <value>Some text containing white spaces</value>
/// <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 @@ -68,7 +68,7 @@ public enum ArrayStringEnumDefaultEnum
/// <param name="arrayStringNullable">arrayStringNullable.</param>
/// <param name="arrayStringExtensionNullable">arrayStringExtensionNullable.</param>
/// <param name="stringNullable">stringNullable.</param>
public DefaultValue(List<StringEnumRef> arrayStringEnumRefDefault = default(List<StringEnumRef>), List<ArrayStringEnumDefaultEnum> arrayStringEnumDefault = default(List<ArrayStringEnumDefaultEnum>), List<string> arrayStringDefault = default(List<string>), List<int> arrayIntegerDefault = default(List<int>), List<string> arrayString = default(List<string>), List<string> arrayStringNullable = default(List<string>), List<string> arrayStringExtensionNullable = default(List<string>), string stringNullable = default(string))
public DefaultValue(List<StringEnumRef>? arrayStringEnumRefDefault = default(List<StringEnumRef>?), List<ArrayStringEnumDefaultEnum> arrayStringEnumDefault = default(List<ArrayStringEnumDefaultEnum>), List<string>? arrayStringDefault = default(List<string>?), List<int>? arrayIntegerDefault = default(List<int>?), List<string>? arrayString = default(List<string>?), List<string>? arrayStringNullable = default(List<string>?), List<string>? arrayStringExtensionNullable = default(List<string>?), string? stringNullable = default(string?))
{
this.ArrayStringEnumRefDefault = arrayStringEnumRefDefault;
this.ArrayStringEnumDefault = arrayStringEnumDefault;
Expand All @@ -84,49 +84,49 @@ public enum ArrayStringEnumDefaultEnum
/// Gets or Sets ArrayStringEnumRefDefault
/// </summary>
[DataMember(Name = "array_string_enum_ref_default", EmitDefaultValue = false)]
public List<StringEnumRef> ArrayStringEnumRefDefault { get; set; }
public List<StringEnumRef>? ArrayStringEnumRefDefault { get; set; }

/// <summary>
/// Gets or Sets ArrayStringEnumDefault
/// </summary>
[DataMember(Name = "array_string_enum_default", EmitDefaultValue = false)]
public List<DefaultValue.ArrayStringEnumDefaultEnum> ArrayStringEnumDefault { get; set; }
public List<DefaultValue.ArrayStringEnumDefaultEnum>? ArrayStringEnumDefault { get; set; }

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

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

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

/// <summary>
/// Gets or Sets ArrayStringNullable
/// </summary>
[DataMember(Name = "array_string_nullable", EmitDefaultValue = true)]
public List<string> ArrayStringNullable { get; set; }
public List<string>? ArrayStringNullable { get; set; }

/// <summary>
/// Gets or Sets ArrayStringExtensionNullable
/// </summary>
[DataMember(Name = "array_string_extension_nullable", EmitDefaultValue = true)]
public List<string> ArrayStringExtensionNullable { get; set; }
public List<string>? ArrayStringExtensionNullable { get; set; }

/// <summary>
/// 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 @@ -38,7 +38,7 @@ public partial class NumberPropertiesOnly : IEquatable<NumberPropertiesOnly>, IV
/// <param name="number">number.</param>
/// <param name="varFloat">varFloat.</param>
/// <param name="varDouble">varDouble.</param>
public NumberPropertiesOnly(decimal number = default(decimal), float varFloat = default(float), double varDouble = default(double))
public NumberPropertiesOnly(decimal? number = default(decimal?), float? varFloat = default(float?), double? varDouble = default(double?))
{
this.Number = number;
this.VarFloat = varFloat;
Expand All @@ -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 VarFloat
/// </summary>
[DataMember(Name = "float", EmitDefaultValue = false)]
public float VarFloat { get; set; }
public float? VarFloat { get; set; }

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

/// <summary>
/// Returns the string presentation of the object
Expand Down Expand Up @@ -146,14 +146,14 @@ public override int GetHashCode()
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
// VarDouble (double) maximum
if (this.VarDouble > (double)50.2)
// VarDouble (double?) maximum
if (this.VarDouble > (double?)50.2)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for VarDouble, must be a value less than or equal to 50.2.", new [] { "VarDouble" });
}

// VarDouble (double) minimum
if (this.VarDouble < (double)0.8)
// VarDouble (double?) minimum
if (this.VarDouble < (double?)0.8)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for VarDouble, must be a value greater than or equal to 0.8.", new [] { "VarDouble" });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected Pet() { }
/// <param name="photoUrls">photoUrls (required).</param>
/// <param name="tags">tags.</param>
/// <param name="status">pet status in the store.</param>
public Pet(long id = default(long), string name = default(string), Category category = default(Category), List<string> photoUrls = default(List<string>), List<Tag> tags = default(List<Tag>), StatusEnum? status = default(StatusEnum?))
public Pet(long? id = default(long?), string name = default(string), Category? category = default(Category?), List<string> photoUrls = default(List<string>), List<Tag>? tags = default(List<Tag>?), StatusEnum? status = default(StatusEnum?))
{
// to ensure "name" is required (not null)
if (name == null)
Expand All @@ -104,7 +104,7 @@ protected Pet() { }
/// </summary>
/// <example>10</example>
[DataMember(Name = "id", EmitDefaultValue = false)]
public long Id { get; set; }
public long? Id { get; set; }

/// <summary>
/// Gets or Sets Name
Expand All @@ -117,7 +117,7 @@ protected Pet() { }
/// Gets or Sets Category
/// </summary>
[DataMember(Name = "category", EmitDefaultValue = false)]
public Category Category { get; set; }
public Category? Category { get; set; }

/// <summary>
/// Gets or Sets PhotoUrls
Expand All @@ -129,7 +129,7 @@ protected Pet() { }
/// Gets or Sets Tags
/// </summary>
[DataMember(Name = "tags", EmitDefaultValue = false)]
public List<Tag> Tags { get; set; }
public List<Tag>? Tags { get; set; }

/// <summary>
/// Returns the string presentation of the object
Expand Down
Loading