Skip to content

Commit c796be3

Browse files
authored
CSharp gen: Fix required keyword and add write accessor (set/init) (#1862)
1 parent 45f6c89 commit c796be3

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public CSharpGeneratorSettings()
4646
GenerateJsonMethods = false;
4747
EnforceFlagEnums = false;
4848
UseRequiredKeyword = false;
49+
WriteAccessor = "set";
4950

5051
ValueGenerator = new CSharpValueGenerator(this);
5152
PropertyNameGenerator = new CSharpPropertyNameGenerator();
@@ -155,6 +156,9 @@ public CSharpGeneratorSettings()
155156

156157
/// <summary>Gets or sets a value indicating whether the C# 11 "required" keyword should be used for required properties (default: false). </summary>
157158
public bool UseRequiredKeyword { get; set; }
159+
160+
/// <summary> Gets the read accessor of properties ('set' | 'init').</summary>
161+
public string WriteAccessor { get; set; }
158162

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

src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
170170

171171
/// <summary>Gets a value indicating whether to use the C# 11 "required" keyword.</summary>
172172
public bool UseRequiredKeyword => _settings.UseRequiredKeyword;
173+
174+
/// <summary> Gets the read accessor of properties ('set' | 'init').</summary>
175+
public string WriteAccessor => _settings.WriteAccessor;
173176

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

src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
[System.Obsolete{% if property.HasDeprecatedMessage %}({{ property.DeprecatedMessage | literal }}){% endif %}]
108108
{%- endif -%}
109109
{%- template Class.Property.Annotations -%}
110-
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 %}
110+
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 %}{{ WriteAccessor }}; {% elsif RenderRecord and GenerateNativeRecords %}init; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false and UseRequiredKeyword == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false and UseRequiredKeyword == false %} = default!;{% endif %}
111111
{%- else -%}
112112
{
113113
get { return {{ property.FieldName }}; }

0 commit comments

Comments
 (0)