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

string should be string? for optional properties if "generateOptionalPropertiesAsNullable": true #4280

Open
SymboLinker opened this issue Dec 9, 2022 · 2 comments

Comments

@SymboLinker
Copy link

SymboLinker commented Dec 9, 2022

NSwagStudio v13.16.1.0

Runtime: Net60
Outputs: CSharpClient
Settings:

"generateOptionalPropertiesAsNullable": true,
"generateNullableReferenceTypes": true,

According to its docs, the Newtonsoft.Json.Required.DisallowNull enum value means:

The property is not required but it cannot be a null value.

so then properties that are marked that way are "optional".

For (EDIT: some) value type properties, the generated outputs is:

[Newtonsoft.Json.JsonProperty("repaymentType", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(3)]
public string RepaymentType { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("numberOfInstallments", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.Range(1, 99999)]
public int NumberOfInstallments { get; set; } = default!;

This is wrong: string should be string? and int should be int? for these optional properties.

For nullable reference types and enums it always looks OK:

[Newtonsoft.Json.JsonProperty("installmentAmount", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public MoneyAmount? InstallmentAmount { get; set; } = default!;

EDIT: wow, weird. At some places in the code, string? IS used correctly:

/// <summary>
/// The name of the institution that provides the income.
/// </summary>
[Newtonsoft.Json.JsonProperty("institutionName", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(30)]
public string? InstitutionName { get; set; } = default!;

Help for finding were to fix it:

  • It seems that if a property has a <summary>, then it is generated correctly (as nullable, with ?).
  • If there is no summary, it is generated wrongly (without ?, as non-nullable).

I have more than 15 cases of both and without exception!

@markm77
Copy link

markm77 commented Jun 26, 2023

Yeah, I noticed this for strings and DateTimeOffset values. Any chance of a fix @RicoSuter?

@RicoSuter
Copy link
Owner

Might be fixed with the next version of NJS/NSwag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants