From e19e21e2e839f1d6d483db42e6af0cd1f1b12d3d Mon Sep 17 00:00:00 2001 From: Christoph Genneper Date: Wed, 8 Jun 2022 00:09:52 +0200 Subject: [PATCH 1/2] Add support for flag enums with more than 32 values --- .../EnumTests.cs | 73 +++++++++++++++---- .../Models/EnumTemplateModel.cs | 3 + .../Templates/Enum.liquid | 2 +- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs index 2e1a62088..28d6d856e 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs @@ -15,7 +15,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated() { //// Arrange var json = - @"{ + @"{ ""type"": ""object"", ""properties"": { ""category"" : { @@ -41,7 +41,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags() { //// Arrange var json = - @"{ + @"{ ""type"": ""object"", ""properties"": { ""category"" : { @@ -57,7 +57,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags() }"; var schema = await JsonSchema.FromJsonAsync(json); - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { EnforceFlagEnums = true }); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings {EnforceFlagEnums = true}); //// Act var code = generator.GenerateFile("MyClass"); @@ -139,7 +139,8 @@ public async Task When_enum_list_uses_string_enums_then_ItemConverterType_is_set //// Arrange var schema = JsonSchema.FromType(); var data = schema.ToJson(); - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); + var generator = + new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco}); //// Act var code = generator.GenerateFile(); @@ -154,16 +155,22 @@ public async Task When_enum_is_nullable_then_StringEnumConverter_is_set() //// Arrange var schema = JsonSchema.FromType(); var data = schema.ToJson(); - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); + var generator = + new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco}); //// Act var code = generator.GenerateFile(); //// Assert - Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]", code); + Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]", + code); } - public enum SomeEnum { Thing1, Thing2 } + public enum SomeEnum + { + Thing1, + Thing2 + } public class SomeClass { @@ -179,7 +186,8 @@ public async Task When_class_has_enum_array_property_then_enum_name_is_preserved var json = schema.ToJson(); //// Act - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); + var generator = + new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco}); var code = generator.GenerateFile(); //// Assert @@ -232,7 +240,7 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null { //// Arrange var json = - @"{ + @"{ ""type"": ""object"", ""required"": [ ""name"", @@ -251,7 +259,7 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null } }"; var schema = await JsonSchema.FromJsonAsync(json); - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { SchemaType = SchemaType.Swagger2 }); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings {SchemaType = SchemaType.Swagger2}); //// Act var code = generator.GenerateFile("MyClass"); @@ -523,7 +531,7 @@ public async Task When_enum_is_nullable_required_it_should_be_nullable_with_conv //// Act var schema = await JsonSchema.FromJsonAsync(json); - var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true }; + var settings = new CSharpGeneratorSettings {EnforceFlagEnums = true}; var generator = new CSharpGenerator(schema, settings); var code = generator.GenerateFile("Foo"); @@ -539,7 +547,7 @@ public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_om { //// Arrange var json = - @"{ + @"{ ""type"": ""object"", ""properties"": { ""category"" : { @@ -565,7 +573,44 @@ public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_om var code = generator.GenerateFile("MyClass"); //// Assert - Assert.Contains("public MyClassCategory? Category { get; set; } = MyNamespace.MyClassCategory.Commercial;", code); + Assert.Contains("public MyClassCategory? Category { get; set; } = MyNamespace.MyClassCategory.Commercial;", + code); + } + + [Fact] + public async Task When_enum_has_a_format_then_enum_is_generated_with_correct_basetype() + { + // Arrange + var json = @" +{ + ""properties"": { + ""foo"": { + ""$ref"": ""#/definitions/ManyValuesTestEnum"" + } + }, + ""definitions"": { + ""ManyValuesTestEnum"": { + ""type"": ""integer"", + ""format"": ""int64"", + ""x-enumNames"": [ + ""None"", + ""FirstBit"" + ], + ""enum"": [ + 0, + 1 + ] + } + } +}"; + var schema = await JsonSchema.FromJsonAsync(json); + var generator = new CSharpGenerator(schema); + + //// Act + var code = generator.GenerateFile("MyClass"); + + //// Assert + Assert.Contains("public enum ManyValuesTestEnum : long", code); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs index b897ecda9..7d2c3a85c 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs @@ -52,6 +52,9 @@ public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSett /// Gets or sets if we output as Bit Flags. public bool IsEnumAsBitFlags => _settings.EnforceFlagEnums || _schema.IsFlagEnumerable; + /// Gets a value indicating whether the enum needs an other base type to representing an extended value range. + public bool HasExtendedValueRange => _schema.Format == JsonFormatStrings.Long; + /// Gets the enum values. public IEnumerable Enums { diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid index 56401d8d3..849187e90 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Enum.liquid @@ -7,7 +7,7 @@ {%- if IsEnumAsBitFlags -%} [System.Flags] {%- endif -%} -{{ TypeAccessModifier }} enum {{ Name }} +{{ TypeAccessModifier }} enum {{ Name }}{%- if HasExtendedValueRange %} : long{% endif %} { {%- for enum in Enums %} {%- if IsStringEnum -%} From 869649e5ef8a17c4da0e9becc7c9d8764cfc6e7b Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Fri, 16 Sep 2022 15:25:20 +0200 Subject: [PATCH 2/2] Update EnumTests.cs --- src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs index 28d6d856e..695563538 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs @@ -156,7 +156,7 @@ public async Task When_enum_is_nullable_then_StringEnumConverter_is_set() var schema = JsonSchema.FromType(); var data = schema.ToJson(); var generator = - new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco}); + new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); //// Act var code = generator.GenerateFile(); @@ -187,7 +187,7 @@ public async Task When_class_has_enum_array_property_then_enum_name_is_preserved //// Act var generator = - new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco}); + new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile(); //// Assert @@ -259,7 +259,7 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null } }"; var schema = await JsonSchema.FromJsonAsync(json); - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings {SchemaType = SchemaType.Swagger2}); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { SchemaType = SchemaType.Swagger2 }); //// Act var code = generator.GenerateFile("MyClass"); @@ -613,4 +613,4 @@ public async Task When_enum_has_a_format_then_enum_is_generated_with_correct_bas Assert.Contains("public enum ManyValuesTestEnum : long", code); } } -} \ No newline at end of file +}