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

feat: option to use c# 11 required keyword for required properties #1711

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 @@ -46,6 +46,7 @@ public CSharpGeneratorSettings()
PropertySetterAccessModifier = string.Empty;
GenerateJsonMethods = false;
EnforceFlagEnums = false;
UseRequiredKeyword = false;

ValueGenerator = new CSharpValueGenerator(this);
PropertyNameGenerator = new CSharpPropertyNameGenerator();
Expand Down Expand Up @@ -147,6 +148,9 @@ public CSharpGeneratorSettings()
/// <summary>Gets or sets a value indicating whether enums should be always generated as bit flags (default: false).</summary>
public bool EnforceFlagEnums { get; set; }

/// <summary>Gets or sets a value indicating whether the C# 11 "required" keyword should be used for required properties (default: false). </summary>
public bool UseRequiredKeyword { get; set; }

/// <summary>Gets or sets a value indicating whether named/referenced dictionaries should be inlined or generated as class with dictionary inheritance.</summary>
public bool InlineNamedDictionaries { get; set; }

Expand All @@ -165,4 +169,4 @@ public CSharpGeneratorSettings()
/// <summary>Generate C# 9.0 record types instead of record-like classes.</summary>
public bool GenerateNativeRecords { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
/// <summary>Gets or sets the access modifier of generated classes and interfaces.</summary>
public string TypeAccessModifier => _settings.TypeAccessModifier;

/// <summary>Gets a value indicating whether to use the C# 11 "required" keyword.</summary>
public bool UseRequiredKeyword => _settings.UseRequiredKeyword;

/// <summary>Gets the access modifier of property setters (default: '').</summary>
public string PropertySetterAccessModifier => !string.IsNullOrEmpty(_settings.PropertySetterAccessModifier) ? _settings.PropertySetterAccessModifier + " " : "";

Expand All @@ -169,4 +172,4 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
/// <summary>Gets the deprecated message.</summary>
public string? DeprecatedMessage => _schema.DeprecatedMessage;
}
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
[System.Obsolete{% if property.HasDeprecatedMessage %}({{ property.DeprecatedMessage | literal }}){% endif %}]
{%- endif -%}
{%- template Class.Property.Annotations -%}
public {{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% elsif RenderRecord and GenerateNativeRecords %}init; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false %} = default!;{% endif %}
public {% if UseRequiredKeyword and property.IsRequired %}required {% endif %}{{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% elsif RenderRecord and GenerateNativeRecords %}init; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false %} = default!;{% endif %}
{% else %}
{
get { return {{ property.FieldName }}; }
Expand Down Expand Up @@ -143,4 +143,4 @@
{% template Class.Inpc %}
{%- endif -%}
{% template Class.Body %}
}
}
Loading