Skip to content

Commit

Permalink
Enable support for C# 9.0 records (#1419)
Browse files Browse the repository at this point in the history
* Enable support for C# 9.0 records

* Make unit test pass by upgrading AssertCompile's Roslyn dependency to understand C# 9.0. This requires dropping the dependency on .NET 4.52.

* Code review: rename suggested
  • Loading branch information
MaxWilson authored Oct 25, 2021
1 parent 424e9ad commit 3aef4ea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,31 @@ public async Task When_record_no_setter_in_class_and_constructor_provided()
AssertCompile(output);
}

[Fact]
public async Task When_native_record_no_setter_in_class_and_constructor_provided()
{
//// Arrange
var schema = JsonSchema.FromType<Address>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record,
GenerateNativeRecords = true
});

//// Act
var output = generator.GenerateFile("Address");

//// Assert
Assert.Contains(@"record Address", output);
Assert.Contains(@"public string Street { get; init; }", output);
Assert.DoesNotContain(@"public string Street { get; set; }", output);

Assert.Contains("public Address(string @city, string @street)", output);

AssertCompile(output);
}

public abstract class AbstractAddress
{
[JsonProperty("city")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp5.0;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1998,1591</NoWarn>
Expand Down Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" Condition="'$(TargetFramework)' == 'netcoreapp5.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" Condition="'$(TargetFramework)' == 'netcoreapp5.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.2" Condition="'$(TargetFramework)' == 'net452'" />

<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,8 @@ public CSharpGeneratorSettings()

/// <summary>Gets or sets a value indicating whether to generate Nullable Reference Type annotations (default: false).</summary>
public bool GenerateNullableReferenceTypes { get; set; }

/// <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 @@ -102,6 +102,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
/// <summary>Gets a value indicating whether the class style is Record.</summary>
public bool RenderRecord => _settings.ClassStyle == CSharpClassStyle.Record;

/// <summary>Gets a value indicating whether to generate records as C# 9.0 records.</summary>
public bool GenerateNativeRecords => _settings.GenerateNativeRecords;

/// <summary>Gets a value indicating whether to render ToJson() and FromJson() methods.</summary>
public bool GenerateJsonMethods => _settings.GenerateJsonMethods;

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 @@ -25,7 +25,7 @@
[System.Obsolete{% if HasDeprecatedMessage %}({{ DeprecatedMessage | literal }}){% endif %}]
{% endif -%}
{% template Class.Annotations %}
{{ TypeAccessModifier }} {% if IsAbstract %}abstract {% endif %}partial class {{ClassName}} {% template Class.Inheritance %}
{{ TypeAccessModifier }} {% if IsAbstract %}abstract {% endif %}partial {% if GenerateNativeRecords %}record{% else %}class{% endif %} {{ClassName}} {% template Class.Inheritance %}
{
{% if IsTuple -%}
public {{ ClassName }}({% for tupleType in TupleTypes -%}{{ tupleType }} item{{ forloop.index }}{% if forloop.last == false %}, {% endif %}{% endfor -%}) : base({% for tupleType in TupleTypes -%}item{{ forloop.index }}{% if forloop.last == false %}, {% endif %}{% endfor -%})
Expand Down Expand Up @@ -92,7 +92,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; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false -%} = default!;{% endif %}
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 %}
{% else %}
{
get { return {{ property.FieldName }}; }
Expand Down

0 comments on commit 3aef4ea

Please sign in to comment.