From 8ca84462931efd6ce2b736c185c177a7c4d9b006 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Thu, 11 Jan 2024 11:18:32 +0000 Subject: [PATCH 1/2] Let non-required, nullable properties of Record classes be C#-optional in the constructor --- src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs | 3 +++ .../Templates/Class.Constructor.Record.liquid | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index bc1684a9b..d2110f584 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -54,6 +54,9 @@ public PropertyModel( /// Gets a value indicating whether the property is nullable. public override bool IsNullable => (_settings.GenerateOptionalPropertiesAsNullable && !_property.IsRequired) || base.IsNullable; + /// Gets a value indicating whether the property's corresponding constructor parameter should be optional. + public bool RenderOptionalCtorArgument => IsNullable && _property.IsRequired; + /// Gets or sets a value indicating whether empty strings are allowed. public bool AllowEmptyStrings => _property.ActualTypeSchema.Type.IsString() && diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid index 5f576efd8..634f1d4a8 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.Constructor.Record.liquid @@ -5,7 +5,7 @@ {%- assign parentProperties = "" | empty %} {% endif %} -{%- assign sortedProperties = AllProperties | sort: "Name" %} +{%- assign sortedProperties = AllProperties | sort: "RenderOptionalCtorArgument", "Name" %} {%- assign sortedParentProperties = parentProperties | sort: "Name" %} {%- if UseSystemTextJson %} @@ -13,7 +13,7 @@ {%- else %} [Newtonsoft.Json.JsonConstructor] {%- endif %} -{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Type }} @{{ property.Name | lowercamelcase }}{% endfor %}) +{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Type }} @{{ property.Name | lowercamelcase }}{% if property.RenderOptionalCtorArgument %} = null{% endif %}{% endfor %}) {%- assign skipComma = true %} {%- if HasInheritance %} : base({%- for property in sortedParentProperties %}{%- if skipComma %}{%- assign skipComma = false %}{% else %}, {% endif %}{{ property.Name | lowercamelcase }}{%- endfor %}) From 8e755266e4b3434f7f88d090752b00d9b274118c Mon Sep 17 00:00:00 2001 From: adamjones2 <102040616+adamjones2@users.noreply.github.com> Date: Sun, 30 Mar 2025 19:28:26 +0100 Subject: [PATCH 2/2] Update PropertyModel.cs --- src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index f1eb2a481..38efb2c17 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -66,7 +66,7 @@ public PropertyModel( public override bool IsNullable => (_settings.GenerateOptionalPropertiesAsNullable && !_property.IsRequired) || base.IsNullable; /// Gets a value indicating whether the property's corresponding constructor parameter should be optional. - public bool RenderOptionalCtorArgument => IsNullable && _property.IsRequired; + public bool RenderOptionalCtorArgument => IsNullable && !_property.IsRequired; /// Gets or sets a value indicating whether empty strings are allowed. public bool AllowEmptyStrings => @@ -339,4 +339,4 @@ private static string GetRangeType(string? propertyFormat) => _ => "double", }; } -} \ No newline at end of file +}