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

Adds configuration settings to influence required annotation #1563

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public CSharpGeneratorSettings()
/// (sets Required.Always when the property is required) (default: true).</summary>
public bool RequiredPropertiesMustBeDefined { get; set; }

/// <summary>Gets or sets a value indicating whether a required property should be treated as nullable by
/// default (if the property is not explicitly market as nullable: false)
/// (sets Required.Default when the property is not required and not explicitly marked as nullable: false).
/// </summary>
public bool DefaultNonRequiredToNullable { get; set; }

/// <summary>Gets or sets a value indicating whether to generated data annotation attributes (default: true).</summary>
public bool GenerateDataAnnotations { get; set; }

Expand Down
11 changes: 11 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public string JsonPropertyRequiredCode
{
get
{
if (_settings.DefaultNonRequiredToNullable && !_property.IsRequired)
{
if (_property.IsNullableRaw == false)
{
return "Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore";
}
else
{
return "Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore";
}
}
if (_settings.RequiredPropertiesMustBeDefined && _property.IsRequired)
{
if (!_property.IsNullable(_settings.SchemaType))
Expand Down