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

Improved generated code with GenerateNullableReferenceTypes #1201

Merged
Merged
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
1 change: 1 addition & 0 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected override string GenerateFile(IEnumerable<CodeArtifact> artifactCollect
var model = new FileTemplateModel
{
Namespace = Settings.Namespace ?? string.Empty,
GenerateNullableReferenceTypes = Settings.GenerateNullableReferenceTypes,
TypesCode = artifactCollection.Concatenate()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
/// <summary>Gets the namespace.</summary>
public string Namespace => _settings.Namespace;

/// <summary>Gets a value indicating whether the C#8 nullable reference types are enabled for this file.</summary>
public bool GenerateNullableReferenceTypes => _settings.GenerateNullableReferenceTypes;

/// <summary>Gets a value indicating whether an additional properties type is available.</summary>
public bool HasAdditionalPropertiesType =>
!_schema.IsDictionary &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class FileTemplateModel
/// <summary>Gets or sets the namespace.</summary>
public string Namespace { get; set; }

/// <summary>Gets or sets a value indicating whether the C#8 nullable reference types are enabled for this file.</summary>
public bool GenerateNullableReferenceTypes { get; set; }

/// <summary>Gets or sets the types code.</summary>
public string TypesCode { get; set; }
}
Expand Down
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 @@ -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 -%}
Expand Down Expand Up @@ -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 }}; }
Expand Down
4 changes: 4 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp/Templates/File.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
// </auto-generated>
//----------------------

{% if GenerateNullableReferenceTypes -%}
#nullable enable

{% endif -%}
namespace {{ Namespace }}
{
#pragma warning disable // Disable all warnings
Expand Down