diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs index 6a72e7532..dfcb8e047 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs @@ -86,6 +86,7 @@ protected override string GenerateFile(IEnumerable artifactCollect var model = new FileTemplateModel { Namespace = Settings.Namespace ?? string.Empty, + GenerateNullableReferenceTypes = Settings.GenerateNullableReferenceTypes, TypesCode = artifactCollection.Concatenate() }; diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs index 1bd6fd6be..d836a1ef5 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs @@ -56,6 +56,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, /// Gets the namespace. public string Namespace => _settings.Namespace; + /// Gets a value indicating whether the C#8 nullable reference types are enabled for this file. + public bool GenerateNullableReferenceTypes => _settings.GenerateNullableReferenceTypes; + /// Gets a value indicating whether an additional properties type is available. public bool HasAdditionalPropertiesType => !_schema.IsDictionary && diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/FileTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/FileTemplateModel.cs index 633cccb1c..5a27111d4 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/FileTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/FileTemplateModel.cs @@ -14,6 +14,9 @@ public class FileTemplateModel /// Gets or sets the namespace. public string Namespace { get; set; } + /// Gets or sets a value indicating whether the C#8 nullable reference types are enabled for this file. + public bool GenerateNullableReferenceTypes { get; set; } + /// Gets or sets the types code. public string TypesCode { get; set; } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index cee67581d..3c5091057 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -24,7 +24,7 @@ {% endif -%} {% if RenderInpc or RenderPrism -%} {% for property in Properties -%} - private {{ property.Type }} {{ property.FieldName }}{% if property.HasDefaultValue %} = {{ property.DefaultValue }}{% endif -%}; + private {{ property.Type }} {{ property.FieldName }}{% if property.HasDefaultValue %} = {{ property.DefaultValue }}{% elsif GenerateNullableReferenceTypes -%} = default!{% endif -%}; {% endfor -%} {% endif -%} @@ -63,7 +63,7 @@ [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))] {% endif -%} {% template Class.Property.Annotations %} - public {{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% endif %} + public {{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false -%} = default!;{% endif %} {% else %} { get { return {{ property.FieldName }}; } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid index c48cf9577..1e1758d73 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid @@ -4,6 +4,10 @@ // //---------------------- +{% if GenerateNullableReferenceTypes -%} +#nullable enable + +{% endif -%} namespace {{ Namespace }} { #pragma warning disable // Disable all warnings