Skip to content

Commit 45f6c89

Browse files
authored
Add JsonLibraryVersion and generate JsonStringEnumMemberNameAttriute for >= 9.0, EnumMemberAttribute otherwise (#1865)
* fix: JsonStringEnumMemberNameAttriute only for .NET9, EnumMemberAttribute as fallback & tests * up * update * update
1 parent f55027b commit 45f6c89

16 files changed

+291
-12
lines changed

src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,27 @@ public async Task When_enum_list_uses_string_enums_then_ItemConverterType_is_set
181181
CSharpCompiler.AssertCompile(code);
182182
}
183183

184+
[Fact]
185+
public async Task When_enum_list_uses_string_enums_then_ItemConverterType_is_set_for_STJ_target()
186+
{
187+
// Arrange
188+
var schema = NewtonsoftJsonSchemaGenerator.FromType<MyStringEnumListTest>();
189+
var data = schema.ToJson();
190+
var generator =
191+
new CSharpGenerator(schema, new CSharpGeneratorSettings
192+
{
193+
ClassStyle = CSharpClassStyle.Poco,
194+
JsonLibrary = CSharpJsonLibrary.SystemTextJson
195+
});
196+
197+
// Act
198+
var code = generator.GenerateFile();
199+
200+
// Assert
201+
await VerifyHelper.Verify(code);
202+
CSharpCompiler.AssertCompile(code);
203+
}
204+
184205
[Fact]
185206
public async Task When_enum_is_nullable_then_StringEnumConverter_is_set()
186207
{

src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=False.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ namespace MyNamespace
2020
public enum MyClassSize
2121
{
2222

23-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"small")]
23+
[System.Runtime.Serialization.EnumMember(Value = @"small")]
2424
Small = 0,
2525

2626

27-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"medium")]
27+
[System.Runtime.Serialization.EnumMember(Value = @"medium")]
2828
Medium = 1,
2929

3030

31-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"large")]
31+
[System.Runtime.Serialization.EnumMember(Value = @"large")]
3232
Large = 2,
3333

3434

src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_enum_is_string_then_generic_StringEnumConverter_is_used_nullable=True.verified.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ namespace MyNamespace
2020
public enum MyClassSize
2121
{
2222

23-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"small")]
23+
[System.Runtime.Serialization.EnumMember(Value = @"small")]
2424
Small = 0,
2525

2626

27-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"medium")]
27+
[System.Runtime.Serialization.EnumMember(Value = @"medium")]
2828
Medium = 1,
2929

3030

31-
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"large")]
31+
[System.Runtime.Serialization.EnumMember(Value = @"large")]
3232
Large = 2,
3333

3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//----------------------
2+
// <auto-generated>
3+
// </auto-generated>
4+
//----------------------
5+
6+
7+
namespace MyNamespace
8+
{
9+
#pragma warning disable // Disable all warnings
10+
11+
public enum MyStringEnum
12+
{
13+
14+
[System.Runtime.Serialization.EnumMember(Value = @"Foo")]
15+
Foo = 0,
16+
17+
18+
[System.Runtime.Serialization.EnumMember(Value = @"Bar")]
19+
Bar = 1,
20+
21+
22+
}
23+
24+
public partial class MyStringEnumListTest
25+
{
26+
27+
[System.Text.Json.Serialization.JsonPropertyName("Enums")]
28+
// TODO(system.text.json): Add ItemConverterType with enum converter when supported
29+
public System.Collections.Generic.ICollection<MyStringEnum> Enums { get; set; }
30+
31+
[System.Text.Json.Serialization.JsonPropertyName("NullableEnum")]
32+
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter<MyStringEnum>))]
33+
public MyStringEnum? NullableEnum { get; set; }
34+
35+
}
36+
}

src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public CSharpGeneratorSettings()
120120
/// <summary>Gets or sets the CSharp JSON library to use (default: 'NewtonsoftJson', 'SystemTextJson' is experimental/not complete).</summary>
121121
public CSharpJsonLibrary JsonLibrary { get; set; }
122122

123+
/// <summary>Gets or sets the CSharp JSON library version to use (applies only to System.Text.Json, default: 8.0).</summary>
124+
public decimal JsonLibraryVersion { get; set; } = 8.0m;
125+
123126
/// <summary>Gets or sets the CSharp JSON polymorphic serialization style (default: 'NJsonSchema', 'SystemTextJson' is experimental/not complete).</summary>
124127
public CSharpJsonPolymorphicSerializationStyle JsonPolymorphicSerializationStyle { get; set; }
125128

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSett
5555
/// <summary>Gets a value indicating whether to use System.Text.Json</summary>
5656
public bool UseSystemTextJson => _settings.JsonLibrary == CSharpJsonLibrary.SystemTextJson;
5757

58+
/// <summary>Gets or sets the CSharp JSON library version to use.</summary>
59+
public decimal JsonLibraryVersion => _settings.JsonLibraryVersion;
60+
5861
/// <summary>Gets a value indicating whether the enum needs another base type to representing an extended value range.</summary>
5962
public bool HasExtendedValueRange => _schema.Format == JsonFormatStrings.Long;
6063

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
{%- if UseSystemTextJson -%}
6363
[System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")]
6464
{%- if property.IsStringEnumArray -%}
65-
// TODO(system.text.json): Add string enum item converter
65+
// TODO(system.text.json): Add ItemConverterType with enum converter when supported
6666
{%- endif -%}
6767
{%- else -%}
6868
[Newtonsoft.Json.JsonProperty("{{ property.Name }}", Required = {{ property.JsonPropertyRequiredCode }}{% if property.IsStringEnumArray %}, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter){% endif %})]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
{%- for enum in Enums %}
1414
{%- if IsStringEnum -%}
1515
{%- if UseSystemTextJson -%}
16+
{%- if JsonLibraryVersion >= 9.0 -%}
1617
[System.Text.Json.Serialization.JsonStringEnumMemberName(@"{{ enum.Value | replace: '"', '""' }}")]
18+
{%- else -%}
19+
[System.Runtime.Serialization.EnumMember(Value = @"{{ enum.Value | replace: '"', '""' }}")]
20+
{%- endif -%}
1721
{%- else -%}
1822
[System.Runtime.Serialization.EnumMember(Value = @"{{ enum.Value | replace: '"', '""' }}")]
1923
{%- endif -%}

src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public async Task When_export_types_is_true_add_export_before_enum_in_typescript
5252
var schema = NewtonsoftJsonSchemaGenerator.FromType<StringAndIntegerEnumTestClass>(new NewtonsoftJsonSchemaGeneratorSettings());
5353
var data = schema.ToJson();
5454

55-
TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings()
55+
TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings
5656
{
5757
ExportTypes = true
5858
};
@@ -72,8 +72,8 @@ public async Task When_add_export_keyword_is_false_dont_add_export_before_enum_i
7272
// Arrange
7373
var schema = NewtonsoftJsonSchemaGenerator.FromType<StringAndIntegerEnumTestClass>(new NewtonsoftJsonSchemaGeneratorSettings());
7474
var data = schema.ToJson();
75-
76-
TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings()
75+
76+
TypeScriptGeneratorSettings typeScriptGeneratorSettings = new TypeScriptGeneratorSettings
7777
{
7878
ExportTypes = false
7979
};
@@ -144,6 +144,30 @@ public async Task When_enum_has_string_value_then_CS_code_has_EnumMember_attribu
144144
await VerifyHelper.Verify(code);
145145
CSharpCompiler.AssertCompile(code);
146146
}
147+
148+
#if NET9_0_OR_GREATER
149+
150+
[Fact]
151+
public async Task When_enum_has_string_value_then_CS_code_has_JsonStringEnumMemberName_attribute()
152+
{
153+
// Arrange
154+
var schema = NewtonsoftJsonSchemaGenerator.FromType<ClassWithStringEnum>();
155+
var schemaData = schema.ToJson();
156+
157+
// Act
158+
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
159+
{
160+
JsonLibraryVersion = 9.0m,
161+
JsonLibrary = CSharpJsonLibrary.SystemTextJson
162+
});
163+
var code = generator.GenerateFile("MyClass");
164+
165+
// Assert
166+
await VerifyHelper.Verify(code);
167+
CSharpCompiler.AssertCompile(code);
168+
}
169+
170+
#endif
147171

148172
[Fact]
149173
public async Task When_enum_has_string_value_then_TS_code_has_string_value()

src/NJsonSchema.CodeGeneration.Tests/NJsonSchema.CodeGeneration.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
55
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
<NoWarn>$(NoWarn),1998,1591,618,IDE1006</NoWarn>

0 commit comments

Comments
 (0)