From 38287e88689b44ad94f16b5793e30f71399d677b Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Wed, 8 Nov 2023 11:11:22 +0200 Subject: [PATCH] Support configuring whether to generate StringEnumConverter --- .../EnumTests.cs | 19 +++++++++- ...nSchema.CodeGeneration.CSharp.Tests.csproj | 1 + ...ed_attribute_is_not_generated.verified.txt | 37 +++++++++++++++++++ .../VerifyHelper.cs | 19 ++++++++++ .../CSharpGeneratorSettings.cs | 4 ++ .../Models/ClassTemplateModel.cs | 3 ++ .../Templates/Class.liquid | 4 +- 7 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_StringEnumConverter_is_disabled_attribute_is_not_generated.verified.txt create mode 100644 src/NJsonSchema.CodeGeneration.CSharp.Tests/VerifyHelper.cs diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs index 2af50a47b..488384296 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs @@ -1,14 +1,17 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; using NJsonSchema.CodeGeneration.CSharp; -using NJsonSchema.Generation; using NJsonSchema.NewtonsoftJson.Generation; using System.Collections.Generic; using System.Threading.Tasks; +using VerifyXunit; using Xunit; +using static NJsonSchema.CodeGeneration.CSharp.Tests.VerifyHelper; + namespace NJsonSchema.CodeGeneration.Tests.CSharp { + [UsesVerify] public class EnumTests { [Fact] @@ -167,6 +170,20 @@ public async Task When_enum_is_nullable_then_StringEnumConverter_is_set() code); } + [Fact] + public async Task When_StringEnumConverter_is_disabled_attribute_is_not_generated() + { + //// Arrange + var schema = NewtonsoftJsonSchemaGenerator.FromType(); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, GenerateStringEnumConverterAttributes = false }); + + //// Act + var output = generator.GenerateFile(); + + //// Assert + await Verify(output); + } + public enum SomeEnum { Thing1, diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj index a346f6fac..6fb3b1d56 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_StringEnumConverter_is_disabled_attribute_is_not_generated.verified.txt b/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_StringEnumConverter_is_disabled_attribute_is_not_generated.verified.txt new file mode 100644 index 000000000..73fe6357a --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/Snapshots/EnumTests.When_StringEnumConverter_is_disabled_attribute_is_not_generated.verified.txt @@ -0,0 +1,37 @@ +//---------------------- +// +// Generated using the NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0) (http://NJsonSchema.org) +// +//---------------------- + + +namespace MyNamespace +{ + #pragma warning disable // Disable all warnings + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.0.0 (Newtonsoft.Json v13.0.0.0)")] + public enum MyStringEnum + { + + [System.Runtime.Serialization.EnumMember(Value = @"Foo")] + Foo = 0, + + + [System.Runtime.Serialization.EnumMember(Value = @"Bar")] + Bar = 1, + + + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.0.0 (Newtonsoft.Json v13.0.0.0)")] + public partial class MyStringEnumListTest + { + [Newtonsoft.Json.JsonProperty("Enums", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public System.Collections.Generic.ICollection Enums { get; set; } + + [Newtonsoft.Json.JsonProperty("NullableEnum", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public MyStringEnum? NullableEnum { get; set; } + + + } +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/VerifyHelper.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/VerifyHelper.cs new file mode 100644 index 000000000..7aab6a6f0 --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/VerifyHelper.cs @@ -0,0 +1,19 @@ +using System; +using VerifyTests; +using VerifyXunit; + +namespace NJsonSchema.CodeGeneration.CSharp.Tests; + +public static class VerifyHelper +{ + /// + /// Helper to run verify tests with sane defaults. + /// + public static SettingsTask Verify(string output) + { + return Verifier + .Verify(output) + .ScrubLinesContaining(StringComparison.OrdinalIgnoreCase, "Generated using the NSwag toolchain") + .UseDirectory("Snapshots"); + } +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs index 8c486e879..32fbb488a 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs @@ -37,6 +37,7 @@ public CSharpGeneratorSettings() RequiredPropertiesMustBeDefined = true; GenerateDataAnnotations = true; + GenerateStringEnumConverterAttributes = true; TypeAccessModifier = "public"; PropertySetterAccessModifier = string.Empty; GenerateJsonMethods = false; @@ -64,6 +65,9 @@ public CSharpGeneratorSettings() /// Gets or sets a value indicating whether to generated data annotation attributes (default: true). public bool GenerateDataAnnotations { get; set; } + /// Gets or sets a value indicating whether to add string to enum converter attribute for the property (default: true). + public bool GenerateStringEnumConverterAttributes { get; set; } + /// Gets or sets the any type (default: "object"). public string AnyType { get; set; } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs index ef3983f1b..561250830 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs @@ -147,6 +147,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, /// Gets a value indicating whether to use the DateFormatConverter. public bool UseDateFormatConverter => _settings.DateType.StartsWith("System.DateTime"); + /// Gets a value indicating whether to use the DateFormatConverter. + public bool UseStringEnumConverter => _settings.GenerateStringEnumConverterAttributes; + /// Gets or sets the access modifier of generated classes and interfaces. public string TypeAccessModifier => _settings.TypeAccessModifier; diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index 41666803f..d8235c4a4 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -60,7 +60,7 @@ // TODO(system.text.json): Add string enum item converter {%- endif -%} {%- else -%} - [Newtonsoft.Json.JsonProperty("{{ property.Name }}", Required = {{ property.JsonPropertyRequiredCode }}{% if property.IsStringEnumArray %}, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter){% endif %})] + [Newtonsoft.Json.JsonProperty("{{ property.Name }}", Required = {{ property.JsonPropertyRequiredCode }}{% if property.IsStringEnumArray and UseStringEnumConverter %}, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter){% endif %})] {%- endif -%} {%- if property.RenderRequiredAttribute -%} [System.ComponentModel.DataAnnotations.Required{% if property.AllowEmptyStrings %}(AllowEmptyStrings = true){% endif %}] @@ -80,7 +80,7 @@ {%- if property.RenderRegularExpressionAttribute -%} [System.ComponentModel.DataAnnotations.RegularExpression(@"{{ property.RegularExpressionValue }}")] {%- endif -%} -{%- if property.IsStringEnum -%} +{%- if property.IsStringEnum and UseStringEnumConverter -%} {%- if UseSystemTextJson -%} [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))] {%- else -%}